完整的混合路由优化系统
All checks were successful
构建并部署 AI Agent 服务 / deploy (push) Successful in 6m8s

1. 双模型服务 (llm + smallLLM)
   - 增加 get_small_llm_service() 函数
   - 支持智谱/DeepSeek 小模型作为轻量级选项

2. 前置混合路由
   - 规则快速分流(无 LLM,超快速)
   - 轻量级意图分类(smallLLM)
   - 快速路径:fast_chitchat, fast_rag, fast_tool

3. 自动升级机制
   - 快速路径失败 → 自动回到 React 循环
   - SSE 事件增强:intent_classified, path_decision, fast_path_*, escalation

4. 向后兼容
   - build_react_main_graph(use_hybrid_router=True/False)
   - 可选择启用或禁用混合路由

5. 更新 intent.py
   - 支持 use_small_llm 参数
   - 保留原有完整功能供 React 循环使用
This commit is contained in:
2026-05-03 16:45:46 +08:00
parent 9c53f58165
commit a5fc9cd5d8
5 changed files with 928 additions and 63 deletions

View File

@@ -14,6 +14,14 @@ from app.main_graph.nodes.react_nodes import (
error_handling_node,
route_by_reasoning
)
from app.main_graph.nodes.hybrid_router import (
hybrid_router_node,
fast_chitchat_node,
fast_rag_node,
fast_tool_node,
route_from_hybrid_decision,
check_fast_path_success
)
from app.main_graph.nodes.llm_call import create_llm_call_node
from app.main_graph.nodes.rag_nodes import rag_retrieve_node
from app.main_graph.nodes.retrieve_memory import create_retrieve_memory_node
@@ -173,39 +181,20 @@ def wrap_subgraph_for_error_handling(subgraph, name: str):
return wrapped_node
# ========== 主图构建 ==========
def build_react_main_graph(llm=None, tools=None, mem0_client=None) -> StateGraph:
def build_react_main_graph(llm=None, tools=None, mem0_client=None, use_hybrid_router: bool = True) -> StateGraph:
"""
构建整合后的完整主图
构建整合后的完整主图(支持混合路由)
完整流程:
START
retrieve_memory (从Mem0检索长期记忆)
memory_trigger (记忆触发器)
init_state (初始化)
react_reason (推理) ←───────────────────────┐
↓ │
条件路由 │
├─ rag_retrieve →─────────────────────────┤
├─ contact_subgraph →─────────────────────┤
├─ dictionary_subgraph →──────────────────┤
├─ news_analysis_subgraph →───────────────┤
├─ web_search →───────────────────────────┤
├─ handle_error → (重试或结束) ────────────┤
└─ llm_call (大模型调用) ←────────────────┘
检查:需要总结吗?
├─ 是 → summarize (提交给Mem0存储)
└─ 否 → (跳过)
finalize (发送完成事件)
END
Args:
llm: LangChain ChatModel 实例
tools: 工具列表
mem0_client: Mem0 客户端实例
use_hybrid_router: 是否使用混合路由(快速路径 + React 循环)
Returns:
StateGraph: 构建好的图
"""
# 创建图
graph = StateGraph(MainGraphState)
@@ -232,8 +221,17 @@ def build_react_main_graph(llm=None, tools=None, mem0_client=None) -> StateGraph
graph.add_node("retrieve_memory", retrieve_memory_node)
graph.add_node("memory_trigger", memory_trigger_node)
# 第二阶段:React 循环推理
# 第二阶段:初始化
graph.add_node("init_state", init_state_node)
# ========== 混合路由节点(如果启用) ==========
if use_hybrid_router:
graph.add_node("hybrid_router", hybrid_router_node)
graph.add_node("fast_chitchat", fast_chitchat_node)
graph.add_node("fast_rag", fast_rag_node)
graph.add_node("fast_tool", fast_tool_node)
# 第三阶段React 循环推理(始终保留)
graph.add_node("react_reason", react_reason_node)
graph.add_node("rag_retrieve", rag_retrieve_node)
graph.add_node("web_search", web_search_node)
@@ -260,25 +258,57 @@ def build_react_main_graph(llm=None, tools=None, mem0_client=None) -> StateGraph
wrap_subgraph_for_error_handling(news_analysis_graph.compile(), "news_analysis")
)
# 第阶段:完成处理
# 第阶段:完成处理
if summarize_node:
graph.add_node("summarize", summarize_node)
graph.add_node("finalize", finalize_node)
# ========== 添加边 ==========
# 第一阶段:记忆检索
if retrieve_memory_node:
graph.add_edge(START, "retrieve_memory")
graph.add_edge("retrieve_memory", "memory_trigger")
else:
graph.add_edge(START, "memory_trigger")
# 进入第二阶段
# 进入初始化
graph.add_edge("memory_trigger", "init_state")
graph.add_edge("init_state", "react_reason")
# 第二阶段React 循环推理
# ========== 混合路由分支(如果启用) ==========
if use_hybrid_router:
graph.add_edge("init_state", "hybrid_router")
# 从 hybrid_router 条件分支
graph.add_conditional_edges(
"hybrid_router",
route_from_hybrid_decision,
{
"fast_chitchat": "fast_chitchat",
"fast_rag": "fast_rag",
"fast_tool": "fast_tool",
"react_loop": "react_reason"
}
)
# 快速路径的完成检查
for fast_node in ["fast_chitchat", "fast_rag", "fast_tool"]:
graph.add_conditional_edges(
fast_node,
check_fast_path_success,
{
"success": "finalize",
"escalate": "react_reason"
}
)
info(f"✅ [图构建] 混合路由模式已启用")
else:
# 无混合路由,直接到 react_reason
graph.add_edge("init_state", "react_reason")
info(f"✅ [图构建] 纯 React 模式")
# ========== React 循环边(始终保留) ==========
graph.add_conditional_edges(
"react_reason",
route_by_reasoning,
@@ -292,8 +322,8 @@ def build_react_main_graph(llm=None, tools=None, mem0_client=None) -> StateGraph
"llm_call": "llm_call"
}
)
# 循环边rag、web_search、子图、error都回到reason
# 循环边rag、web_search、子图、error都回到 reason
graph.add_edge("rag_retrieve", "react_reason")
graph.add_edge("web_search", "react_reason")
graph.add_edge("contact_subgraph", "react_reason")
@@ -301,7 +331,7 @@ def build_react_main_graph(llm=None, tools=None, mem0_client=None) -> StateGraph
graph.add_edge("news_analysis_subgraph", "react_reason")
graph.add_edge("handle_error", "react_reason")
# 第三阶段llm_call 后进入完成处理
# ========== 最终完成阶段 ==========
if llm_node is not None:
if summarize_node:
# 检查是否需要总结
@@ -321,7 +351,7 @@ def build_react_main_graph(llm=None, tools=None, mem0_client=None) -> StateGraph
# 完成
graph.add_edge("finalize", END)
info("✅ [图构建] 整合后的完整主图构建完成")
info(f"✅ [图构建] 整合后的完整主图构建完成(混合路由: {use_hybrid_router}")
return graph