File size: 2,540 Bytes
9b62bba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/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)