mirror of
https://github.com/ThearchyHelios/AgentLab.git
synced 2026-11-17 02:52:00 +00:00
4e876ff9d8
一次真实运行里证据台账有上百次查询(4 个 Agent 查询次数悬殊),清单写死 12000 字,十万字以上的目录被截到一万两千字以内,只有前二十来次查询进了写作者的视野;查询按编号排,前面的 Agent 占满了预算,有的 Agent 一次都没露面。 - 预算:写作模型上下文窗口的 40%(字数按 1 字约 1 token 保守算),窗口先看模型接入里给这个模型登记的 context,再看模型目录;拿不到按 128K(和 Agent 护栏同一个保守值),即 51200 字;下限 12000、上限 160000 - 第一优先:各 Agent 经按出处核对交来的结构化字段(换成报告目录里的全局编号,值按单元格引用的渲染规则写,给出可照抄的引用标记)、口径卡指标和运行输入,一律列出;结构化结论为空的来源写明不代表没有数据 - 查询按产出它的节点分组、轮流展开:字段引用到的查询先展开,再给每次查询列前 3 行,有余量再补到 20 行;表和字段、知识库检索最多占预算的四分之一 - 没展开的查询列出编号和一行摘要(表、行数);约定只能在句末写 [[see:Qn]] 作依据,不猜行列、不凭摘要写数,数字照样逐字核对 - 清单开头明说目录有省略、各来源各省略了几次;报告节点发 report_catalog_trimmed 警告(带 queries、omitted、nodes),时间线给它专门的标题和下一步,运行复核算作缺口 - 放得下时清单和以前一字不差(多出的只有结构化字段那一节)
107 lines
5.4 KiB
Python
107 lines
5.4 KiB
Python
"""合成的「4 个专项 Agent 并行查库 → 报告」场景:查询多、分布不均,每个 Agent 都交了按出处核对的字段。
|
|
|
|
形状仿一次真实运行(4 个 Agent 并行、查询次数悬殊,合计上百次),次数和内容全部虚构:门店、会员、卡券、活动。
|
|
不连库、不调模型:台账条目、查询快照、表结构快照、Agent 的 data / data_evidence 都在内存里拼好,快照经 loader 取。
|
|
|
|
台账按节点跑完的先后排(state["evidence"] 是各节点结束时追加的),所以全局编号 Q1… 前 26 个都是会员专项的。
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
|
|
from app.engine.evidence import cell_eid, make_eid
|
|
|
|
#: (节点 id, 画布上的名字, 查询次数, 查的表)
|
|
AGENTS: list[tuple[str, str, int, str]] = [
|
|
("agent_member", "会员专项", 26, "member_daily"),
|
|
("agent_store", "门店专项", 17, "store_daily"),
|
|
("agent_coupon", "卡券核销", 79, "coupon_redemption"),
|
|
("agent_campaign", "活动内容", 42, "campaign_view"),
|
|
]
|
|
COLUMNS = ["store_name", "biz_time", "cnt", "amount"]
|
|
TYPES = {"store_name": "text", "biz_time": "datetime", "cnt": "number", "amount": "number"}
|
|
ROWS = 30
|
|
#: 结构化抽取失败(两次都被截断)的那个 Agent:字段全是空值
|
|
EMPTY_AGENT = "agent_coupon"
|
|
FIELDS = ["total_cnt", "top_store", "latest_time", "top_stores", "churn_rate"]
|
|
|
|
|
|
def artifact_of(node_id: str, i: int) -> str:
|
|
return f"{node_id}-q{i:03d}".ljust(64, "0")
|
|
|
|
|
|
def snapshot(node_id: str, i: int, rows: int = ROWS) -> dict[str, Any]:
|
|
"""第 i 次查询的快照:每行一个门店,时间是 ISO 写法(快照里存的就是这样)。"""
|
|
seed = sum(map(ord, node_id)) + i * 7
|
|
return {
|
|
"columns": list(COLUMNS),
|
|
"column_types": dict(TYPES),
|
|
"rows": [[f"门店{(seed + r) % 90 + 1:02d}", f"2026-06-{r % 28 + 1:02d}T08:{(seed + r) % 60:02d}:39",
|
|
(seed * 13 + r * 17) % 900 + 10, f"{(seed * 31 + r * 7) % 9000 + 100}.50"] for r in range(rows)],
|
|
"truncated": False,
|
|
}
|
|
|
|
|
|
def schema_snapshot(table: str) -> dict[str, Any]:
|
|
return {"source": "shop", "tables": {table: {"columns": [{"name": c, "type": TYPES[c]} for c in COLUMNS]}}}
|
|
|
|
|
|
def build() -> tuple[list[dict[str, Any]], dict[str, Any], dict[str, Any], dict[str, str]]:
|
|
"""(台账, 快照 {工件 id: 内容}, state["nodes"], {节点 id: 名字})。"""
|
|
ledger: list[dict[str, Any]] = []
|
|
store: dict[str, Any] = {}
|
|
nodes: dict[str, Any] = {}
|
|
titles: dict[str, str] = {}
|
|
for node_id, title, count, table in AGENTS:
|
|
titles[node_id] = title
|
|
schema_id = f"schema-{table}".ljust(64, "0")
|
|
store[schema_id] = schema_snapshot(table)
|
|
ledger.append({"kind": "schema", "node_id": node_id, "exec": 1, "artifact": schema_id, "source": "shop",
|
|
"tables": {table: list(COLUMNS)}})
|
|
for i in range(1, count + 1):
|
|
aid = artifact_of(node_id, i)
|
|
store[aid] = snapshot(node_id, i)
|
|
ledger.append({"kind": "query", "node_id": node_id, "exec": 1, "artifact": aid,
|
|
"via": f"tool-{aid}"[:64], "call_id": f"call_{i}", "tool": "db_query__shop", "source": "shop",
|
|
"columns": list(COLUMNS), "rows": ROWS, "truncated": False,
|
|
"schema_artifact": schema_id, "tables": [table]})
|
|
nodes[node_id] = agent_output(node_id, store)
|
|
return ledger, store, nodes, titles
|
|
|
|
|
|
def agent_output(node_id: str, store: dict[str, Any]) -> dict[str, Any]:
|
|
"""Agent 交出的结构化字段:本节点的 Q3 那一格、Q5 的前 5 行;EMPTY_AGENT 抽取失败,字段全空。"""
|
|
if node_id == EMPTY_AGENT:
|
|
reason = "结构化抽取没有完成:模型的输出额度在推理阶段就用完了(最后一次的额度为 32768 token),结构化结论没能写出来"
|
|
return {"text": "查到了大量核销和兑换记录。", "data": {f: None for f in FIELDS},
|
|
"data_evidence": {f: {"status": "unresolved", "reason": reason} for f in FIELDS}}
|
|
q3, q5 = artifact_of(node_id, 3), artifact_of(node_id, 5)
|
|
first = store[q3]["rows"][0]
|
|
|
|
def cell(column: str, index: int) -> dict[str, Any]:
|
|
return {"ref": f"Q3.r0.{column}", "call": "Q3", "artifact": q3, "via": f"tool-{q3}"[:64],
|
|
"locator": {"row": 0, "column": column}, "eid": cell_eid(q3, 0, column), "status": "verified"}
|
|
|
|
rows_locator = {"rows": [0, 4], "column": "store_name"}
|
|
return {
|
|
"text": "结论见字段。",
|
|
"data": {"total_cnt": first[2], "top_store": first[0], "latest_time": first[1],
|
|
"top_stores": [r[0] for r in store[q5]["rows"][:5]], "churn_rate": None},
|
|
"data_evidence": {
|
|
"total_cnt": cell("cnt", 2), "top_store": cell("store_name", 0), "latest_time": cell("biz_time", 1),
|
|
"top_stores": {"ref": "Q5.r0-4.store_name", "call": "Q5", "artifact": q5, "via": f"tool-{q5}"[:64],
|
|
"locator": rows_locator, "eid": make_eid("rows", q5, rows_locator), "status": "verified"},
|
|
"churn_rate": {"ref": None, "status": "missing"},
|
|
},
|
|
}
|
|
|
|
|
|
def global_alias(node_id: str, local: int) -> str:
|
|
"""本节点第 local 次查询在报告目录里的全局编号(台账按节点先后排)。"""
|
|
offset = 0
|
|
for nid, _, count, _ in AGENTS:
|
|
if nid == node_id:
|
|
return f"Q{offset + local}"
|
|
offset += count
|
|
raise KeyError(node_id)
|