fix(roundtable): rwfx 结构化抽取纯树·单父兜底,根治叙事多对一乱线
rwfx 全链是一棵树:除预期行为↔驱动因素 1:1 外其余皆 1:N,任何节点只有一个父节点、 绝无多对一/多对多/汇聚。但弱模型常把「叙事」连到一片驱动因素 → 汇聚乱线。 - extraction_spec:ChainSpec 加 single_parent 标志(rwfx=True,xdfx 有汇聚=False); helper is_single_parent_chain / chain_level_rank;强化 rwfx ②「叙事只有一个父驱动因素」 + 新增④「全链是树,每个非根节点恰一个 from」 - flow_extract:_collapse_to_single_parent 确定性兜底——纯树业务把每个节点的多父入边 收敛到「紧邻上一层」的单一父节点(只删多余入边、不新增、不动节点),在 final 前执行; 非纯树业务(xdfx)跳过 - dag-types/types:DAG 派活忠实本阶段目标——不擅自加数量上限/精简、不替席位加体例风格、 逐条全部输出、不诱导脱离输入编造(前后台同步) - Tests: tests/test_flow_extract_single_parent.py Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
3d75040297
commit
f63a1faf3b
5 files changed
+189
-5
No files matched your search
@@ -36,7 +36,11 @@ from app.gateway.routers._roundtable_seed import (
|
||||
STRUCTURE_AGENT_ID,
|
||||
ensure_roundtable_functional_agents,
|
||||
)
|
||||
from deerflow.agents.roundtable_orchestrator.extraction_spec import build_structure_directive
|
||||
from deerflow.agents.roundtable_orchestrator.extraction_spec import (
|
||||
build_structure_directive,
|
||||
chain_level_rank,
|
||||
is_single_parent_chain,
|
||||
)
|
||||
from deerflow.config import get_app_config
|
||||
from deerflow.config.agents_config import load_agent_config, load_agent_soul
|
||||
from deerflow.models.factory import create_chat_model
|
||||
@@ -406,6 +410,64 @@ def _derive_layered_edges(nodes: list[dict[str, Any]]) -> list[dict[str, Any]] |
|
||||
return out
|
||||
|
||||
|
||||
def _collapse_to_single_parent(graph: dict[str, Any], business_code: str | None) -> dict[str, Any]:
|
||||
"""**纯树**业务链(rwfx)确定性兜底:每个非根节点最多保留一个父节点(一条入边)。
|
||||
|
||||
rwfx 全链是一棵树(除 1:1 的预期行为↔驱动因素,其余皆 1:N),任何节点都只有一个父节点、
|
||||
绝无多对一/汇聚。但弱模型常给某节点(典型是「叙事」)连多个上层父节点 → 画出汇聚/乱线。
|
||||
这里按「最近上一层」原则把每个节点的入边收敛到一条:在该节点的多个父节点里,优先保留 group
|
||||
处于它**紧邻上一层**的那个(按业务链层级序 ``chain_level_rank``);同层多个则保留模型给出的
|
||||
第一个;都判断不了就保留第一个。根节点(无入边)不变。**只删多余入边、不新增**,不影响节点。
|
||||
"""
|
||||
nodes = graph.get("nodes") or []
|
||||
edges = graph.get("edges") or []
|
||||
rank = chain_level_rank(business_code) # 层级名 → 自上而下序号
|
||||
group_by_id: dict[str, str] = {
|
||||
n["id"]: _as_str(n.get("group")) for n in nodes if isinstance(n, dict) and n.get("id")
|
||||
}
|
||||
appear: dict[str, int] = {}
|
||||
for n in nodes:
|
||||
g = _as_str(n.get("group")) if isinstance(n, dict) else ""
|
||||
if g and g not in appear:
|
||||
appear[g] = len(appear)
|
||||
|
||||
def grank(nid: str) -> int:
|
||||
g = group_by_id.get(nid, "")
|
||||
if g in rank:
|
||||
return rank[g]
|
||||
return 1000 + appear.get(g, 0) # 未在业务层级里的 group 排到最后,不抢「上一层」判定
|
||||
|
||||
by_to: dict[str, list[dict[str, Any]]] = {}
|
||||
order: list[str] = []
|
||||
for e in edges:
|
||||
t = e.get("to")
|
||||
if t not in by_to:
|
||||
by_to[t] = []
|
||||
order.append(t)
|
||||
by_to[t].append(e)
|
||||
|
||||
kept: list[dict[str, Any]] = []
|
||||
collapsed = 0
|
||||
for t in order:
|
||||
ins = by_to[t]
|
||||
if len(ins) <= 1:
|
||||
kept.extend(ins)
|
||||
continue
|
||||
tr = grank(t)
|
||||
prev_layer = [e for e in ins if grank(_as_str(e.get("from"))) == tr - 1]
|
||||
if prev_layer:
|
||||
best = prev_layer[0]
|
||||
else:
|
||||
upper = [e for e in ins if grank(_as_str(e.get("from"))) < tr]
|
||||
best = max(upper, key=lambda e: grank(_as_str(e.get("from")))) if upper else ins[0]
|
||||
kept.append(best)
|
||||
collapsed += len(ins) - 1
|
||||
if collapsed:
|
||||
logger.info("flow-extract: collapsed %d extra parent edge(s) to single-parent (tree chain %s)", collapsed, business_code)
|
||||
graph["edges"] = kept
|
||||
return graph
|
||||
|
||||
|
||||
# ── prompts ────────────────────────────────────────────────────────────────
|
||||
|
||||
_SHAPE = (
|
||||
@@ -735,6 +797,10 @@ async def extract_flow_stream(request: Request, payload: FlowExtractRequest = Bo
|
||||
if graph is None:
|
||||
yield _frame({"event": "error", "detail": "未能从模型输出解析出流程图节点"})
|
||||
return
|
||||
# 纯树业务链(rwfx)确定性兜底:把任何节点的多父入边收敛到单一父节点,杜绝弱模型
|
||||
# 违反 1:N 画出的汇聚/乱线(典型是叙事连了一片驱动因素)。非纯树业务(xdfx 有汇聚)跳过。
|
||||
if is_single_parent_chain(payload.business_code):
|
||||
graph = _collapse_to_single_parent(graph, payload.business_code)
|
||||
yield _frame({"event": "final", "graph": graph})
|
||||
except Exception as exc: # noqa: BLE001
|
||||
logger.exception("flow-extract stream failed")
|
||||
|
||||
Reference in new issue
Block a user