| |
| """ |
| Usage: python scripts/query_cli.py |
| |
| Interactive CLI for asking questions about ENGR 445 material. |
| Type 'quit' to exit. |
| """ |
| import argparse |
| from query.retriever import retrieve |
| from query.prompt_builder import build_prompt |
| from query.generator import generate |
|
|
|
|
| def main(verbose=False): |
| """Main CLI function.""" |
| print("ENGR 445 RAG System - Question Answering CLI") |
| print("Type 'quit' to exit.\n") |
| |
| while True: |
| |
| question = input("Question: ").strip() |
| |
| |
| if question.lower() in ['quit', 'exit', 'q']: |
| print("Goodbye!") |
| break |
| |
| if not question: |
| continue |
| |
| |
| print("\n๐ Retrieving relevant sources...") |
| chunks = retrieve(question) |
| |
| print(f"\nFound {len(chunks)} relevant sections:") |
| for i, chunk in enumerate(chunks, 1): |
| source = chunk["metadata"].get("source", "unknown").split('/')[-1] |
| page = chunk["metadata"].get("page", chunk["metadata"].get("slide", "?")) |
| distance = chunk.get("distance", "?") |
| print(f" {i}. {source}") |
| print(f" Page/Slide: {page} | Relevance: {distance:.4f}") |
| |
| |
| print("\n๐ง Building prompt...") |
| prompt = build_prompt(question, chunks) |
| |
| |
| print("Generating response (this may take 15-20 seconds)...") |
| response = generate(prompt) |
| |
| |
| print(f"\n{'='*70}") |
| print(f"Answer:\n{response}\n") |
| print(f"{'='*70}") |
| |
| if verbose and chunks: |
| print("\n๐ Full Retrieved Context:") |
| for i, chunk in enumerate(chunks, 1): |
| source = chunk["metadata"].get("source", "unknown") |
| page = chunk["metadata"].get("page", chunk["metadata"].get("slide", "?")) |
| print(f"\n--- Source {i}: {source} (page/slide {page}) ---") |
| print(chunk["text"][:400] + ("..." if len(chunk["text"]) > 400 else "")) |
| |
| print("\n" + "-" * 70 + "\n") |
|
|
|
|
| if __name__ == "__main__": |
| parser = argparse.ArgumentParser(description="Query the RAG system") |
| parser.add_argument("--verbose", "-v", action="store_true", |
| help="Show retrieved chunks for debugging") |
| |
| args = parser.parse_args() |
| main(verbose=args.verbose) |