mirror of
https://github.com/ThearchyHelios/AgentLab.git
synced 2026-11-17 01:01:58 +00:00
975dde1146
- 均分:超过 40 列的表,列分段数是列数除以 40 向上取整,各段列数平均(相差不超过 1),不再留下 2 到 8 列的尾段(实测 会思考的模型常对这种小段只给空壳);表级那段、窄表拼段、parts_total 不变。 - 拆不开的段(不到 10 列)只给空壳:再问一次,结构化输出换纯文本,已经走纯文本的原样再问,仍是空壳才记没写出来。 - 超时当作这一段太大:不再原样重试同样大小的段,按额度用完的办法拆小再问;网络错误、429、5xx 仍原样退避重试。 - 段大小随模型调整:一次起草里额度用完或超时累计 2 次,之后新开始的段列数上限减半(40 → 20 → 10,最低 10),新开始 的段超过上限就先按上限均分开(多表的段按表重新拼);任务记当前的上限 part_columns。 - 封顶:一段(连同它拆出来的、补问的、重试的)最多调用 8 次,到了次数还没写完的记没写出来、原因写清。止损照旧。 - 提醒:最近 6 次调用有 4 次以上没写出来(额度用完、超时、空壳、出错),任务的 notice 写一句提醒(次数、原因,建议在 「设置 → 偏好设置」的「起草模型」换一个模型、减少表或停止);之后最近 6 次里少于 2 次就清掉。 - 用量:进度回调 usage 换成 call_done(CallReport:用量、没写出来的原因种类、当前的提醒和列数上限),任务的用量加 timeouts、failed_calls;超时的调用 token 照实记 0。 - 留空的列:请模型写了、模型也列出了,中文名和含义却都没给(或者给的都没通过校验)的列记在 ModelOutcome.blank,逐表 记 blank_columns 和最多 5 个列名 blank_sample,任务上是合计,目录清单的 last_draft 也带列数;表的状态不因此变。已确认 的、库注释已经说清的列不算。补问时把漏的列列出来、字段却留空的,算留空,不再记成「漏了这些列」。 - 公平排队:宽表的表级那段写完,它的列分段排到已经排着的段后面(先来先服务),不再插到队首让窄表一直等。 - 原因:几段因为同一个原因没写出来时说「这几段」,同一段拆出来的几块仍是「这一段」;只有真拆小过才补「已拆小重试仍 没写出」(截断后只补剩下的不算);每段写上列的范围,例如「第 5 段(列名 到 列名,共 33 列)」,窄表写出是哪几列。 - 库:catalog_draft_jobs 加 timeouts、failed_calls、part_columns、notice,catalog_draft_tasks 加 blank_columns、 blank_sample,旧库启动时补列(存量回填 0 和空)。假模型加 hang(头几次调用一直不回,用来模拟超时)。
162 lines
7.6 KiB
Python
162 lines
7.6 KiB
Python
"""数据目录起草用的假模型:不发请求,按表名回固定的草稿。
|
||
|
||
model = FakeModel({"visits": {"label": "入园记录", "kind": "fact"}}, fail={"orders"})
|
||
await catalog.draft_with_model(model, source, ["visits"])
|
||
|
||
- 结构化输出(with_structured_output(...).ainvoke)从提示词里认出这一段请它写哪些表、每张表写哪部分(requested:
|
||
整张表、只写表级的项、只写列出的几列、表级的项加几列),返回 {"tables": [{"table": 表名, …}, …]},只给请它写的
|
||
那部分(整张表给 replies[表名] 的全部;只写几列时只给 replies 里这几列);
|
||
- 照提示词的要求,请它写的每一列都列出来:replies 里没写的列只给列名({"name": 列名},字段留空,是拿不准),
|
||
不整列漏掉。list_all=False 时只给 replies 里有的列(模拟漏列的模型:漏掉的列会被补问一次,补问后仍没给的算没写出来);
|
||
- 直接 ainvoke 是纯文本那条路:同样的内容写成一段正文(AIMessage),前面带一句话;
|
||
- fail 里的表所在的那一段抛异常,用来验证「一段失败只影响这一段」;
|
||
- hang=N:头 N 次调用一直不回(等起草引擎按时限掐掉,当作超时);
|
||
- calls 记下每次收到的提示词全文,用来核对交给模型的内容(只有结构,没有数据);modes 记下每次走的是
|
||
structured 还是 text。
|
||
|
||
structured、text 模拟实测见过的几种回复(两条路各自设):
|
||
|
||
- "tables":正常,{"tables": [...]};
|
||
- "hollow":退化的空壳,表名对上了、一个字段都没有:{"tables": [{"table": 表名}, …]};
|
||
- "single":只给第一张表的对象,没有 tables 外壳:{"table": 表名, …};
|
||
- "error":这条路用不了(网关不支持工具调用之类),一调就抛异常。
|
||
"""
|
||
from __future__ import annotations
|
||
|
||
import asyncio
|
||
import json
|
||
from typing import Any
|
||
|
||
from app.data.catalog import CATALOG_DRAFT_PROMPT_LABELS as _P
|
||
|
||
#: 表名下一行写着「这次只写…」时,请模型写的是哪一部分(data/catalog_draft.py 按这几句写提示词)
|
||
_ONLY_TABLE = _P["only_table"]
|
||
_ONLY_COLUMNS = _P["only_columns"].split("{")[0]
|
||
_TABLE_AND = _P["table_and_columns"].split("{")[0]
|
||
|
||
|
||
def requested(text: str) -> dict[str, tuple[str, list[str] | None]]:
|
||
"""提示词里请模型写哪些表、每张表写哪部分:{表名: (哪部分, 列)}。哪部分是 full(整张表)、table(只写表级的项)、
|
||
columns(只写列出的列,列是表结构描述里列出的那几列)、table+columns(表级的项加「这次只写」那一行点名的列)。"""
|
||
out: dict[str, tuple[str, list[str] | None]] = {}
|
||
for chunk in text.split("\n\n"):
|
||
lines = chunk.split("\n")
|
||
head = lines[0]
|
||
if not head.startswith("表 "):
|
||
continue
|
||
name = head[2:]
|
||
if name.endswith("(视图)"):
|
||
name = name[:-len("(视图)")]
|
||
second = lines[1] if len(lines) > 1 else ""
|
||
listed = [line[2:].split(" ")[0] for line in lines if line.startswith("- ")]
|
||
if second.startswith(_ONLY_TABLE):
|
||
out[name] = ("table", None)
|
||
elif second.startswith(_ONLY_COLUMNS):
|
||
out[name] = ("columns", listed)
|
||
elif second.startswith(_TABLE_AND):
|
||
out[name] = ("table+columns", second.split(":", 1)[1].split("、"))
|
||
else:
|
||
out[name] = ("full", None)
|
||
return out
|
||
|
||
|
||
def answer(reply: dict, name: str, part: str, columns: list[str] | None) -> dict:
|
||
"""按请它写的那部分,从 replies[表名] 里取。"""
|
||
if part == "full":
|
||
return {"table": name, **reply}
|
||
out: dict[str, Any] = {"table": name}
|
||
if part in ("table", "table+columns"):
|
||
out.update({k: v for k, v in reply.items() if k != "columns"})
|
||
if part in ("columns", "table+columns"):
|
||
wanted = set(columns or [])
|
||
out["columns"] = [c for c in reply.get("columns") or [] if isinstance(c, dict) and c.get("name") in wanted]
|
||
return out
|
||
|
||
|
||
def listed(text: str) -> dict[str, list[str]]:
|
||
"""提示词里每张表的结构描述列出了哪些列(「- 列名 类型」那几行)。"""
|
||
out: dict[str, list[str]] = {}
|
||
for chunk in text.split("\n\n"):
|
||
lines = chunk.split("\n")
|
||
if not lines[0].startswith("表 "):
|
||
continue
|
||
name = lines[0][2:]
|
||
if name.endswith("(视图)"):
|
||
name = name[:-len("(视图)")]
|
||
out[name] = [line[2:].split(" ")[0] for line in lines if line.startswith("- ")]
|
||
return out
|
||
|
||
|
||
def wanted_columns(text: str, name: str, part: str, columns: list[str] | None) -> list[str]:
|
||
"""这一段请它写这张表的哪几列:整张表是结构描述里的全部列;只写表级的项时没有。"""
|
||
if part == "full":
|
||
return listed(text).get(name, [])
|
||
if part in ("columns", "table+columns"):
|
||
return list(columns or [])
|
||
return []
|
||
|
||
|
||
def with_every_column(entry: dict, wanted: list[str]) -> dict:
|
||
"""请写的列都列出来:没写的只给列名(字段留空)。"""
|
||
have = {c.get("name") for c in entry.get("columns") or [] if isinstance(c, dict)}
|
||
missing = [{"name": c} for c in wanted if c not in have]
|
||
if not missing:
|
||
return entry
|
||
return {**entry, "columns": list(entry.get("columns") or []) + missing}
|
||
|
||
|
||
class FakeModel:
|
||
def __init__(self, replies: dict[str, dict], fail: set[str] = frozenset(), *,
|
||
structured: str = "tables", text: str = "tables", list_all: bool = True, hang: int = 0) -> None:
|
||
self.replies, self.fail, self.calls = replies, set(fail), []
|
||
self.structured, self.text = structured, text
|
||
self.list_all = list_all
|
||
self.hang = hang
|
||
self.modes: list[str] = []
|
||
|
||
def with_structured_output(self, schema: Any, **_: Any) -> "_Structured":
|
||
assert schema["title"]
|
||
return _Structured(self)
|
||
|
||
async def ainvoke(self, messages: list[Any]) -> Any:
|
||
from langchain_core.messages import AIMessage
|
||
|
||
reply = await self._answer(messages, "text")
|
||
return AIMessage(content="草稿如下:" + json.dumps(reply, ensure_ascii=False))
|
||
|
||
async def _answer(self, messages: list[Any], mode: str) -> dict:
|
||
if self.hang > 0:
|
||
self.hang -= 1
|
||
self.calls.append("\n".join(str(getattr(m, "content", m)) for m in messages))
|
||
self.modes.append(mode)
|
||
await asyncio.sleep(3600)
|
||
return self._reply(messages, mode)
|
||
|
||
def _reply(self, messages: list[Any], mode: str) -> dict:
|
||
text = "\n".join(str(getattr(m, "content", m)) for m in messages)
|
||
self.calls.append(text)
|
||
self.modes.append(mode)
|
||
asked = requested(text)
|
||
names = [t for t in asked if t in self.replies]
|
||
if any(t in self.fail for t in asked):
|
||
raise RuntimeError("网关超时")
|
||
shape = self.structured if mode == "structured" else self.text
|
||
if shape == "error":
|
||
raise NotImplementedError("这条路用不了")
|
||
if shape == "hollow":
|
||
return {"tables": [{"table": t} for t in names]}
|
||
answers = [answer(self.replies[t], t, *asked[t]) for t in names]
|
||
if self.list_all:
|
||
answers = [with_every_column(a, wanted_columns(text, t, *asked[t])) for a, t in zip(answers, names)]
|
||
if shape == "single":
|
||
return answers[0] if answers else {}
|
||
return {"tables": answers}
|
||
|
||
|
||
class _Structured:
|
||
def __init__(self, owner: FakeModel) -> None:
|
||
self.owner = owner
|
||
|
||
async def ainvoke(self, messages: list[Any]) -> dict:
|
||
return await self.owner._answer(messages, "structured")
|