Files
AgentLab/backend/app/chat/optimization.py
T
ThearchyHelios 46a2988dc1 问数据双轨重构:快速直查与深度分析分流、可点击数字证据闭环及双语文档
- 重构问数据执行架构为「快速直查(fast)」与「深度分析(deep)」双模式,内置语义预命中(Grounding)注入指标卡片、标准实体编码与表目录血缘
- 快速直查免工作流生成单节点执行,新增零额外模型调用的数字证据归一与封存(direct_evidence),打通可点击数字溯源与 /verify 校验
- 深度分析与助手编排接入跨源本体穿透(自动升级重跑与一键沿本体排查关联数据源),优化数据源范围智能收敛与往返缓存
- 补齐端到端回归测试矩阵与中英文全量文档说明
2026-11-12 12:58:42 +08:00

40 lines
2.3 KiB
Python

"""Versioned optimizations for newly launched, simple ask-data runs.
Stored graphs/checkpoints keep their original behavior. No tools or permissions are added.
"""
from __future__ import annotations
from copy import deepcopy
import re
def simple_lookup(question: str) -> bool:
return (0 < len(question.strip()) <= 120
and bool(re.search(r"多少|查询|查一下|给我|是多少|昨天|今天|前天", question))
and not re.search(r"分析|原因|为什么|趋势|对比|比较|预测|建议|报告|周报|月报|详细|解释|同比|环比", question))
def prepare(graph: dict, question: str) -> dict:
graph = deepcopy(graph)
nodes = graph.get("nodes") or []
# Only the standard single-agent lookup chain. Custom analysis/report graphs retain their behavior.
if not simple_lookup(question) or sorted(n.get("type", "") for n in nodes) != ["agent", "input", "output", "report"]:
return graph
by_type = {n["type"]: n for n in nodes}
chain = [by_type[t]["id"] for t in ("input", "agent", "report", "output")]
if {(e.get("source"), e.get("target")) for e in graph.get("edges", [])} != set(zip(chain, chain[1:])):
return graph
if by_type["output"].get("data", {}).get("config", {}).get("contract"):
return graph
for kind in ("agent", "report"):
config = by_type[kind].setdefault("data", {}).setdefault("config", {})
config["ask_data_optimization"] = 1
by_type["report"]["data"]["config"]["reuse_answer_from"] = by_type["agent"]["id"]
return graph
CONCISE_RULE = """这是一次简短问数。优先使用已授权且已绑定的 caliber_query 或 metric_query;不能绕过工具权限。
对象、日期与口径明确且已有完整结果时即可结束,不为补充背景反复查询。
最终答案简短写出结果、对象、时间归属与必要缺口:所有数值必须严格写成带行号的单元格证据引用(例如 [[v:Q1.r0.oee]],必须带 .r0. 这类行号),严禁在引用外手写任何裸数字(包括百分比、行数、差值);查询无返回记录时写「未返回记录」或「暂无数据」,不要写「0 行」。已有证据不足时明确说明,不把不同班次比率简单平均。
系统会核验这份答案,核验通过后直接作为答复。"""