From a340d968fa8c28716e7cc8c444f8663b1cc1c018 Mon Sep 17 00:00:00 2001 From: ThearchyHelios Date: Sat, 3 Oct 2026 09:42:48 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=B0=E6=8D=AE=E7=9B=AE=E5=BD=95=E9=98=B6?= =?UTF-8?q?=E6=AE=B5=201A=EF=BC=9A=E7=BB=93=E6=9E=84=E5=8C=96=E8=BE=93?= =?UTF-8?q?=E5=87=BA=E6=8A=A5=E9=94=99=E6=97=B6=E9=80=80=E5=9B=9E=E7=BA=AF?= =?UTF-8?q?=E6=96=87=E6=9C=AC=E8=B5=B7=E8=8D=89=EF=BC=8C=E5=85=B3=E8=81=94?= =?UTF-8?q?=E5=85=B3=E7=B3=BB=E7=9A=84=E6=A0=A1=E9=AA=8C=E8=AF=B4=E6=98=8E?= =?UTF-8?q?=E8=A1=A5=E4=B8=8A=E7=A9=BA=E6=A0=BC=E5=92=8C=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E6=97=B6=E9=97=B4=E7=9A=84=E6=A3=80=E6=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/data/catalog.py | 23 ++++++++++++++--------- backend/tests/test_catalog_draft.py | 15 +++++++++++++++ 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/backend/app/data/catalog.py b/backend/app/data/catalog.py index ae2a482d..f4431d7d 100644 --- a/backend/app/data/catalog.py +++ b/backend/app/data/catalog.py @@ -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) diff --git a/backend/tests/test_catalog_draft.py b/backend/tests/test_catalog_draft.py index 2de32020..a80d4282 100644 --- a/backend/tests/test_catalog_draft.py +++ b/backend/tests/test_catalog_draft.py @@ -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=(),