-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb_app.py
More file actions
513 lines (400 loc) · 16 KB
/
Copy pathweb_app.py
File metadata and controls
513 lines (400 loc) · 16 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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
# -*- coding: utf-8 -*-
"""
Flask Web 应用
提供 Web 界面用于测试 AI Agent
"""
import os
import json
import time
import uuid
from datetime import datetime
from flask import Flask, render_template, jsonify, request, Response, stream_with_context
from core.agent_client import AgentClient
from core.evaluator import Evaluator
from history import HistoryManager
app = Flask(__name__)
client = AgentClient()
evaluator = Evaluator()
history_mgr = HistoryManager()
TESTCASES_DIR = "testcases"
REPORTS_DIR = "reports"
API_CONFIG_DIR = "config"
USE_LLM_EVAL = False
LLM_API_URL = "http://localhost:8000"
def load_test_cases():
"""加载测试用例"""
json_file = os.path.join(TESTCASES_DIR, "testcases.json")
if not os.path.exists(json_file):
return []
with open(json_file, "r", encoding="utf-8") as f:
data = json.load(f)
all_cases = []
for category, cases in data.items():
for case in cases:
keywords_str = "|".join(case.get("expect_keywords", []))
all_cases.append({
"id": case.get("id", ""),
"question": case.get("question", ""),
"expect_intent": case.get("expect_intent", ""),
"expect_keywords": keywords_str,
"category": category
})
return all_cases
def save_test_cases(data):
"""保存测试用例"""
json_file = os.path.join(TESTCASES_DIR, "testcases.json")
with open(json_file, "w", encoding="utf-8") as f:
json.dump(data, f, ensure_ascii=False, indent=2)
@app.route("/")
def index():
"""主页"""
return render_template("index.html")
@app.route("/api/testcases")
def get_testcases():
"""获取测试用例列表(支持分页)"""
page = request.args.get("page", 1, type=int)
size = request.args.get("size", 10, type=int)
category = request.args.get("category", "")
testcases = load_test_cases()
# 过滤分类
if category:
testcases = [c for c in testcases if c.get("category") == category]
total = len(testcases)
start = (page - 1) * size
end = start + size
return jsonify({
"cases": testcases[start:end],
"total": total,
"page": page,
"size": size
})
@app.route("/api/testcases", methods=["POST"])
def add_testcase():
"""添加测试用例"""
data = request.json
json_file = os.path.join(TESTCASES_DIR, "testcases.json")
if not os.path.exists(json_file):
return jsonify({"success": False, "error": "测试用例文件不存在"}), 400
with open(json_file, "r", encoding="utf-8") as f:
testcases = json.load(f)
category = data.get("category", "default")
if category not in testcases:
testcases[category] = []
# 生成全局唯一 ID (UUID 前 8 位)
new_id = str(uuid.uuid4())[:8]
new_case = {
"id": new_id,
"question": data.get("question", ""),
"expect_intent": data.get("expect_intent", ""),
"expect_keywords": data.get("expect_keywords", [])
}
testcases[category].append(new_case)
save_test_cases(testcases)
return jsonify({"success": True, "case": new_case, "category": category})
@app.route("/api/testcases/<category>/<case_id>", methods=["PUT"])
def update_testcase(category, case_id):
"""更新测试用例"""
data = request.json
json_file = os.path.join(TESTCASES_DIR, "testcases.json")
if not os.path.exists(json_file):
return jsonify({"success": False, "error": "测试用例文件不存在"}), 400
with open(json_file, "r", encoding="utf-8") as f:
testcases = json.load(f)
if category not in testcases:
return jsonify({"success": False, "error": "分类不存在"}), 404
for case in testcases[category]:
if str(case.get("id")) == str(case_id):
case["question"] = data.get("question", case.get("question", ""))
case["expect_intent"] = data.get("expect_intent", case.get("expect_intent", ""))
case["expect_keywords"] = data.get("expect_keywords", case.get("expect_keywords", []))
save_test_cases(testcases)
return jsonify({"success": True, "case": case})
return jsonify({"success": False, "error": "测试用例不存在"}), 404
@app.route("/api/testcases/<category>/<case_id>", methods=["DELETE"])
def delete_testcase(category, case_id):
"""删除测试用例"""
json_file = os.path.join(TESTCASES_DIR, "testcases.json")
if not os.path.exists(json_file):
return jsonify({"success": False, "error": "测试用例文件不存在"}), 400
with open(json_file, "r", encoding="utf-8") as f:
testcases = json.load(f)
if category not in testcases:
return jsonify({"success": False, "error": "分类不存在"}), 404
testcases[category] = [c for c in testcases[category] if str(c.get("id")) != str(case_id)]
save_test_cases(testcases)
return jsonify({"success": True})
@app.route("/api/categories")
def get_categories():
"""获取测试用例分类"""
json_file = os.path.join(TESTCASES_DIR, "testcases.json")
if not os.path.exists(json_file):
return jsonify(["default"])
with open(json_file, "r", encoding="utf-8") as f:
testcases = json.load(f)
return jsonify(list(testcases.keys()))
@app.route("/api/config", methods=["GET"])
def get_config():
"""获取当前配置"""
return jsonify({
"use_llm_eval": USE_LLM_EVAL,
"llm_api_url": LLM_API_URL
})
@app.route("/api/config", methods=["POST"])
def set_config():
"""设置评测配置"""
global USE_LLM_EVAL, LLM_API_URL
data = request.json
USE_LLM_EVAL = data.get("use_llm", False)
LLM_API_URL = data.get("llm_api_url", "http://localhost:8000")
evaluator.set_llm_mode(USE_LLM_EVAL, LLM_API_URL)
return jsonify({"success": True})
@app.route("/api/api-configs", methods=["GET"])
def get_api_configs():
"""获取API配置列表"""
config_file = os.path.join(API_CONFIG_DIR, "api_configs.json")
if not os.path.exists(config_file):
return jsonify([])
with open(config_file, "r", encoding="utf-8") as f:
configs = json.load(f)
return jsonify(configs)
@app.route("/api/api-configs", methods=["POST"])
def add_api_config():
"""添加API配置"""
config_file = os.path.join(API_CONFIG_DIR, "api_configs.json")
data = request.json
if not os.path.exists(config_file):
configs = []
else:
with open(config_file, "r", encoding="utf-8") as f:
configs = json.load(f)
new_config = {
"id": str(uuid.uuid4())[:8],
"name": data.get("name", "新API"),
"url": data.get("url", "http://localhost:8000"),
"description": data.get("description", "")
}
configs.append(new_config)
with open(config_file, "w", encoding="utf-8") as f:
json.dump(configs, f, ensure_ascii=False, indent=2)
return jsonify(new_config)
@app.route("/api/api-configs/<config_id>", methods=["PUT"])
def update_api_config(config_id):
"""更新API配置"""
config_file = os.path.join(API_CONFIG_DIR, "api_configs.json")
data = request.json
if not os.path.exists(config_file):
return jsonify({"error": "配置文件不存在"}), 404
with open(config_file, "r", encoding="utf-8") as f:
configs = json.load(f)
for config in configs:
if config["id"] == config_id:
config["name"] = data.get("name", config["name"])
config["url"] = data.get("url", config["url"])
config["description"] = data.get("description", config.get("description", ""))
break
else:
return jsonify({"error": "配置不存在"}), 404
with open(config_file, "w", encoding="utf-8") as f:
json.dump(configs, f, ensure_ascii=False, indent=2)
return jsonify({"success": True})
@app.route("/api/api-configs/<config_id>", methods=["DELETE"])
def delete_api_config(config_id):
"""删除API配置"""
config_file = os.path.join(API_CONFIG_DIR, "api_configs.json")
if not os.path.exists(config_file):
return jsonify({"error": "配置文件不存在"}), 404
with open(config_file, "r", encoding="utf-8") as f:
configs = json.load(f)
configs = [c for c in configs if c["id"] != config_id]
with open(config_file, "w", encoding="utf-8") as f:
json.dump(configs, f, ensure_ascii=False, indent=2)
return jsonify({"success": True})
@app.route("/api/run/stream", methods=["POST"])
def run_tests_stream():
"""运行测试(实时进度)"""
global USE_LLM_EVAL, LLM_API_URL
data = request.json
api_url = data.get("api_url", "http://localhost:8000")
use_llm = data.get("use_llm", USE_LLM_EVAL)
llm_api_url = data.get("llm_api_url", LLM_API_URL)
case_ids = data.get("case_ids", [])
client.base_url = api_url
client.chat_endpoint = f"{api_url}/chat"
if use_llm:
test_evaluator = Evaluator(api_url=llm_api_url, use_llm=True)
else:
test_evaluator = Evaluator()
test_cases = load_test_cases()
if case_ids:
test_cases = [c for c in test_cases if str(c["id"]) in [str(cid) for cid in case_ids]]
results = []
start_time = time.time()
def generate():
start_data = json.dumps({"total": len(test_cases)})
yield f"event: start\ndata: {start_data}\n\n"
for i, case in enumerate(test_cases):
question = case["question"]
expect_keywords = case["expect_keywords"]
expect_intent = case.get("expect_intent", "")
try:
answer = client.send_message(question)
if use_llm:
llm_result = test_evaluator.evaluate_with_llm(
question, answer, expect_intent, expect_keywords
)
result = {
"id": case["id"],
"category": case["category"],
"question": question,
"answer": answer,
"expect_keywords": expect_keywords,
"expect_intent": expect_intent,
"is_pass": llm_result.get("final_pass", False),
"matched_keyword": None,
"llm_evaluation": llm_result
}
else:
is_pass, matched_keyword = test_evaluator.evaluate(answer, expect_keywords)
result = {
"id": case["id"],
"category": case["category"],
"question": question,
"answer": answer,
"expect_keywords": expect_keywords,
"is_pass": is_pass,
"matched_keyword": matched_keyword
}
except Exception as e:
result = {
"id": case["id"],
"category": case["category"],
"question": question,
"answer": "",
"expect_keywords": expect_keywords,
"is_pass": False,
"matched_keyword": None,
"error": str(e)
}
results.append(result)
progress = {
"current": i + 1,
"total": len(test_cases),
"result": result
}
yield f"event: progress\ndata: {json.dumps(progress)}\n\n"
duration = time.time() - start_time
history_mgr.save_run(results, use_llm, api_url, duration)
summary = {
"total": len(results),
"passed": sum(1 for r in results if r.get("is_pass", False)),
"failed": sum(1 for r in results if not r.get("is_pass", True)),
"duration": round(duration, 2)
}
yield f"event: complete\ndata: {json.dumps(summary)}\n\n"
return Response(stream_with_context(generate()), mimetype='text/event-stream')
@app.route("/api/run", methods=["POST"])
def run_tests():
"""运行测试(非流式)"""
global USE_LLM_EVAL, LLM_API_URL
data = request.json
api_url = data.get("api_url", "http://localhost:8000")
use_llm = data.get("use_llm", USE_LLM_EVAL)
llm_api_url = data.get("llm_api_url", LLM_API_URL)
client.base_url = api_url
client.chat_endpoint = f"{api_url}/chat"
if use_llm:
test_evaluator = Evaluator(api_url=llm_api_url, use_llm=True)
else:
test_evaluator = Evaluator()
test_cases = load_test_cases()
results = []
start_time = time.time()
for case in test_cases:
question = case["question"]
expect_keywords = case["expect_keywords"]
expect_intent = case.get("expect_intent", "")
try:
answer = client.send_message(question)
if use_llm:
llm_result = test_evaluator.evaluate_with_llm(
question, answer, expect_intent, expect_keywords
)
results.append({
"id": case["id"],
"category": case["category"],
"question": question,
"answer": answer,
"expect_keywords": expect_keywords,
"expect_intent": expect_intent,
"is_pass": llm_result.get("final_pass", False),
"matched_keyword": None,
"llm_evaluation": llm_result
})
else:
is_pass, matched_keyword = test_evaluator.evaluate(answer, expect_keywords)
results.append({
"id": case["id"],
"category": case["category"],
"question": question,
"answer": answer,
"expect_keywords": expect_keywords,
"is_pass": is_pass,
"matched_keyword": matched_keyword
})
except Exception as e:
results.append({
"id": case["id"],
"category": case["category"],
"question": question,
"answer": "",
"expect_keywords": expect_keywords,
"is_pass": False,
"matched_keyword": None,
"error": str(e)
})
duration = time.time() - start_time
history_mgr.save_run(results, use_llm, api_url, duration)
return jsonify({
"success": True,
"results": results,
"use_llm": use_llm,
"duration": round(duration, 2)
})
@app.route("/api/history")
def get_history():
"""获取测试历史(支持分页)"""
page = request.args.get("page", 1, type=int)
size = request.args.get("size", 10, type=int)
offset = (page - 1) * size
runs = history_mgr.get_runs(limit=size, offset=offset)
total = history_mgr.get_runs_count()
return jsonify({
"history": runs,
"total": total,
"page": page,
"size": size
})
@app.route("/api/history/<int:run_id>")
def get_history_detail(run_id):
"""获取历史测试详情"""
results = history_mgr.get_run_results(run_id)
return jsonify(results)
@app.route("/api/history/<int:run_id>", methods=["DELETE"])
def delete_history(run_id):
"""删除历史记录"""
history_mgr.delete_run(run_id)
return jsonify({"success": True})
@app.route("/api/history/stats")
def get_history_stats():
"""获取统计信息"""
stats = history_mgr.get_statistics()
return jsonify(stats)
@app.route("/api/results")
def get_results():
"""获取上次测试结果"""
return jsonify([])
if __name__ == "__main__":
print("=" * 50)
print("Web 服务器启动: http://localhost:8080")
print("=" * 50)
app.run(host="0.0.0.0", port=8080, debug=False)