Repository navigation
Expand file tree
/
Copy pathfull_test.py
More file actions
executable file
·102 lines (87 loc) · 4.7 KB
/
Copy pathfull_test.py
File metadata and controls
executable file
·102 lines (87 loc) · 4.7 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
"""完整测试套件 v19(普通模式终版):9 项全链路验证"""
import json
import time
from fastapi.testclient import TestClient
from server import app
tools = [{"type": "function", "function": {
"name": "read", "description": "读取文件内容",
"parameters": {"type": "object", "properties": {"path": {"type": "string"}}, "required": ["path"]}}}]
passed, failed = [], []
def check(name, ok, detail=""):
(passed if ok else failed).append(name)
print(f"{'✓' if ok else '✗'} {name}" + (f" | {detail}" if detail else ""), flush=True)
time.sleep(3) # 防上游限流
with TestClient(app) as tc:
# 1. 模型列表
r = tc.get("/v1/models")
ids = [m["id"] for m in r.json()["data"]]
check("模型列表", r.status_code == 200 and len(ids) == 4, f"{ids}")
# 2. 默认对话(GLM-5.3-Flash)
r = tc.post("/v1/chat/completions", json={
"model": "Hy4 preview", "messages": [{"role": "user", "content": "1+1=?只回答数字"}]})
ok = r.status_code == 200 and "2" in r.json()["choices"][0]["message"]["content"]
check("默认对话(GLM)", ok,
r.json()["choices"][0]["message"]["content"][:30] if r.status_code == 200 else r.text[:80])
# 3. 指定 DeepSeek-V4.1-Flash
r = tc.post("/v1/chat/completions", json={
"model": "DeepSeek-V4.1-Flash", "messages": [{"role": "user", "content": "2+2=?只回答数字"}]})
ok = r.status_code == 200 and "4" in r.json()["choices"][0]["message"]["content"]
check("指定DeepSeek", ok,
r.json()["choices"][0]["message"]["content"][:30] if r.status_code == 200 else r.text[:80])
# 4. 指定 Hy3
r = tc.post("/v1/chat/completions", json={
"model": "Hy3", "messages": [{"role": "user", "content": "3+3=?只回答数字"}]})
check("指定Hy3", r.status_code == 200,
r.json()["choices"][0]["message"]["content"][:30] if r.status_code == 200 else r.text[:80])
# 5. 非法模型 → 400
r = tc.post("/v1/chat/completions", json={
"model": "GPT-999", "messages": [{"role": "user", "content": "hi"}]})
check("非法模型400", r.status_code == 400, r.json()["error"]["message"][:50] if r.status_code == 400 else "")
# 6. tool_call 确定性透传(连续 3 次)
tc_ok = 0
for i in range(3):
time.sleep(3)
r = tc.post("/v1/chat/completions", json={
"model": "Hy4 preview", "tools": tools,
"messages": [{"role": "user", "content": f"用 read 工具读取 /tmp/e2e_{i}.txt 告诉我内容"}]})
m = r.json()["choices"][0]["message"]
if m.get("tool_calls"):
tc_ok += 1
check("tool_call透传(3次)", tc_ok >= 2, f"{tc_ok}/3 标准 tool_calls")
# 7. tool 闭环(执行→回注→作答)
r = tc.post("/v1/chat/completions", json={
"model": "Hy4 preview", "tools": tools,
"messages": [{"role": "user", "content": "用 read 工具读取 /tmp/final_check.txt 告诉我内容"}]})
m = r.json()["choices"][0]["message"]
closed = False
if m.get("tool_calls"):
# 调用方执行
with open("/tmp/final_check.txt", "w") as f:
f.write("TOOLCALL_CLOSED_LOOP_OK_20261031")
tc_res = {"role": "tool", "tool_call_id": m["tool_calls"][0]["id"],
"content": "TOOLCALL_CLOSED_LOOP_OK_20261031"}
r2 = tc.post("/v1/chat/completions", json={
"model": "Hy4 preview", "tools": tools,
"messages": [{"role": "user", "content": "用 read 工具读取 /tmp/final_check.txt 告诉我内容"},
m, tc_res]})
closed = "TOOLCALL_CLOSED_LOOP_OK" in r2.json()["choices"][0]["message"]["content"]
check("tool闭环", closed)
# 8. 流式
r = tc.post("/v1/chat/completions", json={
"model": "Hy4 preview", "stream": True,
"messages": [{"role": "user", "content": "回复 stream ok 四个字"}]})
content = "".join(json.loads(l[5:])["choices"][0]["delta"].get("content") or ""
for l in r.text.splitlines()
if l.startswith("data:") and "[DONE]" not in l and l[5:].strip())
check("流式", "ok" in content.lower(), content[:40])
# 9. 多轮上下文
r = tc.post("/v1/chat/completions", json={
"model": "Hy4 preview", "messages": [
{"role": "user", "content": "我叫帅哥,记住这个名字"},
{"role": "assistant", "content": "好的"},
{"role": "user", "content": "我叫什么名字?只回答名字"}]})
check("多轮上下文", "帅哥" in r.json()["choices"][0]["message"]["content"],
r.json()["choices"][0]["message"]["content"][:30])
print(f"\n===== 结果: {len(passed)} 通过 / {len(failed)} 失败 =====")
if failed:
print("失败项:", failed)