Files
ailine/REACT_MODE_SUMMARY.md
root 53fbfb4741
All checks were successful
构建并部署 AI Agent 服务 / deploy (push) Successful in 6m41s
📚 整理并更新文档
- 删除过时文档:REACT_PLAN.md、backend/docs/HYBRID_ROUTER.md
- 更新 REACT_MODE_SUMMARY.md:加入新的混合路由架构
- 更新 README.md:加入混合路由、双模型服务等新特性
- 更新 backend/app/README.md:加入 hybrid_router.py
- 更新 backend/app/model_services/README.md:加入 get_chat_service/get_small_llm_service
- 更新 .gitignore:允许 REACT_MODE_SUMMARY.md 上传
- 新增 backend/test/test_hybrid_router.py:测试脚本
2026-05-03 16:53:34 +08:00

150 lines
5.8 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# React 模式架构总结
---
## ✅ 当前架构:混合路由 + React 循环
本项目采用 **两层混合架构**
```
┌─────────────────────────────────────────────────────────────┐
│ 第一层:前置混合路由(低延迟) │
│ ├─ 规则快速分流(无 LLM
│ ├─ 轻量级意图分类smallLLM
│ └─ 快速路径fast_chitchat, fast_rag, fast_tool
└───────────────────────┬─────────────────────────────────────┘
↓(自动升级:失败时)
┌─────────────────────────────────────────────────────────────┐
│ 第二层:完整 React 循环(兜底,复杂任务处理) │
│ └─ 推理 → 行动 → 观察(最多 40 步) │
└─────────────────────────────────────────────────────────────┘
```
---
## 🎯 第一层:前置混合路由(新)
### 核心功能
| 功能 | 说明 |
|------|------|
| 规则快速分流 | 无 LLM毫秒级响应用于问候、感谢、子图关键词等 |
| 轻量级意图分类 | 使用 smallLLM压缩到 4 类chitchat, knowledge, tool, complex |
| 快速路径 | 三个快速处理节点fast_chitchat, fast_rag, fast_tool |
| 自动升级 | 快速路径失败时,自动回到完整 React 循环 |
| SSE 事件增强 | intent_classified, path_decision, fast_path_*, escalation |
### 快速流程图
```
START
init_state
hybrid_router (前置路由) ←────────────┐
↓ │
├─ 规则分流 → fast_chitchat →────────┤
│ ↓ │
├─ 模型分类 → fast_rag →────────────┤
│ ↓ │
├─ fast_tool →────────┤
│ ↓ │
└─ react_loop →────────┤
↓ │
检查成功/升级? ──────────┘
↓ ↓
finalize react_reason
```
### 关键文件
| 文件 | 说明 |
|------|------|
| `backend/app/main_graph/nodes/hybrid_router.py` | 混合路由完整实现 |
| `backend/app/model_services/chat_services.py` | get_chat_service() + get_small_llm_service() |
| `backend/app/main_graph/utils/main_graph_builder.py` | 集成混合路由到主图 |
### 配置项
```python
# 构建图时可选择
graph = build_react_main_graph(use_hybrid_router=True) # 启用混合路由(默认)
graph = build_react_main_graph(use_hybrid_router=False) # 禁用,纯 React 循环
```
---
## 🎯 第二层:完整 React 循环(保留)
### 核心特性
| 特性 | 说明 |
|------|------|
| 循环推理 | 每轮推理判断下一步,最多 40 步 |
| 结构化错误 | ErrorRecord + ErrorSeverity |
| 超时重试 | RAG 最多 2 次,子图最多 1 次 |
| 子图集成 | contact, dictionary, news_analysis |
| RAG 检索 | 支持重检索re_retrieve |
### 流程图
```
react_reason (推理) ←──────────────────┐
↓ │
条件路由 │
├─→ rag_retrieve (带重试) →──────────┤
├─→ contact_subgraph →───────────────┤
├─→ dictionary_subgraph →────────────┤
├─→ news_analysis_subgraph →─────────┤
├─→ handle_error → (重试或降级) →────┤
└─→ finalize
END
```
---
## 📁 关键文件清单
| 文件 | 说明 |
|------|------|
| `backend/app/main_graph/utils/main_graph_builder.py` | 主图构建(支持混合路由开关) |
| `backend/app/main_graph/nodes/react_nodes.py` | React 循环节点 |
| `backend/app/main_graph/nodes/hybrid_router.py` | 混合路由节点(新) |
| `backend/app/main_graph/nodes/rag_nodes.py` | RAG 检索节点 |
| `backend/app/main_graph/utils/retry_utils.py` | 超时重试工具 |
| `backend/app/main_graph/state.py` | 主状态 |
| `backend/app/core/intent.py` | React 模式意图推理器 |
| `backend/app/model_services/chat_services.py` | 双模型服务llm + smallLLM |
---
## 🚀 快速使用
```python
from backend.app.main_graph.utils.main_graph_builder import build_react_main_graph
# 构建图(默认启用混合路由)
graph = build_react_main_graph(use_hybrid_router=True)
compiled_graph = graph.compile()
# 调用
result = compiled_graph.invoke({"user_query": "你好", "user_id": "test"})
print(result.final_result)
```
---
## 🎉 完整特性总结
✅ 双模型服务 (llm + smallLLM)
✅ 前置混合路由(规则快速分流 + 轻量级意图分类)
✅ 三个快速路径fast_chitchat, fast_rag, fast_tool
✅ 自动升级机制(快速路径失败 → 完整 React 循环)
✅ SSE 事件增强intent_classified, path_decision, fast_path_*, escalation
✅ 完整 React 循环(最多 40 步)
✅ 结构化错误处理
✅ 超时和重试策略
✅ 子图集成contact, dictionary, news_analysis
✅ 向后兼容use_hybrid_router=True/False