From 0543a4da8b3e142c6324d6f19aa08c9ef79bf317 Mon Sep 17 00:00:00 2001 From: root <953994191@qq.com> Date: Mon, 4 May 2026 19:03:41 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=BD=BB=E5=BA=95=E4=BF=AE=E5=A4=8D=20R?= =?UTF-8?q?AG=20=E6=97=A0=E9=99=90=E5=BE=AA=E7=8E=AF=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 关键修复: 1. rag_nodes.py: history 里记录的 action 改成大写 RETRIEVE_RAG 2. react_nodes.py: route_by_reasoning 优先检查 DIRECT_RESPONSE 3. 优化路由判断顺序,避免不必要的循环 --- backend/app/main_graph/nodes/rag_nodes.py | 2 +- backend/app/main_graph/nodes/react_nodes.py | 20 +++++++++++++------- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/backend/app/main_graph/nodes/rag_nodes.py b/backend/app/main_graph/nodes/rag_nodes.py index 2a4c749..83652ad 100644 --- a/backend/app/main_graph/nodes/rag_nodes.py +++ b/backend/app/main_graph/nodes/rag_nodes.py @@ -234,7 +234,7 @@ async def rag_retrieve_node(state: MainGraphState, config: Optional[Dict[str, An # 关键修复:把 rag_retrieve 加到 reasoning_history 里,让下次推理知道 state.reasoning_history.append({ "step": state.reasoning_step, - "action": "rag_retrieve", + "action": "RETRIEVE_RAG", # 大写,和推理结果保持一致 "confidence": 1.0, "reasoning": "RAG 检索完成", "timestamp": datetime.now().isoformat() diff --git a/backend/app/main_graph/nodes/react_nodes.py b/backend/app/main_graph/nodes/react_nodes.py index 251262c..2c60263 100644 --- a/backend/app/main_graph/nodes/react_nodes.py +++ b/backend/app/main_graph/nodes/react_nodes.py @@ -359,30 +359,36 @@ def route_by_reasoning(state: MainGraphState) -> str: return "rag_retrieve" return "react_reason" - # 关键修复:检查是否已经执行过子图,如果是,直接去 llm_call + # ========== 关键修复:优先检查当前推理结果 ========== + reasoning_result: Optional[ReasoningResult] = state.debug_info.get("reasoning_result") + if reasoning_result and reasoning_result.action == ReasoningAction.DIRECT_RESPONSE: + info(f"[route_by_reasoning] 当前推理结果=DIRECT_RESPONSE,直接去 llm_call") + return "llm_call" + + # ========== 然后检查历史和状态 ========== previous_actions = [h.get("action") for h in state.reasoning_history] + + # 检查是否已经执行过子图 if "subgraph_completed" in previous_actions or state.final_result: return "llm_call" - # 关键修复:检测 RAG 重复循环 - 如果发现"RETRIEVE_RAG"出现超过1次,直接去 LLM + # 检测 RAG 重复循环 - 如果发现"RETRIEVE_RAG"出现超过1次,直接去 LLM rag_count = previous_actions.count("RETRIEVE_RAG") if rag_count >= 2: info(f"[route_by_reasoning] 检测到 RAG 重复循环({rag_count}次),直接去 llm_call") return "llm_call" - # 关键修复:如果已经有 rag_docs 或 rag_context,说明已经检索过了,直接去 LLM + # 如果已经有 rag_docs 或 rag_context,说明已经检索过了,直接去 LLM if (state.rag_docs and len(state.rag_docs) > 0) or (state.rag_context and len(state.rag_context) > 0): info(f"[route_by_reasoning] 检测到已存在 RAG 检索结果,直接去 llm_call") return "llm_call" - # 关键修复:限制最多 3 次推理,避免无限循环 + # 限制最多 3 次推理,避免无限循环 if len(previous_actions) >= 3: info(f"[route_by_reasoning] 已达到最大推理次数 ({len(previous_actions)}),直接去 llm_call") return "llm_call" - # 获取推理结果 - reasoning_result: Optional[ReasoningResult] = state.debug_info.get("reasoning_result") - + # ========== 最后处理其他推理结果 ========== if not reasoning_result: return "llm_call"