Download scripts/query_cli.py from HokieBird/445-bot: direct link, hf CLI and curl.
- Browser
- Download file 2.54 kB
-
https://huggingface.co/spaces/HokieBird/445-bot/resolve/main/scripts/query_cli.py
- Command line
-
hf download hf://spaces/HokieBird/445-bot/scripts/query_cli.py
-
curl -L -o query_cli.py https://huggingface.co/spaces/HokieBird/445-bot/resolve/main/scripts/query_cli.py
2.54 kB
| #!/usr/bin/env python3 | |
| """ | |
| 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: | |
| # Get user question | |
| question = input("Question: ").strip() | |
| # Check for quit command | |
| if question.lower() in ['quit', 'exit', 'q']: | |
| print("Goodbye!") | |
| break | |
| if not question: | |
| continue | |
| # Retrieve relevant chunks | |
| 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}") | |
| # Build prompt with context | |
| print("\n🧠 Building prompt...") | |
| prompt = build_prompt(question, chunks) | |
| # Generate response | |
| print("Generating response (this may take 15-20 seconds)...") | |
| response = generate(prompt) | |
| # Print response | |
| 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) |