feat: 实现 React 模式循环推理,带超时重试和结构化错误处理
Some checks failed
构建并部署 AI Agent 服务 / deploy (push) Failing after 6m15s
Some checks failed
构建并部署 AI Agent 服务 / deploy (push) Failing after 6m15s
- 更新 intent.py 为 React 模式推理器 - 新增 react_nodes.py: React 模式节点 - 新增 retry_utils.py: 超时和重试工具 - 更新 state.py: 支持循环步数和错误记录 - 重写 subgraph_builder.py: 完整 React 循环流程 - 结构化错误输出,符合 Agent 执行循环最佳实践 - 限制最大推理步数 ≤40,防止无限循环 - RAG 检索带重试和超时保护 - 子图错误可传递给主图处理
This commit is contained in:
@@ -1,157 +1,193 @@
|
||||
"""
|
||||
子图整合主图构建器
|
||||
Subgraph Integration Main Graph Builder
|
||||
React 模式主图构建器 - 完整循环推理版本
|
||||
Main Graph Builder - Full React Mode with Loop Reasoning
|
||||
"""
|
||||
|
||||
from langgraph.graph import StateGraph, START, END
|
||||
from typing import Dict, Any
|
||||
|
||||
from .state import MainGraphState, CurrentAction
|
||||
from .react_nodes import (
|
||||
init_state_node,
|
||||
react_reason_node,
|
||||
rag_retrieve_node,
|
||||
error_handling_node,
|
||||
final_response_node,
|
||||
route_by_reasoning
|
||||
)
|
||||
from ..agent_subgraphs.contact import build_contact_subgraph
|
||||
from ..agent_subgraphs.dictionary import build_dictionary_subgraph
|
||||
from ..agent_subgraphs.news_analysis import build_news_analysis_subgraph
|
||||
|
||||
|
||||
def parse_user_intent(state: MainGraphState) -> MainGraphState:
|
||||
# ========== 子图包装器(处理子图错误传递) ==========
|
||||
def wrap_subgraph_for_error_handling(subgraph, name: str):
|
||||
"""
|
||||
解析用户意图节点
|
||||
包装子图,使其错误能传递给主图
|
||||
|
||||
确定该路由到哪个子图
|
||||
Args:
|
||||
subgraph: 编译好的子图
|
||||
name: 子图名称(用于错误标识)
|
||||
|
||||
Returns: 包装后的节点函数
|
||||
"""
|
||||
state.current_phase = "intent_parsing"
|
||||
def wrapped_node(state: MainGraphState) -> MainGraphState:
|
||||
try:
|
||||
# 调用子图
|
||||
result = subgraph.invoke(state)
|
||||
|
||||
# 更新主图状态
|
||||
if name == "contact":
|
||||
state.contact_result = result
|
||||
elif name == "dictionary":
|
||||
state.dictionary_result = result
|
||||
elif name == "news_analysis":
|
||||
state.news_result = result
|
||||
|
||||
# 标记成功
|
||||
state.success = True
|
||||
return state
|
||||
|
||||
except Exception as e:
|
||||
# 捕获子图错误,传递给主图
|
||||
from .state import ErrorRecord, ErrorSeverity
|
||||
from datetime import datetime
|
||||
|
||||
error_record = ErrorRecord(
|
||||
error_type=f"{name}SubgraphError",
|
||||
error_message=str(e),
|
||||
severity=ErrorSeverity.WARNING,
|
||||
source=f"{name}_subgraph",
|
||||
timestamp=datetime.now().isoformat(),
|
||||
retry_count=0,
|
||||
max_retries=1,
|
||||
context={"user_query": state.user_query}
|
||||
)
|
||||
state.errors.append(error_record)
|
||||
state.current_error = error_record
|
||||
state.current_phase = "error_handling"
|
||||
state.success = False
|
||||
|
||||
return state
|
||||
|
||||
# 从messages中提取用户查询(如果user_query为空)
|
||||
if not state.user_query and state.messages:
|
||||
# 获取最后一条消息的内容
|
||||
last_msg = state.messages[-1]
|
||||
state.user_query = last_msg.content
|
||||
|
||||
query_lower = state.user_query.lower()
|
||||
|
||||
# 简单的关键词匹配
|
||||
if any(keyword in query_lower for keyword in ["通讯录", "联系人", "contact", "email"]):
|
||||
state.current_action = CurrentAction.CONTACT
|
||||
state.intent_confidence = 0.9
|
||||
|
||||
elif any(keyword in query_lower for keyword in ["词典", "单词", "翻译", "dictionary", "translate"]):
|
||||
state.current_action = CurrentAction.DICTIONARY
|
||||
state.intent_confidence = 0.9
|
||||
|
||||
elif any(keyword in query_lower for keyword in ["资讯", "新闻", "分析", "news", "report"]):
|
||||
state.current_action = CurrentAction.NEWS_ANALYSIS
|
||||
state.intent_confidence = 0.9
|
||||
|
||||
else:
|
||||
# 默认是普通聊天
|
||||
state.current_action = CurrentAction.GENERAL_CHAT
|
||||
state.intent_confidence = 0.8
|
||||
|
||||
return state
|
||||
return wrapped_node
|
||||
|
||||
|
||||
def route_to_subgraph(state: MainGraphState) -> str:
|
||||
# ========== 主图构建 ==========
|
||||
def build_react_main_graph() -> StateGraph:
|
||||
"""
|
||||
条件路由:决定路由到哪个子图
|
||||
"""
|
||||
if state.current_action == CurrentAction.NONE:
|
||||
return "general_chat"
|
||||
elif state.current_action == CurrentAction.GENERAL_CHAT:
|
||||
return "general_chat"
|
||||
elif state.current_action == CurrentAction.CONTACT:
|
||||
return "contact_subgraph"
|
||||
elif state.current_action == CurrentAction.DICTIONARY:
|
||||
return "dictionary_subgraph"
|
||||
elif state.current_action == CurrentAction.NEWS_ANALYSIS:
|
||||
return "news_analysis_subgraph"
|
||||
else:
|
||||
return "general_chat"
|
||||
|
||||
|
||||
def general_chat_node(state: MainGraphState) -> MainGraphState:
|
||||
"""
|
||||
普通聊天节点
|
||||
(目前是占位符,后续整合旧的LLM调用逻辑)
|
||||
"""
|
||||
state.current_phase = "general_chat"
|
||||
state.final_result = f"普通聊天模式:{state.user_query}"
|
||||
state.success = True
|
||||
return state
|
||||
|
||||
|
||||
def integrate_results(state: MainGraphState) -> MainGraphState:
|
||||
"""
|
||||
整合子图结果节点
|
||||
"""
|
||||
state.current_phase = "integrating"
|
||||
构建完整的 React 模式主图
|
||||
|
||||
# 整合通讯录子图结果
|
||||
if state.contact_result:
|
||||
state.final_result = state.contact_result.get("final_result", "")
|
||||
|
||||
# 整合词典子图结果
|
||||
elif state.dictionary_result:
|
||||
state.final_result = state.dictionary_result.get("final_result", "")
|
||||
|
||||
# 整合资讯子图结果
|
||||
elif state.news_result:
|
||||
state.final_result = state.news_result.get("final_result", "")
|
||||
|
||||
else:
|
||||
# 没有子图结果
|
||||
if not state.final_result:
|
||||
state.final_result = "处理完成"
|
||||
|
||||
state.current_phase = "done"
|
||||
return state
|
||||
|
||||
|
||||
def build_main_graph() -> StateGraph:
|
||||
"""
|
||||
构建整合了子图的主图
|
||||
|
||||
Returns:
|
||||
配置好的 StateGraph
|
||||
流程:
|
||||
START
|
||||
↓
|
||||
init_state (初始化)
|
||||
↓
|
||||
react_reason (推理) ←──────────────┐
|
||||
↓ │
|
||||
条件路由 │
|
||||
├─→ rag_retrieve →───────────────┤
|
||||
├─→ contact_subgraph →───────────┤
|
||||
├─→ dictionary_subgraph →────────┤
|
||||
├─→ news_analysis_subgraph →─────┤
|
||||
├─→ handle_error → (重试或结束) ──┤
|
||||
└─→ final_response
|
||||
↓
|
||||
END
|
||||
"""
|
||||
# 创建图
|
||||
graph = StateGraph(MainGraphState)
|
||||
|
||||
# 添加节点
|
||||
graph.add_node("parse_intent", parse_user_intent)
|
||||
graph.add_node("general_chat", general_chat_node)
|
||||
graph.add_node("integrate_results", integrate_results)
|
||||
# ========== 添加节点 ==========
|
||||
|
||||
# 添加子图节点
|
||||
# 1. 初始化节点
|
||||
graph.add_node("init_state", init_state_node)
|
||||
|
||||
# 2. React 推理节点
|
||||
graph.add_node("react_reason", react_reason_node)
|
||||
|
||||
# 3. RAG 检索节点
|
||||
graph.add_node("rag_retrieve", rag_retrieve_node)
|
||||
|
||||
# 4. 错误处理节点
|
||||
graph.add_node("handle_error", error_handling_node)
|
||||
|
||||
# 5. 最终回答节点
|
||||
graph.add_node("final_response", final_response_node)
|
||||
|
||||
# ========== 添加子图节点 ==========
|
||||
|
||||
# 构建并包装子图(带错误处理)
|
||||
contact_graph = build_contact_subgraph()
|
||||
dictionary_graph = build_dictionary_subgraph()
|
||||
news_analysis_graph = build_news_analysis_subgraph()
|
||||
|
||||
graph.add_node("contact_subgraph", contact_graph.compile())
|
||||
graph.add_node("dictionary_subgraph", dictionary_graph.compile())
|
||||
graph.add_node("news_analysis_subgraph", news_analysis_graph.compile())
|
||||
graph.add_node(
|
||||
"contact_subgraph",
|
||||
wrap_subgraph_for_error_handling(contact_graph.compile(), "contact")
|
||||
)
|
||||
graph.add_node(
|
||||
"dictionary_subgraph",
|
||||
wrap_subgraph_for_error_handling(dictionary_graph.compile(), "dictionary")
|
||||
)
|
||||
graph.add_node(
|
||||
"news_analysis_subgraph",
|
||||
wrap_subgraph_for_error_handling(news_analysis_graph.compile(), "news_analysis")
|
||||
)
|
||||
|
||||
# 添加边
|
||||
# 从START开始
|
||||
graph.add_edge(START, "parse_intent")
|
||||
# ========== 添加边 ==========
|
||||
|
||||
# 从parse_intent根据条件路由
|
||||
# 1. START → init_state
|
||||
graph.add_edge(START, "init_state")
|
||||
|
||||
# 2. init_state → react_reason
|
||||
graph.add_edge("init_state", "react_reason")
|
||||
|
||||
# 3. 条件路由:react_reason → 各分支
|
||||
graph.add_conditional_edges(
|
||||
"parse_intent",
|
||||
route_to_subgraph,
|
||||
"react_reason",
|
||||
route_by_reasoning,
|
||||
{
|
||||
"general_chat": "general_chat",
|
||||
# 检索分支 → 检索后回到推理
|
||||
"rag_retrieve": "rag_retrieve",
|
||||
|
||||
# 子图分支 → 子图后回到推理
|
||||
"contact_subgraph": "contact_subgraph",
|
||||
"dictionary_subgraph": "dictionary_subgraph",
|
||||
"news_analysis_subgraph": "news_analysis_subgraph",
|
||||
|
||||
# 错误处理分支
|
||||
"handle_error": "handle_error",
|
||||
|
||||
# 最终回答分支
|
||||
"final_response": "final_response",
|
||||
}
|
||||
)
|
||||
|
||||
# 从普通聊天和子图到结果整合
|
||||
graph.add_edge("general_chat", "integrate_results")
|
||||
graph.add_edge("contact_subgraph", "integrate_results")
|
||||
graph.add_edge("dictionary_subgraph", "integrate_results")
|
||||
graph.add_edge("news_analysis_subgraph", "integrate_results")
|
||||
# 4. 循环边:检索/子图/错误处理 后 → 回到推理
|
||||
graph.add_edge("rag_retrieve", "react_reason")
|
||||
graph.add_edge("contact_subgraph", "react_reason")
|
||||
graph.add_edge("dictionary_subgraph", "react_reason")
|
||||
graph.add_edge("news_analysis_subgraph", "react_reason")
|
||||
graph.add_edge("handle_error", "react_reason") # 错误处理后可能重试
|
||||
|
||||
# 最终到END
|
||||
graph.add_edge("integrate_results", END)
|
||||
# 5. 最终边:final_response → END
|
||||
graph.add_edge("final_response", END)
|
||||
|
||||
return graph
|
||||
|
||||
|
||||
# ========== 兼容性:保留旧的函数名 ==========
|
||||
def build_main_graph() -> StateGraph:
|
||||
"""
|
||||
兼容性函数:旧代码调用 build_main_graph() 时返回 React 版本
|
||||
"""
|
||||
return build_react_main_graph()
|
||||
|
||||
|
||||
# ========== 导出 ==========
|
||||
__all__ = [
|
||||
"build_react_main_graph",
|
||||
"build_main_graph",
|
||||
"wrap_subgraph_for_error_handling"
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user