Repository navigation
Expand file tree
/
Copy pathe2e_test.py
More file actions
executable file
·48 lines (40 loc) · 1.99 KB
/
Copy pathe2e_test.py
File metadata and controls
executable file
·48 lines (40 loc) · 1.99 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
"""端到端测试(进程内):MOCK 上游 → server → tool_bridge → agent 循环 → 真实工具执行"""
import os
os.environ["MOCK_UPSTREAM"] = "1"
import json
import server # noqa: E402 (import 时读取 MOCK env)
from fastapi.testclient import TestClient # noqa: E402
from tools_def import TOOLS_SCHEMA, execute_tool, AGENT_ROOT # noqa: E402
os.makedirs(AGENT_ROOT, exist_ok=True)
http = TestClient(server.app)
MODEL = "ima-copilot"
MAX_ROUNDS = 6
def chat(messages: list[dict]) -> dict:
r = http.post("/v1/chat/completions", json={
"model": MODEL, "messages": messages, "tools": TOOLS_SCHEMA, "stream": False})
r.raise_for_status()
return r.json()
def run(task: str):
print(f"=== 任务: {task}\n")
messages = [{"role": "user", "content": task}]
for rnd in range(1, MAX_ROUNDS + 1):
resp = chat(messages)
choice = resp["choices"][0]
msg = choice["message"]
if choice["finish_reason"] == "tool_calls":
print(f"--- 第{rnd}轮:收到 {len(msg['tool_calls'])} 个 tool_call(finish_reason=tool_calls ✓)")
messages.append({"role": "assistant", "content": None, "tool_calls": msg["tool_calls"]})
for tc in msg["tool_calls"]:
fn = tc["function"]
print(f" ▶ {fn['name']}({fn['arguments'][:100]})")
result = execute_tool(fn["name"], fn["arguments"])
print(f" ◀ {result[:120].replace(chr(10), ' ⏎ ')}")
messages.append({"role": "tool", "tool_call_id": tc["id"],
"name": fn["name"], "content": result})
continue
print(f"--- 第{rnd}轮:最终回答(finish_reason=stop ✓)\n{msg.get('content','')}")
return True
print("!! 超过最大轮次")
return False
ok = run("帮我看下 demo.txt 里写了什么,然后用 bash 列出目录文件")
print("\n" + ("=== 端到端 tool-call 链路测试通过 ===" if ok else "=== 测试失败 ==="))