mirror of
https://github.com/ThearchyHelios/AgentLab.git
synced 2026-11-25 10:52:19 +00:00
合入第 2 波复核修复 S2、S5、S6:关分析工具那一臂挡分析卡与用了分析的子工作流、节点手册里不再出现分析;问题意图词表与自查那句进评测的提示词指纹;问题意图不把问构成当来源、不把问数据质量当异常
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
"""问题意图(app/analysis/intent.py):一句问题是不是分析类问题,给助手搭问数据的图时自查用。
|
||||
|
||||
只收明确的问法、宁可漏判:漏判只是少一条自查提醒,误判要让助手多改一轮。所以反例和正例一样多——问一个字段(退款原因)、
|
||||
问业务状态(异常订单)、问服务(系统正常吗)、筛选(波动超过 10%)、问时长(持续几天)都不算。问法都是虚构的零售场景。
|
||||
问业务状态(异常订单)、问服务(系统正常吗)、问构成(谁贡献最大、哪个门店销售额最多)、问这批数据本身(今天的数据正常
|
||||
吗)、筛选(波动超过 10%)、问时长(持续几天)都不算。问法都是虚构的零售场景。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
@@ -23,7 +24,7 @@ POSITIVE = [
|
||||
("这周和上周比,销售额怎么变了", "source"),
|
||||
("上周各渠道的销售额有什么变化", "source"),
|
||||
("哪个地区拖累了整体销售额", "source"),
|
||||
("谁贡献最大", "source"),
|
||||
("哪个地区对销售额下降贡献最大", "source"),
|
||||
("增长主要来自哪个地区", "source"),
|
||||
("各地区对销售额增长的贡献是多少", "source"),
|
||||
("销售额增长的驱动因素有哪些", "source"),
|
||||
@@ -70,6 +71,15 @@ NEGATIVE = [
|
||||
"退款原因有哪些",
|
||||
"按取消原因统计上周的订单数",
|
||||
"订单主要来自哪些渠道",
|
||||
# 问构成(哪个最多):同句没有变化的说法,不算来源
|
||||
"谁贡献最大",
|
||||
"哪个门店贡献的销售额最多",
|
||||
"哪个门店贡献最大",
|
||||
# 「数据」当主语:问的是这批数据本身,不是那一项的数变没变
|
||||
"数据是否正常",
|
||||
"今天的数据正常吗",
|
||||
"上周的数据有没有异常",
|
||||
"数据有没有异常",
|
||||
"有多少异常订单",
|
||||
"有没有异常记录",
|
||||
"列出状态为异常的订单",
|
||||
@@ -109,7 +119,7 @@ def test_other_questions_are_left_alone(question):
|
||||
|
||||
|
||||
def test_the_lexicon_is_versioned_and_every_kind_has_a_label():
|
||||
assert INTENT_VERSION == 1
|
||||
assert INTENT_VERSION == 2
|
||||
assert {kind for _, kind in POSITIVE} == set(KINDS)
|
||||
found = analysis_intent("上周销售额为什么下降了")
|
||||
assert isinstance(found, Intent) and found.version == INTENT_VERSION and found.label == "原因"
|
||||
|
||||
@@ -195,14 +195,32 @@ async def test_the_rule_stays_quiet_otherwise(question, graph):
|
||||
assert checker.analysis_gaps(level="warning") == []
|
||||
|
||||
|
||||
def _ops(tools: list[str]) -> list[dict]:
|
||||
nodes, edges = _graph(tools=tools)
|
||||
def _graph_ops(nodes: dict, edges: list[dict]) -> list[dict]:
|
||||
return [{"op": "plan", "summary": "查销售额"},
|
||||
*({"op": "add_node", "node": {"id": n["id"], "type": n["type"], "label": n["id"],
|
||||
"config": n["data"]["config"]}} for n in nodes.values()),
|
||||
*({"op": "add_edge", "edge": e} for e in edges), {"op": "done", "explanation": "查一下", "run": True}]
|
||||
|
||||
|
||||
def _ops(tools: list[str]) -> list[dict]:
|
||||
return _graph_ops(*_graph(tools=tools))
|
||||
|
||||
|
||||
def _card_ops(*, subgraph: str | None = None) -> list[dict]:
|
||||
"""带分析卡的图:入口 → 查库的 Agent → 分析卡 → 报告 → 出口。subgraph 给了就把分析卡换成一个嵌套它的子工作流节点。"""
|
||||
middle = (_n("check", "analysis", items=[{"id": "wow", "op": "baseline", "from": "q",
|
||||
"params": {"period": "week", "measure": "销售额"}}])
|
||||
if subgraph is None else _n("sub", "subgraph", workflow_id=subgraph))
|
||||
nodes = {"start": _n("start", "input"),
|
||||
"q": _n("q", "tool", tool="db_query__shop", args={"sql": "SELECT 1"}),
|
||||
"bot": _n("bot", "agent", prompt="{{ input.question }}", tools=["db_query__shop", "db_schema__shop"]),
|
||||
middle["id"]: middle,
|
||||
"write": _n("write", "report", instructions="回答:{{ input.question }}"),
|
||||
"out": _n("out", "output", fields=[{"name": "回答", "value": "{{ nodes.write.text }}"}])}
|
||||
order = ["start", "q", middle["id"], "write", "out"]
|
||||
return _graph_ops(nodes, [{"source": a, "target": b} for a, b in zip(order, order[1:])])
|
||||
|
||||
|
||||
def _scripted(first: list[dict], fix: list[dict], seen: list[str]):
|
||||
class Scripted:
|
||||
async def astream(self, messages):
|
||||
@@ -257,6 +275,8 @@ async def test_a_retry_reads_the_original_question(client, monkeypatch): # noqa
|
||||
|
||||
|
||||
def test_the_catalog_and_the_manual_leave_analysis_out_when_switched_off():
|
||||
"""关掉分析工具的那一臂:工具清单里没有五个分析工具,节点手册里一处都不提分析卡、分析工具、analysis_ 开头的配置键和
|
||||
标记——助手不知道有这回事,也不会被教着去用。开着时一字不差(node_reference() 就是 NODE_REFERENCE)。"""
|
||||
assert all(f"- {name}(" in _tool_catalog([]) for name in TOOLS)
|
||||
off = _tool_catalog([], analysis_tools=False)
|
||||
assert not [line for line in off.splitlines() if line.startswith("- analysis_")]
|
||||
@@ -264,10 +284,15 @@ def test_the_catalog_and_the_manual_leave_analysis_out_when_switched_off():
|
||||
assert node_reference() is NODE_REFERENCE
|
||||
manual = node_reference(analysis_tools=False)
|
||||
assert not any(name in manual for name in TOOLS)
|
||||
# 去掉的是 agent 段里「分析类问题绑分析工具」那一条、分析卡一整条、搭图规则第 7 条;报告段的引用写法、受管规则照留
|
||||
for gone in ("分析卡", "分析工具", "analysis_"):
|
||||
assert gone not in manual, gone
|
||||
# 去掉的是 agent 段里「分析类问题绑分析工具」那一条、分析卡一整条、搭图规则第 7 条;报告段里分析结论的来处和引用写法、
|
||||
# analysis_claims / analysis_scope 两个配置、受管规则里分析卡的那一句也不给(关的那一臂没有分析结论可引)
|
||||
assert "- analysis:" not in manual and "\n7. " not in manual and "**问题问的是为什么" not in manual
|
||||
# 别的段落一字不少:只去掉那三段
|
||||
for kept in (" **默认不要写 max_steps", "- dual_check:", "\n模型字段", "- report:", "[[a:A1]]", "analysis_claims"):
|
||||
assert "[[a:" not in manual and "判定语" not in manual
|
||||
# 别的段落一字不少:报告、双盲复算、反方审稿、受管规则里和分析无关的几条照留
|
||||
for kept in (" **默认不要写 max_steps", "- dual_check:", "\n模型字段", "- report:", "- challenge:",
|
||||
"claims: \"require_citation\"", "[[m:指标id]]", "[[see:m:指标id,Q1,K1]]", "受管级别"):
|
||||
assert kept in manual, kept
|
||||
request = GenerateIn(instruction="上周销售额为什么下降", intent="answer")
|
||||
on, off_text = _user_message(request, patch=False), _user_message(request, patch=False, analysis_tools=False)
|
||||
@@ -318,6 +343,86 @@ async def test_switched_off_the_copilot_sees_no_analysis_and_bound_tools_are_blo
|
||||
assert final["next"] == "fail" and [i["level"] for i in final["issues"] if i["code"] == ANALYSIS_TOOLS_OFF] == ["error"]
|
||||
|
||||
|
||||
async def test_switched_off_an_analysis_card_is_blocked_and_removed(monkeypatch):
|
||||
"""分析卡也挡:关掉分析工具的那一臂,图里有分析卡就交回模型去掉它、把上下游直接连起来。改好了照常运行。"""
|
||||
seen: list[list[tuple[str, str]]] = []
|
||||
fix = [{"op": "remove_node", "id": "check"},
|
||||
{"op": "add_edge", "edge": {"source": "bot", "target": "write"}},
|
||||
{"op": "done", "explanation": "去掉了分析卡"}]
|
||||
_use(monkeypatch, _recording(_card_ops(), fix, seen))
|
||||
got = await _ops_of("上周销售额为什么下降", analysis_tools=False)
|
||||
rounds = [op for op in got if op["op"] == "check" and op["status"] == "repairing"]
|
||||
[issue] = [i for r in rounds for i in r["issues"] if i["code"] == ANALYSIS_TOOLS_OFF]
|
||||
assert (issue["level"], issue["node_id"], issue["field"]) == ("error", "check", None)
|
||||
assert issue["message"] == "这次不能用分析卡:「check」是分析卡,这一轮没有分析能力可用,请去掉它"
|
||||
# 交回模型的那一句守规矩:说清去掉它、上下游接回去、数怎么来;关的那一臂手册里没有「分析卡」这个说法,
|
||||
# 所以指节点说它是 analysis 节点
|
||||
assert "「check」是 analysis 节点" in issue["for_model"] and "remove_node" in issue["for_model"]
|
||||
assert "去掉它" in issue["for_model"] and "写进查询" in issue["for_model"]
|
||||
[final] = [op for op in got if op["op"] == "final"]
|
||||
assert final["next"] == "run", final["issues"]
|
||||
assert "check" not in {n["id"] for n in final["graph"]["nodes"]}
|
||||
assert not [i for i in final["issues"] if i["code"] == ANALYSIS_TOOLS_OFF]
|
||||
|
||||
# 改不好:照实列出、不运行(图里还留着分析卡)
|
||||
seen.clear()
|
||||
_use(monkeypatch, _recording(_card_ops(), [{"op": "done", "explanation": "x"}], seen))
|
||||
got = await _ops_of("上周销售额为什么下降", analysis_tools=False)
|
||||
[final] = [op for op in got if op["op"] == "final"]
|
||||
assert final["next"] == "fail"
|
||||
[left] = [i for i in final["issues"] if i["code"] == ANALYSIS_TOOLS_OFF]
|
||||
assert (left["level"], left["node_id"]) == ("error", "check")
|
||||
|
||||
|
||||
async def test_switched_off_a_subgraph_that_uses_analysis_is_blocked(monkeypatch, tmp_path):
|
||||
"""钉了版本、里面用了分析能力的子工作流(这里是里面有分析卡)也挡:这一轮用不了它。里面没有分析能力的子工作流光看外层
|
||||
看不出来,照常运行。"""
|
||||
from app.db.base import SessionLocal
|
||||
from app.db.models import Workflow
|
||||
|
||||
async def make(name: str, graph: dict) -> str:
|
||||
async with SessionLocal() as session:
|
||||
row = Workflow(name=name, graph=graph, version=1, published_version=1)
|
||||
session.add(row)
|
||||
await session.commit()
|
||||
return row.id
|
||||
|
||||
inner_card = _graph(tools=("db_query__shop", "db_schema__shop"), card=True)[0]
|
||||
plain_nodes = _graph(tools=("db_query__shop", "db_schema__shop"))[0]
|
||||
with_card = await make("异动分析(虚构)", {"nodes": list(inner_card.values()),
|
||||
"edges": [{"source": a, "target": b}
|
||||
for a, b in zip(inner_card, list(inner_card)[1:])]})
|
||||
without_card = await make("只查数(虚构)", {"nodes": list(plain_nodes.values()),
|
||||
"edges": [{"source": a, "target": b}
|
||||
for a, b in zip(plain_nodes, list(plain_nodes)[1:])]})
|
||||
seen: list[list[tuple[str, str]]] = []
|
||||
fix = [{"op": "remove_node", "id": "sub"},
|
||||
{"op": "add_edge", "edge": {"source": "q", "target": "write"}},
|
||||
{"op": "done", "explanation": "去掉了子工作流"}]
|
||||
_use(monkeypatch, _recording(_card_ops(subgraph=with_card), fix, seen))
|
||||
got = await _ops_of("上周销售额为什么下降", analysis_tools=False)
|
||||
[repairing] = [op for op in got if op["op"] == "check" and op["status"] == "repairing"]
|
||||
[issue] = [i for i in repairing["issues"] if i["code"] == ANALYSIS_TOOLS_OFF]
|
||||
assert (issue["node_id"], issue["field"]) == ("sub", "workflow_id")
|
||||
assert "这次不能用这个子工作流" in issue["message"] and "分析卡或分析工具" in issue["message"]
|
||||
assert "去掉这个 subgraph 节点" in issue["for_model"]
|
||||
[final] = [op for op in got if op["op"] == "final"]
|
||||
assert final["next"] == "run", final["issues"]
|
||||
assert "sub" not in {n["id"] for n in final["graph"]["nodes"]}
|
||||
assert not [i for i in final["issues"] if i["code"] == ANALYSIS_TOOLS_OFF]
|
||||
|
||||
# 里面没分析能力的子工作流不挡;开着分析工具时,有分析卡的子工作流也不挡
|
||||
seen.clear()
|
||||
_use(monkeypatch, _recording(_card_ops(subgraph=without_card), [], seen))
|
||||
got = await _ops_of("上周销售额为什么下降", analysis_tools=False)
|
||||
assert not [i for op in got if op["op"] == "check" and op["status"] == "repairing"
|
||||
for i in op["issues"] if i["code"] == ANALYSIS_TOOLS_OFF]
|
||||
_use(monkeypatch, _recording(_card_ops(subgraph=with_card), [], seen))
|
||||
got = await _ops_of("上周销售额为什么下降", analysis_tools=True)
|
||||
assert not [i for op in got if op["op"] == "check" and op["status"] == "repairing"
|
||||
for i in op["issues"] if i["code"] == ANALYSIS_TOOLS_OFF]
|
||||
|
||||
|
||||
async def test_switched_on_bound_analysis_tools_are_fine(monkeypatch):
|
||||
"""缺省开着:绑了分析工具不挡,和以前一样直接运行。"""
|
||||
seen: list[list[tuple[str, str]]] = []
|
||||
|
||||
@@ -343,21 +343,24 @@ def plain_graph() -> dict:
|
||||
|
||||
|
||||
class SwitchCopilot:
|
||||
"""助手剧本:给的工具清单里有分析工具(开着的那一臂)就搭「查库 → 分析卡 → 报告」,没有(关掉的那一臂)就搭「查库 →
|
||||
报告」——照给它看的用。systems 记下每次建图收到的 system。"""
|
||||
"""助手剧本:两臂都先照「查库 → 分析卡 → 报告」搭——关掉分析工具的那一臂靠真代码(answer_ops 的自查)挡回去,
|
||||
替身不替它判断。自查交回来时(只有关的那一臂会有)去掉分析卡、把查库直接接到报告。systems 记下每次建图收到的 system。"""
|
||||
|
||||
def __init__(self) -> None:
|
||||
self.systems: list[str] = []
|
||||
|
||||
async def astream(self, messages):
|
||||
if "上线前自查" in messages[1][1]:
|
||||
yield _line({"op": "done"})
|
||||
yield _line({"op": "remove_node", "id": "card"})
|
||||
yield _line({"op": "add_edge", "edge": {"source": "q", "target": "write"}})
|
||||
# 这一臂没有分析结论可引:报告只按查询结果写(报告撰写的剧本认这个记号,见 use_writers)
|
||||
yield _line({"op": "update_node", "id": "write", "config": {"instructions": f"{PLAIN_MARK}上周的周报"}})
|
||||
yield _line({"op": "done", "explanation": "这一轮没有分析工具,去掉分析卡"})
|
||||
return
|
||||
role, system = messages[0]
|
||||
assert role == "system"
|
||||
self.systems.append(system)
|
||||
graph = answer_graph() if "analysis_decompose" in system else plain_graph()
|
||||
for line in _graph_lines(graph, "看上周销售额"):
|
||||
for line in _graph_lines(answer_graph(), "看上周销售额"):
|
||||
yield line
|
||||
|
||||
|
||||
|
||||
@@ -534,6 +534,30 @@ async def test_the_estimate_says_it_cannot_price_instead_of_zero():
|
||||
assert got["total_usd"] == 0.0 and not got.get("at_least") and not got.get("unpriced_calls"), "目录价是 0 的才是真不花钱"
|
||||
|
||||
|
||||
def test_the_prompt_fingerprint_covers_the_intent_lexicon_and_the_self_check_sentence(monkeypatch):
|
||||
"""提示词指纹认得出「问题意图词表变了」和「自查交回模型的那一句变了」:改词表要升 INTENT_VERSION,改那句话它自己
|
||||
就带在里面。两者任一变了,两次评测比的时候标「提示词变了」。"""
|
||||
from app.analysis import intent
|
||||
from app.api import copilot_gen
|
||||
|
||||
before = runner.prompt_fingerprint()
|
||||
assert before == runner.prompt_fingerprint(), "同一份提示词指纹一样"
|
||||
monkeypatch.setattr(intent, "INTENT_VERSION", intent.INTENT_VERSION + 1)
|
||||
assert runner.prompt_fingerprint() != before, "词表升版本要认得出"
|
||||
monkeypatch.undo()
|
||||
|
||||
real = copilot_gen.analysis_gaps_fix
|
||||
monkeypatch.setattr(copilot_gen, "analysis_gaps_fix",
|
||||
lambda **kw: real(**kw).replace("按「分析类问题」的形状改", "换一种形状改"))
|
||||
assert runner.prompt_fingerprint() != before, "自查那句话变了要认得出"
|
||||
monkeypatch.undo()
|
||||
|
||||
# 那句话里写着两条东西:分析类问题的形状、以及不改这一处时在说明里讲清为什么
|
||||
said = copilot_gen.analysis_gaps_fix(asked="问题问的是原因(「为什么下降」)", nid="bot")
|
||||
assert said.startswith("问题问的是原因(「为什么下降」)。按「分析类问题」的形状改:给查库的 agent「bot」的 tools 加上")
|
||||
assert "analysis_decompose" in said and "done 的 explanation" in said
|
||||
|
||||
|
||||
def test_the_judge_role_keeps_its_unpriced_calls():
|
||||
"""按角色的用量里裁判那一份以前只有金额和次数:裁判模型不在价格目录里时写成 $0。"""
|
||||
usage = runner.judge_usage({"claims": {"cost_usd": 0.0, "calls": 2, "priced": False},
|
||||
|
||||
Reference in New Issue
Block a user