-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_structured.py
More file actions
159 lines (125 loc) · 5.72 KB
/
test_structured.py
File metadata and controls
159 lines (125 loc) · 5.72 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
"""
Test script for GhostTruth structured Q&A endpoint.
Tests the new /api/chat/structured endpoint with JSON validation and source attribution.
"""
import requests
import json
from pathlib import Path
# Configuration
BASE_URL = "http://localhost:3000"
STRUCTURED_ENDPOINT = f"{BASE_URL}/api/chat/structured"
LEGACY_ENDPOINT = f"{BASE_URL}/api/chat/"
def load_test_data(file_path: str):
"""Load test questions from JSON file"""
with open(file_path, 'r', encoding='utf-8') as f:
return json.load(f)
def test_structured_endpoint():
"""Test the new structured endpoint with source attribution"""
print("=" * 80)
print("TESTING STRUCTURED ENDPOINT: /api/chat/structured")
print("=" * 80)
# Load test data
test_file = Path("test/algerie_telecom_questions.json")
if not test_file.exists():
print(f"❌ Test file not found: {test_file}")
return
payload = load_test_data(test_file)
payload["include_sources"] = True
print(f"\n📤 Sending request with {len(payload['question'])} categories...")
print(f"Request payload preview:")
print(json.dumps(payload, indent=2, ensure_ascii=False)[:500])
print("...\n")
try:
response = requests.post(STRUCTURED_ENDPOINT, json=payload)
response.raise_for_status()
result = response.json()
print("✅ Response received successfully!")
print("\n" + "=" * 80)
print("RESULTS")
print("=" * 80)
# Display results
for category_id, questions in result.get("reponses", {}).items():
print(f"\n📁 Category: {category_id}")
print("-" * 80)
for question_id, answer in questions.items():
print(f"\n 🔹 {question_id}:")
print(f" Question: {payload['question'][category_id][question_id]}")
print(f" Answer: {answer[:200]}..." if len(answer) > 200 else f" Answer: {answer}")
# Display sources if available
if result.get("sources") and category_id in result["sources"]:
sources = result["sources"][category_id].get(question_id, [])
if sources:
print(f"\n 📚 Sources ({len(sources)}):")
for idx, source in enumerate(sources, 1):
print(f" {idx}. File: {source.get('file', 'N/A')}")
print(f" Page: {source.get('page', 'N/A')}")
print(f" Evidence: {source.get('evidence', 'N/A')[:150]}...")
# Save full response
output_file = Path("test/structured_response.json")
with open(output_file, 'w', encoding='utf-8') as f:
json.dump(result, f, indent=2, ensure_ascii=False)
print(f"\n\n💾 Full response saved to: {output_file}")
return result
except requests.exceptions.RequestException as e:
print(f"❌ Request failed: {e}")
if hasattr(e, 'response') and e.response is not None:
print(f"Response status: {e.response.status_code}")
print(f"Response body: {e.response.text}")
return None
def test_legacy_endpoint():
"""Test the legacy endpoint for comparison"""
print("\n\n" + "=" * 80)
print("TESTING LEGACY ENDPOINT: /api/chat/")
print("=" * 80)
test_file = Path("test/algerie_telecom_questions.json")
payload = load_test_data(test_file)
payload["include_sources"] = True
print(f"\n📤 Sending request to legacy endpoint...")
try:
response = requests.post(LEGACY_ENDPOINT, json=payload)
response.raise_for_status()
result = response.json()
print("✅ Legacy endpoint response received!")
# Save response
output_file = Path("test/legacy_response.json")
with open(output_file, 'w', encoding='utf-8') as f:
json.dump(result, f, indent=2, ensure_ascii=False)
print(f"💾 Response saved to: {output_file}")
return result
except requests.exceptions.RequestException as e:
print(f"❌ Request failed: {e}")
return None
def compare_endpoints():
"""Compare structured vs legacy endpoints"""
print("\n\n" + "=" * 80)
print("COMPARISON: STRUCTURED vs LEGACY")
print("=" * 80)
structured = test_structured_endpoint()
legacy = test_legacy_endpoint()
if structured and legacy:
print("\n📊 Comparison Summary:")
print("-" * 80)
print(f"Structured endpoint:")
print(f" - JSON validation: ✅ Enforced")
print(f" - Source format: SourceEvidence (file, page, evidence)")
print(f" - Temperature: 0.3 (more deterministic)")
print(f" - Top-K retrieval: 10")
print(f"\nLegacy endpoint:")
print(f" - JSON validation: ❌ Not enforced")
print(f" - Source format: SourceInfo (score, file, chunk)")
print(f" - Temperature: 0.7 (default)")
print(f" - Top-K retrieval: 5")
if __name__ == "__main__":
print("🚀 GhostTruth Structured Q&A System Test")
print(f"Ensure the server is running at {BASE_URL}\n")
choice = input("Choose test mode:\n 1. Structured endpoint only\n 2. Legacy endpoint only\n 3. Compare both\n\nEnter choice (1-3): ").strip()
if choice == "1":
test_structured_endpoint()
elif choice == "2":
test_legacy_endpoint()
elif choice == "3":
compare_endpoints()
else:
print("Invalid choice. Running structured endpoint test by default.")
test_structured_endpoint()
print("\n✨ Test completed!")