数据目录阶段 1A:结构化输出报错时退回纯文本起草,关联关系的校验说明补上空格和修改时间的检查

This commit is contained in:
ThearchyHelios
2026-10-03 09:42:48 +08:00
parent 785660e60d
commit a340d968fa
2 changed files with 29 additions and 9 deletions
+14 -9
View File
@@ -201,7 +201,8 @@ def _relation_problems(rel: Any, seen: set[str]) -> list[str]:
if not isinstance(rel, dict):
return ["关联关系应为对象"]
rid = rel.get("id")
where = f"关联关系 {rid}" if isinstance(rid, str) and rid else "关联关系"
# 带编号时后面空一格再接说明:「关联关系 r1a2 的被指向列…」
where = f"关联关系 {rid} " if isinstance(rid, str) and rid else "关联关系"
problems: list[str] = []
if not (isinstance(rid, str) and rid.strip()) or "." in str(rid):
problems.append(f"{where}缺少编号,或编号里含有句点")
@@ -232,6 +233,8 @@ def _relation_problems(rel: Any, seen: set[str]) -> list[str]:
problems.append(f"{where}的状态「{rel.get('status')}」不在可选值内")
if "note" in rel and not isinstance(rel["note"], str):
problems.append(f"{where}的备注应为文字")
if "updated_at" in rel and not isinstance(rel["updated_at"], str):
problems.append(f"{where}的修改时间应为文字")
return problems
@@ -1121,15 +1124,17 @@ def _model_tables(raw: Any) -> list[dict[str, Any]] | None:
async def _ask_model(model: Any, messages: list[Any]) -> list[dict[str, Any]]:
"""问一批。先走结构化输出;模型不支持、或给回来的不是约定的结构,再退回纯文本、从正文里取 JSON。"""
"""问一批。先走结构化输出;模型不支持、调用报错、或给回来的不是约定的结构,再退回纯文本、从正文里取 JSON。
结构化输出报错也退回(同 judge._ask):有的网关不支持工具调用,一调就报错,纯文本却能用。代价是真的
网络故障会多试一次,两次加起来仍受 MODEL_TIMEOUT_S 约束。
"""
try:
runnable = model.with_structured_output(CATALOG_DRAFT_PROMPT_SCHEMA)
except (AttributeError, NotImplementedError):
runnable = None
if runnable is not None:
parsed = _model_tables(await runnable.ainvoke(messages))
if parsed is not None:
return parsed
parsed = _model_tables(await model.with_structured_output(CATALOG_DRAFT_PROMPT_SCHEMA).ainvoke(messages))
except Exception: # noqa: BLE001 - 退回纯文本
parsed = None
if parsed is not None:
return parsed
from app.engine.state import message_text
reply = await model.ainvoke(messages)
+15
View File
@@ -222,6 +222,21 @@ async def test_model_failure_only_affects_its_batch():
assert len(model.calls) >= 2 # 按表分批,不是一次全发
async def test_model_without_structured_output_falls_back_to_text():
"""有的网关不支持工具调用:结构化输出一调就报错,退回纯文本、从正文里取 JSON。"""
from langchain_core.messages import AIMessage
class TextOnly:
def with_structured_output(self, schema, **_):
raise NotImplementedError
async def ainvoke(self, messages):
return AIMessage(content='草稿如下:{"tables": [{"table": "visits", "label": "入园记录"}]}')
got = await catalog.draft_with_model(TextOnly(), _source(_scenic_tables()), ["visits"])
assert got["visits"].notes == {"label": make_item("入园记录", "llm")}
async def test_model_draft_respects_system_notes_of_imported_tables():
"""导入表格:表说明已经写了粒度、列说明写了单位,模型起草的粒度和单位不进目录,免得重复或矛盾。"""
src = _source({"日报": _table(_col("日期", "TEXT", comment="格式 YYYY-MM-DD"), _col("客流"), pk=(),