This commit is contained in:
153
README.md
153
README.md
@@ -63,8 +63,9 @@
|
||||
| | 服务基类 | base.py | - | BaseServiceProvider + FallbackServiceChain + SingletonServiceManager |
|
||||
| **Embedding** | 向量嵌入 | llama.cpp server | latest | 本地 embedding 服务,支持多种模型 |
|
||||
| **Agent 框架** | 工作流编排 | LangGraph + LangChain | latest | 状态机驱动的智能体工作流 |
|
||||
| **子图系统** | 模块化子图 | agent_subgraphs/ | - | 通讯录、词典、资讯分析等子图 |
|
||||
| | 公共工具 | common/ | - | 状态基类、意图理解、格式化输出、人工审核 |
|
||||
| **子图系统** | 模块化子图 | subgraphs/ | - | 通讯录、词典、资讯分析等子图 |
|
||||
| | 核心工具 | core/ | - | 状态基类、意图理解、格式化输出、人工审核 |
|
||||
| **主图系统** | 主流程 | main_graph/ | - | 主图节点、工具、构建器 |
|
||||
| **向量数据库** | 向量检索 | Qdrant | v1.12+ | 高性能向量相似度检索(远程服务器) |
|
||||
| **后端框架** | API 服务 | FastAPI + Uvicorn | v0.115+ | RESTful API + WebSocket + SSE 流式输出 |
|
||||
| **前端框架** | Web 界面 | Streamlit | v1.40+ | 交互式对话界面,组件化设计 |
|
||||
@@ -110,15 +111,15 @@ graph TB
|
||||
RetrieveMemory -->|存储| PostgreSQL[(PostgreSQL)]
|
||||
Summarize -->|存储| PostgreSQL
|
||||
|
||||
SubgraphAPI -->|路由| Subgraphs[子图系统 agent_subgraphs]
|
||||
SubgraphAPI -->|路由| Subgraphs[子图系统 subgraphs]
|
||||
Subgraphs --> Contact[通讯录子图]
|
||||
Subgraphs --> Dictionary[词典子图]
|
||||
Subgraphs --> NewsAnalysis[资讯分析子图]
|
||||
Subgraphs --> Common[公共工具 common/]
|
||||
Common --> Intent[意图理解 (React)]
|
||||
Common --> HumanReview[人工审核]
|
||||
Common --> Formatter[格式化输出]
|
||||
Common --> StateBase[状态基类]
|
||||
Subgraphs --> Core[核心工具 core/]
|
||||
Core --> Intent[意图理解 (React)]
|
||||
Core --> HumanReview[人工审核]
|
||||
Core --> Formatter[格式化输出]
|
||||
Core --> StateBase[状态基类]
|
||||
|
||||
Contact -->|数据库| ContactDB[(PostgreSQL 联系人)]
|
||||
Dictionary -->|数据库| DictionaryDB[(PostgreSQL 生词本)]
|
||||
@@ -354,53 +355,50 @@ Agent1/
|
||||
│ │ ├── config.py # 配置管理
|
||||
│ │ ├── logger.py # 日志工具
|
||||
│ │ ├── backend.py # FastAPI 后端应用(含子图 API)
|
||||
│ │ ├── agent/
|
||||
│ │ ├── README.md # 后端目录文档
|
||||
│ │ │
|
||||
│ │ ├── core/ # ⭐ 核心模块 - 基类和通用工具
|
||||
│ │ │ ├── __init__.py
|
||||
│ │ │ ├── state_base.py # 子图状态基类
|
||||
│ │ │ ├── intent.py # 意图理解(React 模式)
|
||||
│ │ │ ├── intent_classifier.py # 意图分类器
|
||||
│ │ │ ├── formatter.py # 格式化输出工具
|
||||
│ │ │ └── human_review.py # 人工审核节点
|
||||
│ │ │
|
||||
│ │ ├── agent/ # ⭐ Agent 服务层
|
||||
│ │ │ ├── __init__.py
|
||||
│ │ │ ├── service.py # Agent 服务核心(使用 chat_services)
|
||||
│ │ │ ├── history.py # 历史查询服务
|
||||
│ │ │ ├── prompts.py # 提示模板
|
||||
│ │ │ └── rag_initializer.py # RAG 工具初始化
|
||||
│ │ ├── model_services/ # 模型服务层(新增)
|
||||
│ │ │ ├── __init__.py
|
||||
│ │ │ ├── base.py # 基类:BaseServiceProvider, FallbackServiceChain, SingletonServiceManager
|
||||
│ │ │ ├── chat_services.py # 生成式大模型服务(替代 llm_factory)
|
||||
│ │ │ ├── embedding_services.py # 嵌入模型服务
|
||||
│ │ │ └── rerank_services.py # 重排序服务
|
||||
│ │ ├── graph/
|
||||
│ │ │ └── prompts.py # 提示词模板
|
||||
│ │ │
|
||||
│ │ ├── main_graph/ # ⭐ 主图 - LangGraph 主流程
|
||||
│ │ │ ├── __init__.py
|
||||
│ │ │ ├── state.py # 主图状态定义
|
||||
│ │ │ ├── graph_builder.py # LangGraph 图构建器
|
||||
│ │ │ ├── graph_tools.py # 工具定义
|
||||
│ │ │ ├── state.py # 状态定义
|
||||
│ │ │ ├── rag_nodes.py # RAG 集成节点
|
||||
│ │ │ └── retrieve_memory.py # 记忆检索
|
||||
│ │ ├── nodes/
|
||||
│ │ │ ├── nodes/ # 主图节点
|
||||
│ │ │ │ ├── __init__.py
|
||||
│ │ │ │ ├── router.py # 路由决策
|
||||
│ │ │ │ ├── llm_call.py # LLM 调用节点(React 模式)
|
||||
│ │ │ │ ├── tool_call.py # 工具执行节点
|
||||
│ │ │ │ ├── retrieve_memory.py # 记忆检索节点
|
||||
│ │ │ │ ├── summarize.py # 记忆存储节点
|
||||
│ │ │ │ ├── finalize.py # 最终处理节点
|
||||
│ │ │ │ ├── memory_trigger.py # 记忆触发节点
|
||||
│ │ │ │ ├── rag_nodes.py # RAG 集成节点
|
||||
│ │ │ │ └── react_nodes.py # React 模式节点
|
||||
│ │ │ ├── tools/ # 主图工具
|
||||
│ │ │ │ ├── __init__.py
|
||||
│ │ │ │ ├── graph_tools.py # 工具定义
|
||||
│ │ │ │ └── subgraph_tools.py # 子图调用工具
|
||||
│ │ │ └── utils/ # 主图工具函数
|
||||
│ │ │ ├── __init__.py
|
||||
│ │ │ ├── retry_utils.py # 重试工具
|
||||
│ │ │ ├── subgraph_builder.py # 子图构建器
|
||||
│ │ │ ├── rag_initializer.py # RAG 初始化工具
|
||||
│ │ │ └── visualize_graph.py # 图可视化工具
|
||||
│ │ │
|
||||
│ │ ├── subgraphs/ # ⭐ 子图模块
|
||||
│ │ │ ├── __init__.py
|
||||
│ │ │ ├── router.py # 路由决策
|
||||
│ │ │ ├── llm_call.py # LLM 调用节点(React 模式)
|
||||
│ │ │ ├── tool_call.py # 工具执行节点
|
||||
│ │ │ ├── retrieve_memory.py # 记忆检索节点
|
||||
│ │ │ ├── summarize.py # 记忆存储节点
|
||||
│ │ │ ├── finalize.py # 最终处理节点
|
||||
│ │ │ └── memory_trigger.py # 记忆触发节点
|
||||
│ │ ├── memory/
|
||||
│ │ │ ├── __init__.py
|
||||
│ │ │ └── mem0_client.py # Mem0 客户端封装
|
||||
│ │ ├── rag/
|
||||
│ │ │ ├── __init__.py
|
||||
│ │ │ ├── retriever.py # 检索器
|
||||
│ │ │ ├── rerank.py # 重排序业务逻辑(使用 rerank_services)
|
||||
│ │ │ ├── query_transform.py # 查询转换
|
||||
│ │ │ ├── pipeline.py # RAG 流水线
|
||||
│ │ │ ├── fusion.py # RAG-Fusion
|
||||
│ │ │ └── tools.py # RAG 工具
|
||||
│ │ ├── agent_subgraphs/ # 子图模块(新增)
|
||||
│ │ │ ├── __init__.py
|
||||
│ │ │ ├── README.md # 子图文档
|
||||
│ │ │ ├── common/ # 公共工具
|
||||
│ │ │ │ ├── state_base.py # 状态基类
|
||||
│ │ │ │ ├── intent.py # 意图理解(React 模式)
|
||||
│ │ │ │ ├── formatter.py # 格式化输出工具
|
||||
│ │ │ │ └── human_review.py # 人工审核节点
|
||||
│ │ │ ├── contact/ # 通讯录子图
|
||||
│ │ │ │ ├── state.py # 状态定义
|
||||
│ │ │ │ ├── nodes.py # 节点实现
|
||||
@@ -413,14 +411,41 @@ Agent1/
|
||||
│ │ │ │ ├── graph.py # 图构建
|
||||
│ │ │ │ ├── api_client.py # API 客户端
|
||||
│ │ │ │ └── __init__.py
|
||||
│ │ │ ├── news_analysis/ # 资讯分析子图
|
||||
│ │ │ │ ├── state.py # 状态定义
|
||||
│ │ │ │ ├── nodes.py # 节点实现
|
||||
│ │ │ │ ├── graph.py # 图构建
|
||||
│ │ │ │ ├── api_client.py # API 客户端
|
||||
│ │ │ │ └── __init__.py
|
||||
│ │ │ └── research/ # 研究分析子图(规划中)
|
||||
│ │ └── utils/
|
||||
│ │ │ └── news_analysis/ # 资讯分析子图
|
||||
│ │ │ ├── state.py # 状态定义
|
||||
│ │ │ ├── nodes.py # 节点实现
|
||||
│ │ │ ├── graph.py # 图构建
|
||||
│ │ │ ├── api_client.py # API 客户端
|
||||
│ │ │ └── __init__.py
|
||||
│ │ │
|
||||
│ │ ├── model_services/ # 模型服务层
|
||||
│ │ │ ├── __init__.py
|
||||
│ │ │ ├── base.py # 基类:BaseServiceProvider, FallbackServiceChain, SingletonServiceManager
|
||||
│ │ │ ├── chat_services.py # 生成式大模型服务
|
||||
│ │ │ ├── embedding_services.py # 嵌入模型服务
|
||||
│ │ │ └── rerank_services.py # 重排序服务
|
||||
│ │ │
|
||||
│ │ ├── memory/ # 记忆模块
|
||||
│ │ │ ├── __init__.py
|
||||
│ │ │ └── mem0_client.py # Mem0 客户端封装
|
||||
│ │ │
|
||||
│ │ ├── rag/ # RAG 模块
|
||||
│ │ │ ├── __init__.py
|
||||
│ │ │ ├── retriever.py # 检索器
|
||||
│ │ │ ├── rerank.py # 重排序业务逻辑(使用 rerank_services)
|
||||
│ │ │ ├── query_transform.py # 查询转换
|
||||
│ │ │ ├── pipeline.py # RAG 流水线
|
||||
│ │ │ ├── fusion.py # RAG-Fusion
|
||||
│ │ │ ├── tools.py # RAG 工具
|
||||
│ │ │ └── evaluate.py # RAG 评估
|
||||
│ │ │
|
||||
│ │ ├── db/ # 数据库模块
|
||||
│ │ │ ├── __init__.py
|
||||
│ │ │ ├── base.py # 数据库基类
|
||||
│ │ │ ├── models.py # 数据模型
|
||||
│ │ │ └── init_db.py # 数据库初始化
|
||||
│ │ │
|
||||
│ │ └── utils/ # 工具模块
|
||||
│ │ └── __init__.py
|
||||
│ └── rag_core/
|
||||
│ ├── __init__.py
|
||||
@@ -739,8 +764,8 @@ python scripts/evaluate_rag.py
|
||||
4. **可以多次循环** - LLM 可以调用多个工具,或者同一个工具多次
|
||||
|
||||
**实际代码位置**:
|
||||
- `backend/app/graph/graph_builder.py` 第 79 行:`builder.add_edge("tool_node", "llm_call")`
|
||||
- `backend/app/nodes/router.py`:`should_continue` 函数检查 `last_message.tool_calls`
|
||||
- `backend/app/main_graph/graph_builder.py` 第 79 行:`builder.add_edge("tool_node", "llm_call")`
|
||||
- `backend/app/main_graph/nodes/router.py`:`should_continue` 函数检查 `last_message.tool_calls`
|
||||
|
||||
#### 2.2 状态机设计
|
||||
|
||||
@@ -1207,7 +1232,7 @@ sequenceDiagram
|
||||
|
||||
---
|
||||
|
||||
### 公共工具说明
|
||||
### 核心工具说明
|
||||
|
||||
#### 1. state_base.py - 状态基类
|
||||
提供类型安全的状态基类,所有子图状态继承此类。
|
||||
@@ -1239,13 +1264,13 @@ sequenceDiagram
|
||||
|
||||
1. **创建子图目录**
|
||||
```bash
|
||||
mkdir backend/app/agent_subgraphs/my_subgraph
|
||||
mkdir backend/app/subgraphs/my_subgraph
|
||||
```
|
||||
|
||||
2. **创建状态定义 (state.py)**
|
||||
```python
|
||||
from typing_extensions import TypedDict
|
||||
from ..common.state_base import BaseSubgraphState
|
||||
from ..core.state_base import BaseSubgraphState
|
||||
|
||||
class MySubgraphState(BaseSubgraphState):
|
||||
\"\"\"
|
||||
@@ -1545,7 +1570,7 @@ streamlit run frontend/src/frontend_main.py
|
||||
|
||||
### 添加新工具
|
||||
|
||||
在 [backend/app/graph/graph_tools.py](file:///home/huang/Study/AIProject/Agent1/backend/app/graph/graph_tools.py) 中添加新的 `@tool` 装饰函数:
|
||||
在 [backend/app/main_graph/tools/graph_tools.py](file:///home/huang/Study/AIProject/Agent1/backend/app/main_graph/tools/graph_tools.py) 中添加新的 `@tool` 装饰函数:
|
||||
|
||||
```python
|
||||
@tool
|
||||
@@ -1863,7 +1888,7 @@ curl -X POST http://localhost:6333/collections/my_docs/actions \
|
||||
#### 3.1 添加自定义工具
|
||||
|
||||
```python
|
||||
# 在 backend/app/graph/graph_tools.py 中添加
|
||||
# 在 backend/app/main_graph/tools/graph_tools.py 中添加
|
||||
|
||||
from langchain_core.tools import tool
|
||||
from typing import Optional
|
||||
|
||||
Reference in New Issue
Block a user