Spaces:
Sleeping
Sleeping
File size: 1,835 Bytes
1c29d49 | 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 | import requests
import time
BASE_URL = "http://127.0.0.1:8000"
def test_api():
print("--- Testing RAG Chatbot API ---")
# 1. Health Check
try:
response = requests.get(f"{BASE_URL}/health")
if response.status_code == 200:
print("[OK] Health Check Passed!")
else:
print(f"[FAIL] Health Check Failed: {response.text}")
return
except requests.exceptions.ConnectionError:
print("[FAIL] Could not connect to server. Is it running?")
return
# 2. Reload Documents (Indexing) - SKIPPING TO AVOID RE-TRIGGERING
print("\nSkipping Indexing for this test run...")
# try:
# response = requests.post(f"{BASE_URL}/reload-documents")
# if response.status_code == 200:
# print(f"[OK] Indexing Response: {response.json()}")
# else:
# print(f"[FAIL] Indexing Failed: {response.text}")
# except Exception as e:
# print(f"[FAIL] Error during indexing: {e}")
# 3. Ask a Question
print("\nAsking: 'What is Physical AI?'...")
payload = {
"query": "What is Physical AI?",
"translate_urdu": False
}
try:
start_time = time.time()
response = requests.post(f"{BASE_URL}/ask", json=payload)
duration = time.time() - start_time
if response.status_code == 200:
data = response.json()
print(f"[OK] Answer ({duration:.2f}s):")
print(f"Answer: {data['answer']}")
print(f"Sources: {data['chapter']} / {data['section']}")
else:
print(f"[FAIL] Ask Failed: {response.text}")
except Exception as e:
print(f"[FAIL] Error during asking: {e}")
if __name__ == "__main__":
test_api()
|