-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo.py
More file actions
executable file
·292 lines (261 loc) · 9.75 KB
/
demo.py
File metadata and controls
executable file
·292 lines (261 loc) · 9.75 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#!/usr/bin/env python3
"""
Demo script for KnowledgeOps AI API
"""
import requests
import json
import time
from typing import Dict, Any
BASE_URL = "http://localhost:8000"
def test_health():
"""Test health endpoint"""
print("🔍 Testing Health Check...")
try:
response = requests.get(f"{BASE_URL}/health")
if response.status_code == 200:
data = response.json()
print(f"✅ Health: {data['status']}")
print(f" Version: {data['version']}")
print(f" Timestamp: {data['timestamp']}")
return True
else:
print(f"❌ Health check failed: {response.status_code}")
return False
except Exception as e:
print(f"❌ Health check error: {e}")
return False
def test_metrics():
"""Test metrics endpoint"""
print("\n📊 Testing Metrics...")
try:
response = requests.get(f"{BASE_URL}/metrics")
if response.status_code == 200:
print("✅ Metrics endpoint accessible")
# Print first few lines of metrics
lines = response.text.split('\n')[:10]
for line in lines:
if line.strip():
print(f" {line}")
return True
else:
print(f"❌ Metrics failed: {response.status_code}")
return False
except Exception as e:
print(f"❌ Metrics error: {e}")
return False
def test_ingest():
"""Test document ingestion"""
print("\n📄 Testing Document Ingestion...")
try:
payload = {
"file_url": "https://example.com/sample.pdf",
"metadata": {
"title": "Sample Document",
"author": "Demo User",
"category": "test"
},
"chunk_size": 800,
"overlap": 100
}
response = requests.post(f"{BASE_URL}/ingest", json=payload)
if response.status_code == 200:
data = response.json()
print(f"✅ Ingestion job created")
print(f" Job ID: {data['job_id']}")
print(f" Status: {data['status']}")
print(f" Message: {data['message']}")
return data['job_id']
else:
print(f"❌ Ingestion failed: {response.status_code}")
print(f" Response: {response.text}")
return None
except Exception as e:
print(f"❌ Ingestion error: {e}")
return None
def test_query():
"""Test document querying"""
print("\n❓ Testing Document Query...")
try:
payload = {
"query": "What is the main topic of the document?",
"top_k": 5,
"include_sources": True
}
response = requests.post(f"{BASE_URL}/query", json=payload)
if response.status_code == 200:
data = response.json()
print(f"✅ Query processed successfully")
print(f" Query ID: {data['query_id']}")
print(f" Answer: {data['answer']}")
print(f" Confidence: {data['confidence']}")
print(f" Processing Time: {data['processing_time']:.3f}s")
print(f" Sources: {len(data['sources'])} found")
return True
else:
print(f"❌ Query failed: {response.status_code}")
print(f" Response: {response.text}")
return False
except Exception as e:
print(f"❌ Query error: {e}")
return False
def test_retrieve():
"""Test document retrieval"""
print("\n🔍 Testing Document Retrieval...")
try:
payload = {
"query": "What is the main topic of the document?",
"top_k": 5,
"org_id": "default",
"use_bm25": True,
"max_tokens": 4000
}
response = requests.post(f"{BASE_URL}/retrieve", json=payload)
if response.status_code == 200:
data = response.json()
print(f"✅ Retrieval processed successfully")
print(f" Query: {data['query']}")
print(f" Total Results: {data['total_results']}")
print(f" Top Score: {data['top_score']:.3f}")
print(f" Results: {len(data['results'])} chunks found")
return True
else:
print(f"❌ Retrieval failed: {response.status_code}")
print(f" Response: {response.text}")
return False
except Exception as e:
print(f"❌ Retrieval error: {e}")
return False
def test_intelligent_query():
"""Test intelligent query with reformulation"""
print("\n🧠 Testing Intelligent Query...")
try:
payload = {
"query": "What is the main topic of the document?",
"top_k": 5,
"confidence_threshold": 0.7,
"max_attempts": 2,
"org_id": "default"
}
response = requests.post(f"{BASE_URL}/query/intelligent", json=payload)
if response.status_code == 200:
data = response.json()
print(f"✅ Intelligent query processed successfully")
print(f" Query: {data['query']}")
print(f" Final Answer: {data['final_answer'][:100]}...")
print(f" Final Confidence: {data['final_confidence']:.3f}")
print(f" Total Attempts: {data['total_attempts']}")
print(f" Reformulation Used: {data['reformulation_used']}")
print(f" Processing Time: {data['processing_time']:.3f}s")
if data.get('attempts'):
print(f" Attempt Details:")
for attempt in data['attempts']:
print(f" Attempt {attempt['attempt']}: {attempt['confidence']:.3f} confidence")
return True
else:
print(f"❌ Intelligent query failed: {response.status_code}")
print(f" Response: {response.text}")
return False
except Exception as e:
print(f"❌ Intelligent query error: {e}")
return False
def test_metrics():
"""Test metrics endpoint"""
print("\n📊 Testing Metrics Endpoint...")
try:
response = requests.get(f"{BASE_URL}/metrics")
if response.status_code == 200:
metrics_data = response.text
print(f"✅ Metrics endpoint working")
print(f" Content-Type: {response.headers.get('content-type', 'unknown')}")
print(f" Data Size: {len(metrics_data)} characters")
# Check for some expected metrics
expected_metrics = [
"http_requests_total",
"query_requests_total",
"query_tokens_in_total",
"query_tokens_out_total"
]
found_metrics = 0
for metric in expected_metrics:
if metric in metrics_data:
print(f" ✅ Found metric: {metric}")
found_metrics += 1
print(f" 📈 Found {found_metrics}/{len(expected_metrics)} expected metrics")
return True
else:
print(f"❌ Metrics endpoint failed: {response.status_code}")
return False
except Exception as e:
print(f"❌ Metrics error: {e}")
return False
def test_root():
"""Test root endpoint"""
print("\n🏠 Testing Root Endpoint...")
try:
response = requests.get(f"{BASE_URL}/")
if response.status_code == 200:
data = response.json()
print(f"✅ Root endpoint accessible")
print(f" Message: {data['message']}")
print(f" Version: {data['version']}")
print(f" Docs: {data['docs']}")
return True
else:
print(f"❌ Root failed: {response.status_code}")
return False
except Exception as e:
print(f"❌ Root error: {e}")
return False
def main():
"""Run all demo tests"""
print("🚀 KnowledgeOps AI Demo")
print("=" * 50)
print(f"📍 Testing API at: {BASE_URL}")
print("=" * 50)
# Check if server is running
try:
requests.get(f"{BASE_URL}/health", timeout=5)
except requests.exceptions.ConnectionError:
print("❌ Cannot connect to KnowledgeOps AI server")
print(" Make sure the server is running:")
print(" python run.py")
return
except Exception as e:
print(f"❌ Connection error: {e}")
return
# Run tests
tests = [
("Health Check", test_health),
("Metrics", test_metrics),
("Root Endpoint", test_root),
("Document Ingestion", test_ingest),
("Document Query", test_query),
("Document Retrieval", test_retrieve),
("Intelligent Query", test_intelligent_query),
("Prometheus Metrics", test_metrics)
]
results = {}
for test_name, test_func in tests:
print(f"\n{'='*20} {test_name} {'='*20}")
try:
result = test_func()
results[test_name] = result
except Exception as e:
print(f"❌ {test_name} failed with exception: {e}")
results[test_name] = False
# Summary
print("\n" + "=" * 50)
print("📊 Demo Results Summary")
print("=" * 50)
passed = sum(1 for result in results.values() if result)
total = len(results)
for test_name, result in results.items():
status = "✅ PASS" if result else "❌ FAIL"
print(f"{status} {test_name}")
print(f"\n🎯 Overall: {passed}/{total} tests passed")
if passed == total:
print("🎉 All tests passed! KnowledgeOps AI is working correctly.")
else:
print("⚠️ Some tests failed. Check the server logs for details.")
if __name__ == "__main__":
main()