fix: 彻底修复 RAG 无限循环问题
All checks were successful
构建并部署 AI Agent 服务 / deploy (push) Successful in 5m33s
All checks were successful
构建并部署 AI Agent 服务 / deploy (push) Successful in 5m33s
关键修复: 1. rag_nodes.py: history 里记录的 action 改成大写 RETRIEVE_RAG 2. react_nodes.py: route_by_reasoning 优先检查 DIRECT_RESPONSE 3. 优化路由判断顺序,避免不必要的循环
This commit is contained in:
@@ -234,7 +234,7 @@ async def rag_retrieve_node(state: MainGraphState, config: Optional[Dict[str, An
|
|||||||
# 关键修复:把 rag_retrieve 加到 reasoning_history 里,让下次推理知道
|
# 关键修复:把 rag_retrieve 加到 reasoning_history 里,让下次推理知道
|
||||||
state.reasoning_history.append({
|
state.reasoning_history.append({
|
||||||
"step": state.reasoning_step,
|
"step": state.reasoning_step,
|
||||||
"action": "rag_retrieve",
|
"action": "RETRIEVE_RAG", # 大写,和推理结果保持一致
|
||||||
"confidence": 1.0,
|
"confidence": 1.0,
|
||||||
"reasoning": "RAG 检索完成",
|
"reasoning": "RAG 检索完成",
|
||||||
"timestamp": datetime.now().isoformat()
|
"timestamp": datetime.now().isoformat()
|
||||||
|
|||||||
@@ -359,30 +359,36 @@ def route_by_reasoning(state: MainGraphState) -> str:
|
|||||||
return "rag_retrieve"
|
return "rag_retrieve"
|
||||||
return "react_reason"
|
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]
|
previous_actions = [h.get("action") for h in state.reasoning_history]
|
||||||
|
|
||||||
|
# 检查是否已经执行过子图
|
||||||
if "subgraph_completed" in previous_actions or state.final_result:
|
if "subgraph_completed" in previous_actions or state.final_result:
|
||||||
return "llm_call"
|
return "llm_call"
|
||||||
|
|
||||||
# 关键修复:检测 RAG 重复循环 - 如果发现"RETRIEVE_RAG"出现超过1次,直接去 LLM
|
# 检测 RAG 重复循环 - 如果发现"RETRIEVE_RAG"出现超过1次,直接去 LLM
|
||||||
rag_count = previous_actions.count("RETRIEVE_RAG")
|
rag_count = previous_actions.count("RETRIEVE_RAG")
|
||||||
if rag_count >= 2:
|
if rag_count >= 2:
|
||||||
info(f"[route_by_reasoning] 检测到 RAG 重复循环({rag_count}次),直接去 llm_call")
|
info(f"[route_by_reasoning] 检测到 RAG 重复循环({rag_count}次),直接去 llm_call")
|
||||||
return "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):
|
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")
|
info(f"[route_by_reasoning] 检测到已存在 RAG 检索结果,直接去 llm_call")
|
||||||
return "llm_call"
|
return "llm_call"
|
||||||
|
|
||||||
# 关键修复:限制最多 3 次推理,避免无限循环
|
# 限制最多 3 次推理,避免无限循环
|
||||||
if len(previous_actions) >= 3:
|
if len(previous_actions) >= 3:
|
||||||
info(f"[route_by_reasoning] 已达到最大推理次数 ({len(previous_actions)}),直接去 llm_call")
|
info(f"[route_by_reasoning] 已达到最大推理次数 ({len(previous_actions)}),直接去 llm_call")
|
||||||
return "llm_call"
|
return "llm_call"
|
||||||
|
|
||||||
# 获取推理结果
|
# ========== 最后处理其他推理结果 ==========
|
||||||
reasoning_result: Optional[ReasoningResult] = state.debug_info.get("reasoning_result")
|
|
||||||
|
|
||||||
if not reasoning_result:
|
if not reasoning_result:
|
||||||
return "llm_call"
|
return "llm_call"
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user