julienserbanescu-rag / test_api.py
DaJulster's picture
agentic search and retrieval added
11c6045
Raw
History Blame
2.47 kB
"""
Quick script to test the API locally before deploying to Hugging Face
Run this after starting the Flask app with: python app_hf.py
"""
import requests
import json
API_URL = "http://localhost:7860/api/query"
def test_query(query_text):
"""Test a query against the local API"""
print(f"\n{'='*60}")
print(f"Testing query: {query_text}")
print(f"{'='*60}\n")
try:
response = requests.post(
API_URL,
json={"query": query_text},
headers={"Content-Type": "application/json"},
timeout=120 # Allow up to 2 minutes for agentic responses
)
if response.status_code == 200:
data = response.json()
print("βœ… SUCCESS!")
print(f"\nResponse:\n{data.get('response', 'No response')}")
print(f"\nSources ({len(data.get('sources', []))} total):")
for i, source in enumerate(data.get('sources', [])[:5], 1): # Show first 5
print(f"\n Source {i}:")
print(f" Score: {source.get('score', 'N/A')}")
metadata = source.get('metadata', {})
print(f" Type: {metadata.get('type', 'Unknown')}")
print(f" Source: {metadata.get('source', 'Unknown')}")
if 'urls' in metadata and metadata['urls']:
print(f" URLs: {', '.join(metadata['urls'][:2])}") # Show first 2 URLs
else:
print(f"❌ ERROR: Status code {response.status_code}")
print(f"Response: {response.text}")
except requests.exceptions.Timeout:
print("❌ ERROR: Request timed out (took longer than 120 seconds)")
except requests.exceptions.ConnectionError:
print("❌ ERROR: Could not connect to API. Make sure app_hf.py is running!")
print(" Start it with: python app_hf.py")
except Exception as e:
print(f"❌ ERROR: {str(e)}")
if __name__ == "__main__":
print("πŸ§ͺ Testing RAG API locally")
print("Make sure app_hf.py is running in another terminal!")
print("Start it with: python app_hf.py\n")
# Test queries
test_queries = [
"What projects has Julien worked on?",
"Tell me about Julien's research",
"What is Julien's background?"
]
for query in test_queries:
test_query(query)
input("\nPress Enter to test next query...")
print("\nβœ… All tests complete!")