采用向量数据库实现长期记忆
Some checks failed
构建并部署 AI Agent 服务 / deploy (push) Has been cancelled

This commit is contained in:
2026-04-15 23:52:13 +08:00
parent de68916c5a
commit a92a220ff3
24 changed files with 1237 additions and 713 deletions

157
README.md
View File

@@ -21,6 +21,7 @@
-**高可用架构**:模型自动降级,确保服务稳定
-**前后端分离**FastAPI 后端 + Streamlit 前端
-**Docker 部署**:一键启动所有服务
-**远程服务架构**PostgreSQL 和 Qdrant 部署在远程服务器
---
@@ -30,13 +31,13 @@
| 层级 | 技术选型 | 说明 |
|------|---------|------|
| **LLM 服务** | 智谱 AI API / vLLM (Gemma-4) | 云端 API 或本地推理 |
| **Embedding** | 智谱 Embedding API | 向量嵌入(无需 PyTorch |
| **LLM 服务** | 智谱 AI API / llama.cpp (Gemma-4 GGUF) | 云端 API 或本地推理 |
| **Embedding** | llama.cpp (embeddinggemma-300M GGUF) | 本地向量嵌入服务 |
| **Agent 框架** | LangGraph + LangChain | 工作流编排 |
| **向量数据库** | ChromaDB / pgvector | RAG 知识检索 |
| **向量数据库** | Qdrant | RAG 知识检索(远程服务器) |
| **后端框架** | FastAPI + Uvicorn | RESTful API + WebSocket |
| **前端框架** | Streamlit | 交互式 Web 界面 |
| **数据库** | PostgreSQL 16 | 对话记忆持久化 |
| **数据库** | PostgreSQL 16 | 对话记忆持久化(远程服务器) |
| **容器化** | Docker + Docker Compose | 服务编排 |
### 架构图
@@ -63,34 +64,52 @@
│ │ - Weather │ │
│ │ - File IO │ │
│ │ - Web Scrap│ │
│ │ - RAG │ │
│ │ - Memory │ │
│ └────────────┘ │
└────────┬─────────┘
┌────┴────┐
↓ ↓
┌────────┐ ┌──────────┐
│PostgreSQL│ │ChromaDB
(记忆存储)│ │(向量检索)
└────────┘ └──────────┘
┌────┴────────────────────
┌──────────────┐ ┌──────────────┐
PostgreSQL │ │ Qdrant
(远程服务器) │ │ (远程服务器)
│ 115.190... │ │ 115.190... │
└──────────────┘ └──────────────┘
```
### 项目结构
```
Agent1/
├── agent.py # Agent 服务核心(多模型管理)
├── graph_builder.py # LangGraph 状态图构建器
├── tools.py # 工具函数定义(@tool 装饰器)
├── backend.py # FastAPI 后端应用
├── frontend.py # Streamlit 前端界面
├── rag_example.py # RAG 实现示例(无 PyTorch
├── docker-compose.yml # Docker 服务编排
├── Dockerfile.backend # 后端镜像构建
├── Dockerfile.frontend # 前端镜像构建
├── requirement.txt # Python 依赖
├── .env # 环境变量配置
└── user_docs/ # 用户文档目录
├── app/
│ ├── __init__.py
│ ├── config.py # 配置管理
│ ├── state.py # 状态定义
├── prompts.py # 提示模板
│ ├── logger.py # 日志工具
│ ├── tools.py # 工具函数定义
│ ├── memory/
│ │ ├── __init__.py
│ │ └── mem0_client.py # Mem0 客户端封装
│ ├── nodes/
├── __init__.py
│ │ ├── router.py # 路由决策
│ │ ├── llm_call.py # LLM 调用节点
│ │ ├── tool_call.py # 工具执行节点
│ │ ├── retrieve_memory.py # 记忆检索节点
│ │ └── summarize.py # 记忆存储节点
│ ├── graph_builder.py # LangGraph 图构建器
│ ├── agent.py # Agent 服务核心
│ └── backend.py # FastAPI 后端应用
├── frontend/
│ └── app.py # Streamlit 前端界面
├── docker/
│ ├── docker-compose.yml # Docker 服务编排
│ ├── Dockerfile.backend # 后端镜像构建
│ └── Dockerfile.frontend # 前端镜像构建
├── requirement.txt # Python 依赖
├── .env # 环境变量配置
└── user_docs/ # 用户文档目录
├── a.txt
├── b.pdf
└── c.xlsx
@@ -104,9 +123,9 @@ Agent1/
### 方式一Docker Compose推荐
```
```bash
# 1. 配置环境变量
cp .env.example .env
cp .env.docker .env
# 编辑 .env 文件,填入真实的 API Key
# 2. 启动所有服务
@@ -121,21 +140,19 @@ docker compose -f docker/docker-compose.yml up -d --build
### 方式二:本地开发模式
```
# 1. 启动 PostgreSQL
docker run -d --name postgres-langgraph \
-e POSTGRES_PASSWORD=mysecretpassword \
-e POSTGRES_DB=langgraph_db \
-p 5432:5432 postgres:16
# 2. 安装依赖
```bash
# 1. 安装依赖
pip install -r requirement.txt
# 2. 配置环境变量
cp .env.docker .env
# 编辑 .env根据本地/远程环境调整配置
# 3. 启动后端
python backend.py
python app/backend.py
# 4. 启动前端(新终端)
streamlit run frontend.py
cd frontend && streamlit run app.py
```
---
@@ -161,7 +178,7 @@ streamlit run frontend.py
| 📑 解析 PDF | "总结 b.pdf 的主要内容" |
| 📊 Excel 数据 | "显示 c.xlsx 的数据" |
| 🌐 网页抓取 | "抓取 https://example.com 的内容" |
| 🔍 知识库检索 | "根据知识库回答XXX" |
| 🔍 长期记忆 | "记住我喜欢吃川菜" → "我有什么饮食偏好?" |
### 多模型切换
@@ -179,9 +196,9 @@ streamlit run frontend.py
### 添加新工具
`tools.py` 中添加新的 `@tool` 装饰函数:
`app/tools.py` 中添加新的 `@tool` 装饰函数:
```
```python
@tool
def my_new_tool(param: str) -> str:
"""
@@ -201,9 +218,9 @@ def my_new_tool(param: str) -> str:
### 添加新模型
`agent.py``initialize()` 方法中添加模型配置:
`app/agent.py``initialize()` 方法中添加模型配置:
```
```python
model_configs = {
"zhipu": self._create_zhipu_llm,
"local": self._create_local_llm,
@@ -215,7 +232,7 @@ model_configs = {
项目包含完整的 Docker 配置:
- **docker-compose.yml**:服务编排(PostgreSQL + Backend + Frontend
- **docker-compose.yml**服务编排Backend + Frontend,连接远程数据库
- **Dockerfile.backend**:后端镜像构建
- **Dockerfile.frontend**:前端镜像构建
- **.gitea/workflows/deploy.yml**CI/CD 自动化部署
@@ -228,54 +245,62 @@ model_configs = {
### 配置文件说明
项目使用两个环境配置文件:
项目采用三层环境配置文件体系
| 文件 | 用途 | 是否提交 Git |
|------|------|------------|
| `.env.example` | 配置模板 | ✅ 是 |
| `.env` | 实际使用的配置 | ❌ 否(已忽略) |
| `.env.docker` | Docker 部署模板 | ✅ 是 |
**使用方法:**
- **本地开发**手动创建 `.env`配置 `localhost` 相关地址
- **Docker 部署**`cp .env.docker .env`然后修改 API Key
- **本地开发**`cp .env.example .env`修改为 localhost 相关地址
- **Docker 部署**`cp .env.docker .env`使用远程服务器地址
### 必需的环境变量
代码中所有使用 `os.getenv()` 的地方都必须在 `.env` 文件中定义:
| 变量名 | 说明 | 本地开发示例 | Docker 部署示例 |
|--------|------|------------|----------------|
| `ZHIPUAI_API_KEY` | 智谱 AI API 密钥 | `your_key_here` | `your_key_here` |
| `VLLM_LOCAL_KEY` | vLLM 认证 Token | `token-abc123` | `token-abc123` |
| `VLLM_BASE_URL` | vLLM 服务地址 | `http://localhost:8000/v1` | `http://115.190.121.151:18000/v1` |
| `DB_URI` | PostgreSQL 连接字符串 | `postgresql://...@localhost:5432/...` | `postgresql://...@postgres:5432/...` |
| `API_URL` | 后端 API 地址 | `http://localhost:8001/chat` | (由 docker-compose.yml 注入) |
| `DEEPSEEK_API_KEY` | DeepSeek API 密钥 | `your_key_here` | `your_key_here` |
| `LLAMACPP_API_KEY` | llama.cpp 认证 Token | `token-abc123` | `token-abc123` |
| `VLLM_BASE_URL` | LLM 服务地址 | `http://localhost:8081/v1` | `http://localhost:8081/v1` |
| `VLLM_EMBEDDING_URL` | Embedding 服务地址 | `http://localhost:8082/v1` | `http://localhost:8082/v1` |
| `QDRANT_URL` | Qdrant 地址 | `http://115.190.121.151:6333` | `http://115.190.121.151:6333` |
| `DB_URI` | PostgreSQL 连接字符串 | `postgresql://...@115.190.121.151:5432/...` | `postgresql://...@115.190.121.151:5432/...` |
| `API_URL` | 后端 API 地址 | `http://localhost:8003/chat` | (由 docker-compose.yml 注入) |
### 配置示例
#### 本地开发 (.env)
```bash
```
ZHIPUAI_API_KEY=your_api_key_here
VLLM_LOCAL_KEY=token-abc123
VLLM_BASE_URL=http://localhost:8000/v1
DB_URI=postgresql://postgres:mysecretpassword@localhost:5432/langgraph_db?sslmode=disable
API_URL=http://localhost:8001/chat
DEEPSEEK_API_KEY=your_deepseek_api_key_here
LLAMACPP_API_KEY=token-abc123
VLLM_BASE_URL=http://localhost:8081/v1
VLLM_EMBEDDING_URL=http://localhost:8082/v1
QDRANT_URL=http://115.190.121.151:6333
DB_URI=postgresql://postgres:mysecretpassword@115.190.121.151:5432/langgraph_db?sslmode=disable
API_URL=http://localhost:8003/chat
```
#### Docker 部署 (.env.docker)
```bash
```
ZHIPUAI_API_KEY=your_api_key_here
VLLM_LOCAL_KEY=token-abc123
VLLM_BASE_URL=http://115.190.121.151:18000/v1
DB_URI=postgresql://postgres:mysecretpassword@postgres:5432/langgraph_db?sslmode=disable
# API_URL 在 docker-compose.yml 中配置为 http://backend:8001/chat
DEEPSEEK_API_KEY=your_deepseek_api_key_here
LLAMACPP_API_KEY=token-abc123
VLLM_BASE_URL=http://localhost:8081/v1
VLLM_EMBEDDING_URL=http://localhost:8082/v1
QDRANT_URL=http://115.190.121.151:6333
DB_URI=postgresql://postgres:mysecretpassword@115.190.121.151:5432/langgraph_db?sslmode=disable
# API_URL 在 docker-compose.yml 中配置为 http://backend:8003/chat
```
### 注意事项
- ⚠️ **不要硬编码敏感信息**:所有 API Key 必须通过环境变量配置
- ⚠️ **Docker 网络差异**:容器内使用服务名(如 `postgres``backend`),本地使用 `localhost`
- ⚠️ **远程服务依赖**:确保可以访问远程 PostgreSQL (115.190.121.151:5432) 和 Qdrant (115.190.121.151:6333)
- ⚠️ **修改后重启**:修改 `.env`Docker 部署需要执行 `docker compose down && docker compose up -d --build`
---
@@ -284,13 +309,13 @@ DB_URI=postgresql://postgres:mysecretpassword@postgres:5432/langgraph_db?sslmode
### 常见问题
**Q: 无法连接 PostgreSQL**
**Q: 无法连接远程数据库**
```bash
# 检查容器状态
docker ps | grep postgres
# 测试 PostgreSQL
psql -h 115.190.121.151 -U postgres -d langgraph_db -c "SELECT version();"
# 查看日志
docker logs postgres-langgraph
# 测试 Qdrant
curl http://115.190.121.151:6333/collections
```
**Q: 后端启动失败?**