#!/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)