Repository navigation
Expand file tree
/
Copy pathtest_api.py
More file actions
214 lines (188 loc) · 7.65 KB
/
Copy pathtest_api.py
File metadata and controls
214 lines (188 loc) · 7.65 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
import requests
import time
import json
BASE_URL = "http://127.0.0.1:5000"
BASE_MODEL_URL = "https://api.openai.com"
API_KEY = ""
MODEL_NAME = "gpt-4o-mini"
REQUEST_MODE = "chat"
PROMPT_TEMPLATE = "请根据以下对话内容进行评估,输出 JSON,字段包含 total_score, category, reasons。"
test_cases = [
{
"name": "积极会话",
"category": "积极",
"data": {
"messages": [
"这个模型太棒了,回答得非常准确!",
"我要分享给我的朋友们!",
"感谢开发者的辛苦工作,太赞了"
]
},
"expected_category": "积极",
"expected_score_min": 10
},
{
"name": "中性会话",
"category": "中性",
"data": {
"messages": [
"你好",
"我想了解一下这个功能",
"好的,明白了",
"谢谢"
]
},
"expected_category": "中性",
"expected_score_range": (-9, 9)
},
{
"name": "消极会话",
"category": "消极",
"data": {
"messages": [
"这个回答太差了",
"完全不对",
"糟糕透了,浪费时间",
"一点都不满意"
]
},
"expected_category": "消极",
"expected_score_max": -10
},
{
"name": "包含显式行为测试",
"category": "积极",
"data": {
"messages": [
"这个内容很有用,我要下载它",
"然后复制给其他人分享"
]
},
"expected_behaviors": ["download", "copy", "share"]
}
]
def run_tests():
results = []
for test in test_cases:
print(f"\n{'='*60}")
print(f"测试用例: {test['name']}")
print(f"{'='*60}")
start_time = time.time()
try:
response = requests.post(
f"{BASE_URL}/api/evaluate",
json={
"base_url": BASE_MODEL_URL,
"api_key": API_KEY,
"model": MODEL_NAME,
"request_mode": REQUEST_MODE,
"prompt_template": PROMPT_TEMPLATE,
"session_data": test['data']
},
headers={"Content-Type": "application/json"}
)
end_time = time.time()
response_time = (end_time - start_time) * 1000
print(f"响应时间: {response_time:.2f} 毫秒")
if response.status_code == 200:
result = response.json()
print(f"响应状态: 成功 (200)")
print(f"总分: {result.get('total_score')}")
print(f"分类: {result.get('category')}")
print(f"显式行为: {result.get('explicit_behaviors')}")
passed = True
issues = []
if 'expected_category' in test:
if result.get('category') != test['expected_category']:
passed = False
issues.append(f"分类错误: 期望 {test['expected_category']}, 实际 {result.get('category')}")
if 'expected_score_min' in test:
if result.get('total_score', 0) < test['expected_score_min']:
passed = False
issues.append(f"分数过低: 期望至少 {test['expected_score_min']}, 实际 {result.get('total_score')}")
if 'expected_score_max' in test:
if result.get('total_score', 0) > test['expected_score_max']:
passed = False
issues.append(f"分数过高: 期望最多 {test['expected_score_max']}, 实际 {result.get('total_score')}")
if 'expected_score_range' in test:
score = result.get('total_score', 0)
min_score, max_score = test['expected_score_range']
if score < min_score or score > max_score:
passed = False
issues.append(f"分数不在预期范围内: 期望 {min_score}-{max_score}, 实际 {score}")
if 'expected_behaviors' in test:
behaviors = result.get('explicit_behaviors', [])
for expected in test['expected_behaviors']:
if expected not in behaviors:
passed = False
issues.append(f"未检测到预期行为: {expected}")
if passed:
print("OK 测试通过")
else:
print("FAIL 测试失败:")
for issue in issues:
print(f" - {issue}")
results.append({
"name": test['name'],
"category": test['category'],
"passed": passed,
"response_time": response_time,
"issues": issues,
"result": result
})
else:
print(f"响应状态: 失败 ({response.status_code})")
print(f"错误: {response.text}")
results.append({
"name": test['name'],
"category": test['category'],
"passed": False,
"response_time": response_time,
"issues": [f"HTTP错误: {response.status_code}"],
"error": response.text
})
except Exception as e:
end_time = time.time()
response_time = (end_time - start_time) * 1000
print(f"请求失败: {str(e)}")
results.append({
"name": test['name'],
"category": test['category'],
"passed": False,
"response_time": response_time,
"issues": [f"异常: {str(e)}"]
})
return results
def print_summary(results):
print(f"\n{'='*60}")
print("测试总结")
print(f"{'='*60}")
total = len(results)
passed = sum(1 for r in results if r['passed'])
failed = total - passed
print(f"总测试数: {total}")
print(f"通过: {passed}")
print(f"失败: {failed}")
avg_response_time = sum(r['response_time'] for r in results) / total
print(f"平均响应时间: {avg_response_time:.2f} 毫秒")
all_under_1s = all(r['response_time'] < 1000 for r in results)
print(f"所有响应时间 < 1秒: {'OK' if all_under_1s else 'FAIL'}")
for r in results:
status = "OK" if r['passed'] else "FAIL"
print(f"{status} {r['name']} - {r['response_time']:.2f}ms")
return {
"total": total,
"passed": passed,
"failed": failed,
"avg_response_time": avg_response_time,
"all_under_1s": all_under_1s,
"results": results
}
if __name__ == "__main__":
print("开始API集成测试")
print("="*60)
results = run_tests()
summary = print_summary(results)
with open('test_report.json', 'w', encoding='utf-8') as f:
json.dump(summary, f, ensure_ascii=False, indent=2)
print(f"\n测试报告已保存到 test_report.json")