File size: 1,697 Bytes
4dc3b79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
Script to query the RAG API.
"""
import httpx
import argparse
import sys
from pathlib import Path

# Add the app directory to the path to import settings
sys.path.append(str(Path(__file__).parent.parent))
from app.core.config import settings

def query_api(query_text: str):
    """Sends a query to the running FastAPI application."""
    api_url = f"http://localhost:{settings.port}/api/v1/query"
    
    print(f"▶️  Sending query to: {api_url}")
    print(f"❓ Query: \"{query_text}\"")
    
    try:
        with httpx.Client(timeout=60.0) as client:
            response = client.post(api_url, json={"query": query_text})
            response.raise_for_status()  # Raise an exception for bad status codes (4xx or 5xx)
            
            data = response.json()
            
            print("\n" + "="*20 + " API Response " + "="*20)
            print(f"💡 Answer: {data.get('answer', 'No answer found.')}")
            print("\n📚 Source Documents:")
            
            for i, doc in enumerate(data.get('source_documents', [])):
                print(f"  [{i+1}] Source: {doc['metadata'].get('source', 'N/A')}")
                print(f"      Content: \"{doc['page_content'][:150]}...\"")
            print("="*54)

    except httpx.RequestError as e:
        print(f"\n❌ Error connecting to the API: {e}")
        print("   Please ensure the FastAPI server is running (`python app/main.py`).")

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Query the RAG API.")
    parser.add_argument("query", type=str, help="The question to ask the RAG system.")
    args = parser.parse_args()
    
    query_api(args.query)