-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_system.py
More file actions
executable file
Β·165 lines (128 loc) Β· 4.8 KB
/
test_system.py
File metadata and controls
executable file
Β·165 lines (128 loc) Β· 4.8 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/usr/bin/env python3
"""
Simple test script for the ETI RAG system.
Tests the ingestion and API functionality.
"""
import os
import sys
import time
import requests
import subprocess
from pathlib import Path
# Configuration
API_BASE_URL = "http://localhost:8080"
HR_MANUAL_PATH = "data/HR_Manual.pdf"
TEST_QUERIES = [
"What is the vacation policy?",
"How do I request time off?",
"What are the working hours?",
"What benefits are offered?",
]
def check_requirements():
"""Check if all requirements are met."""
print("π Checking requirements...")
# Check OpenAI API key
if not os.getenv("OPENAI_API_KEY"):
print("β OPENAI_API_KEY environment variable not set")
return False
# Check if HR manual exists
if not Path(HR_MANUAL_PATH).exists():
print(f"β HR manual not found at: {HR_MANUAL_PATH}")
return False
print("β
Requirements check passed")
return True
def test_ingestion():
"""Test the data ingestion process."""
print("\nπ Testing data ingestion...")
try:
result = subprocess.run([
sys.executable, "scripts/ingest.py",
"--pdf", HR_MANUAL_PATH,
"--output-dir", "data/index"
], capture_output=True, text=True, check=True)
print("β
Ingestion completed successfully")
print(f"Output: {result.stdout.split('Ingestion completed')[0]}...")
return True
except subprocess.CalledProcessError as e:
print(f"β Ingestion failed: {e.stderr}")
return False
def wait_for_api(timeout=30):
"""Wait for API to be ready."""
print(f"\nβ³ Waiting for API to be ready (timeout: {timeout}s)...")
start_time = time.time()
while time.time() - start_time < timeout:
try:
response = requests.get(f"{API_BASE_URL}/healthz", timeout=5)
if response.status_code == 200:
data = response.json()
if data.get("ok", False):
print("β
API is ready and indexes are loaded")
return True
else:
print("β³ API running but indexes not loaded yet...")
else:
print(f"β³ API returned status: {response.status_code}")
except requests.exceptions.RequestException:
print("β³ API not ready yet...")
time.sleep(2)
print("β API not ready within timeout")
return False
def test_queries():
"""Test the API with sample queries."""
print("\n㪠Testing queries...")
for i, query in enumerate(TEST_QUERIES, 1):
print(f"\nπ Query {i}: {query}")
try:
response = requests.post(
f"{API_BASE_URL}/ask",
json={"query": query, "max_tokens": 600},
timeout=30
)
if response.status_code == 200:
data = response.json()
print(f"β
Response received ({data.get('latency_ms', 0)}ms)")
print(f"π Retrieved {len(data.get('retrieved_ids', []))} chunks")
print(f"π {len(data.get('citations', []))} citations")
# Show first part of answer
answer = data.get('answer', '')
if answer:
preview = answer[:100] + "..." if len(answer) > 100 else answer
print(f"π‘ Answer: {preview}")
else:
print(f"β Query failed: {response.status_code} - {response.text}")
except requests.exceptions.RequestException as e:
print(f"β Query failed: {e}")
def test_full_system():
"""Run complete system test."""
print("π ETI RAG System Test")
print("=" * 50)
# Check requirements
if not check_requirements():
return False
# Test ingestion
if not test_ingestion():
return False
# Check if we need to start API manually
if not wait_for_api(timeout=5):
print("\nβ οΈ API not running. Please start it manually:")
print(" python -m uvicorn app.main:app --host 0.0.0.0 --port 8080")
print("\nThen run this script again to test queries.")
return False
# Test queries
test_queries()
print("\nπ System test completed!")
return True
def main():
"""Main test function."""
if len(sys.argv) > 1:
if sys.argv[1] == "--ingest-only":
if check_requirements():
return test_ingestion()
elif sys.argv[1] == "--queries-only":
if wait_for_api():
test_queries()
return True
return test_full_system()
if __name__ == "__main__":
success = main()
sys.exit(0 if success else 1)