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"