test_llm_live.py
4.66 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
"""真实大模型场景评测(默认跳过):RUN_LLM=1 时用 .env 的 LLM_API_KEY 调真实模型,验证「每句话都调对工具、不照抄旧通知」。
RUN_LLM=1 pytest tests/test_llm_live.py -s # 用系统设置里的模型(默认 qwen-plus-latest)
RUN_LLM=1 EVAL_MODEL=qwen-plus pytest tests/test_llm_live.py -s # 换个模型对比
场景取自 2026-11-12 sit 事故(建单 → 不提交 → 又说不提交 → 再发零件:qwen-plus 不调工具、照抄聊天记录里的旧通知),
以及日常最容易判错的几类话。DMS / 取数端用 FakeEdge,不碰真实系统;每句话打印「原话 → 工具 / 结果」,-s 可以看到。
换模型、改提示词后都应该跑一遍。
"""
import os
import pytest
from app.services import agent, settings
from tests.conftest import REAL_LLM_KEY, fake_now
from tests.test_restock_flow import FakeEdge, group_msgs, say, settle, setup_store
pytestmark = pytest.mark.skipif(os.environ.get("RUN_LLM") != "1" or not REAL_LLM_KEY, reason="RUN_LLM=1 才跑真实模型")
REAL_RUN = agent.Assistant._run_llm # 模块导入时还没被 conftest 的假模型替换
@pytest.fixture()
def k(client, admin, monkeypatch):
from app.services import edge
monkeypatch.setenv("LLM_API_KEY", REAL_LLM_KEY)
monkeypatch.setattr(agent.Assistant, "_run_llm", REAL_RUN)
monkeypatch.setattr(edge, "run", FakeEdge())
kit = setup_store(client, admin) # 会清空测试库的业务数据和设置:换模型要放在它后面
if os.environ.get("EVAL_MODEL"):
settings.save("system", {"llm_model": os.environ["EVAL_MODEL"]}, "评测")
settings.invalidate()
fake_now(hh=10)
kit.heartbeat()
print(f"\n模型:{settings.get().llm_model}")
return kit
def turn(k, text: str, expect: str | tuple[str, ...], sender="zhangsan") -> dict:
"""说一句话,断言调了 expect 里的工具(reply_text 也是工具),且没有走「没调工具直接回复」。"""
d = say(k, text, sender=sender)
settle(k)
tools = [c["tool"] for c in d["calls"] if not c.get("duplicate")]
print(f" {text!r:44} → {','.join(tools) or '(没调工具)':28} {d['result']}")
want = (expect,) if isinstance(expect, str) else expect
assert d["result"] != "no_tool_text", d.get("blocked_reply")
assert tools and tools[0] in want, (tools, d.get("blocked_reply"), d.get("rounds"))
return d
def test_incident_replay(k, outbox):
"""事故原样复现:上一单取消之后,再说「不提交」、再发零件,都要调工具,最后真的新建了单次补货。"""
turn(k, "17801F0060 1个", "single_restock")
turn(k, "订", "confirm_order")
run = k.svc().single_open(k.store_id)
assert run["status"] == "awaiting_submit"
turn(k, "0888080463 5个", "single_restock") # 待提交时发新零件:工具回「上一单还没处理完」
turn(k, "8713902280,1个 \n车型 广汽丰田bZ4X", "single_restock") # 再来一次:仍要调工具,不能照抄上一句
turn(k, "不提交", "cancel_order")
assert k.svc().run(run["id"])["status"] == "cancelled"
turn(k, "不提交", "cancel_order", sender="lisi") # 单已经结束:照样调工具,由工具回复
assert run["order_no"] not in group_msgs(k, outbox)[-1]
turn(k, "17801F0060*1 车架号LVGBXA729LG010472", "single_restock", sender="lisi")
new = k.svc().single_open(k.store_id)
assert new and new["id"] != run["id"]
def test_pending_submit_phrases(k):
"""待确认提交时的几种说法:口语同意 → confirm_order(不提交);「提交吧」→ submit_order(工具提示要说确认提交)。"""
turn(k, "8713902280 2个", "single_restock")
turn(k, "没毛病,整吧", "confirm_order")
turn(k, "好的", "confirm_order")
turn(k, "提交吧", "submit_order")
assert k.svc().single_open(k.store_id)["status"] == "awaiting_submit"
turn(k, "我那单到哪了?", "single_status")
turn(k, "确认提交", "submit_order")
def test_chitchat_and_queries(k, outbox):
"""闲聊走 reply_text(不能编单号);只问有没有货、没说数量 → 反问要订几个,不建单;一句话多个零件只发起第一个。"""
d = turn(k, "你好,你能做什么?", "reply_text")
assert d["result"] in ("free_reply", "reply_blocked", "refused")
turn(k, "帮我写一首诗", ("reply_text",))
turn(k, "8713902280 有货吗", "reply_text")
assert k.svc().single_open(k.store_id) is None
d = turn(k, "帮我订:591819818*2,5198192123*10", "single_restock")
assert d["calls"][0]["args"]["part_no"] == "591819818"