修复循环推理bug
This commit is contained in:
95
backend/app/main_graph/nodes/error_handling.py
Normal file
95
backend/app/main_graph/nodes/error_handling.py
Normal file
@@ -0,0 +1,95 @@
|
||||
"""
|
||||
错误处理节点 - 处理子图/工具调用错误
|
||||
"""
|
||||
|
||||
from app.main_graph.state import MainGraphState, ErrorSeverity
|
||||
from app.logger import info
|
||||
|
||||
|
||||
def error_handling_node(state: MainGraphState) -> MainGraphState:
|
||||
"""
|
||||
错误处理节点:处理子图/工具调用错误
|
||||
|
||||
返回结构化错误信息,格式如下:
|
||||
{
|
||||
"tool/node": "...",
|
||||
"status": "failed",
|
||||
"error": "...",
|
||||
"retries_exceeded": true/false,
|
||||
"suggestion": "..."
|
||||
}
|
||||
"""
|
||||
state.current_phase = "error_handling"
|
||||
|
||||
if not state.current_error:
|
||||
state.current_phase = "react_reasoning"
|
||||
return state
|
||||
|
||||
error = state.current_error
|
||||
|
||||
# 更新错误状态
|
||||
state.error_message = f"{error.error_type}: {error.error_message}"
|
||||
|
||||
# 记录结构化错误信息
|
||||
structured_error = {
|
||||
"tool": error.source,
|
||||
"status": "failed",
|
||||
"error": error.error_message,
|
||||
"retries_exceeded": error.retry_count >= error.max_retries,
|
||||
"retry_count": error.retry_count,
|
||||
"max_retries": error.max_retries
|
||||
}
|
||||
|
||||
# 根据错误类型添加建议
|
||||
if "RAG" in error.error_type:
|
||||
structured_error["suggestion"] = "尝试重新表述问题或直接询问"
|
||||
elif "subgraph" in error.source or "contact" in error.source:
|
||||
structured_error["suggestion"] = "子图执行失败,请尝试简化查询"
|
||||
elif "timeout" in error.error_message.lower():
|
||||
structured_error["suggestion"] = "请求超时,请稍后再试"
|
||||
else:
|
||||
structured_error["suggestion"] = "请尝试其他方式提问"
|
||||
|
||||
state.debug_info["structured_error"] = structured_error
|
||||
|
||||
# 策略1: 检查是否可以重试
|
||||
can_retry = (
|
||||
error.severity in [ErrorSeverity.WARNING, ErrorSeverity.ERROR]
|
||||
and error.retry_count < error.max_retries
|
||||
)
|
||||
|
||||
if can_retry:
|
||||
error.retry_count += 1
|
||||
state.retry_action = error.source
|
||||
state.debug_info["retry_count"] = error.retry_count
|
||||
|
||||
if "RAG" in error.error_type:
|
||||
state.last_action = "RE_RETRIEVE_RAG"
|
||||
elif "subgraph" in error.source:
|
||||
state.last_action = "DIRECT_RESPONSE"
|
||||
else:
|
||||
state.last_action = "REASON"
|
||||
|
||||
state.current_phase = "retrying"
|
||||
return state
|
||||
|
||||
# 策略2: 无法重试,尝试降级方案
|
||||
if error.severity != ErrorSeverity.FATAL:
|
||||
state.final_result = (
|
||||
f"⚠️ 遇到一些问题:\n"
|
||||
f"```json\n{structured_error}\n```\n"
|
||||
f"但我会尽力用现有信息回答您。"
|
||||
)
|
||||
state.success = True
|
||||
state.current_phase = "finalizing"
|
||||
return state
|
||||
|
||||
# 策略3: 致命错误
|
||||
state.final_result = (
|
||||
f"❌ 服务暂时不可用,请稍后再试。\n"
|
||||
f"```json\n{structured_error}\n```"
|
||||
)
|
||||
state.success = False
|
||||
state.current_phase = "finalizing"
|
||||
|
||||
return state
|
||||
Reference in New Issue
Block a user