|
| 1 | +# 微服务智能对话项目 |
| 2 | + |
| 3 | +## 项目简介 |
| 4 | + |
| 5 | +基于 FastAPI + LangChain构建的智能对话系统,集成了RAG技术,能够基于文档内容进行智能问答。 |
| 6 | + |
| 7 | +## 技术栈 |
| 8 | + |
| 9 | +### 后端技术栈 |
| 10 | +- **FastAPI**: 高性能的 Python Web 框架 |
| 11 | +- **LangChain**: 大语言模型应用开发框架 |
| 12 | +- **ChromaDB**: 轻量级向量数据库,用于存储和检索文档嵌入 |
| 13 | +- **Django**: 用于用户认证和管理 |
| 14 | +- **DashScope API**: 提供大语言模型和嵌入模型服务 |
| 15 | + |
| 16 | +### 前端技术栈 |
| 17 | +- **Vue 3**: 前端框架 |
| 18 | +- **Vite**: 构建工具 |
| 19 | +- **Vue Router**: 路由管理 |
| 20 | +- **Pinia**: 状态管理 |
| 21 | +- **i18n**: 国际化支持 |
| 22 | + |
| 23 | +## 项目亮点 |
| 24 | + |
| 25 | +1. **RAG 技术**: 结合文档检索和大语言模型,提供基于文档的智能问答 |
| 26 | +2. **会话持久化**: 使用mysql存储会话历史,可长期保存 |
| 27 | +3. **多语言支持**: 前端集成 i18n,支持中英文切换 |
| 28 | +4. **模块化设计**: 清晰的代码结构,易于维护和扩展 |
| 29 | +5. **向量数据库**: 使用 ChromaDB 实现高效的文档检索 |
| 30 | +6. **微服务架构**: 分离的用户服务和对话服务 |
| 31 | + |
| 32 | +## 项目结构 |
| 33 | + |
| 34 | +``` |
| 35 | +├── backend/ # FastAPI 后端服务 |
| 36 | +│ ├── app/ # 应用代码 |
| 37 | +│ │ ├── agent/ # 智能代理 |
| 38 | +│ │ ├── config/ # 配置文件 |
| 39 | +│ │ ├── model/ # 数据模型 |
| 40 | +│ │ ├── prompt/ # 提示词模板 |
| 41 | +│ │ ├── rag/ # RAG 相关功能 |
| 42 | +│ │ ├── router/ # API 路由 |
| 43 | +│ │ ├── services/ # 服务层 |
| 44 | +│ │ └── utils/ # 工具函数 |
| 45 | +│ ├── data/ # 数据文件 |
| 46 | +│ ├── main.py # 应用入口 |
| 47 | +│ └── requirements.txt |
| 48 | +├── front/ # Vue 前端 |
| 49 | +│ ├── src/ # 源代码 |
| 50 | +│ ├── public/ # 静态资源 |
| 51 | +│ └── package.json |
| 52 | +├── DjangoUserService/ # Django 用户服务 |
| 53 | +└── README.md # 项目说明 |
| 54 | +``` |
| 55 | + |
| 56 | +## 快速开始 |
| 57 | + |
| 58 | +### 1. 环境准备 |
| 59 | + |
| 60 | +#### 后端环境 |
| 61 | +- Python 3.12 |
| 62 | +- pip |
| 63 | + |
| 64 | +#### 前端环境 |
| 65 | +- Node.js 16+ |
| 66 | +- npm 或 pnpm |
| 67 | + |
| 68 | +### 克隆项目 |
| 69 | + |
| 70 | +```bash |
| 71 | +git clone https://github.com/RMA-MUN/LangChain-RAG-FastAPI-Service.git |
| 72 | +cd LangChain-RAG-FastAPI-Service |
| 73 | +``` |
| 74 | + |
| 75 | +### 2. 安装依赖 |
| 76 | + |
| 77 | +#### 后端依赖 |
| 78 | +```bash |
| 79 | +# 进入后端目录 |
| 80 | +cd backend |
| 81 | + |
| 82 | +# 安装依赖 |
| 83 | +pip install -r requirements.txt |
| 84 | +``` |
| 85 | + |
| 86 | +#### 前端依赖 |
| 87 | +```bash |
| 88 | +# 进入前端目录 |
| 89 | +cd front |
| 90 | + |
| 91 | +# 安装依赖 |
| 92 | +npm install |
| 93 | +# 或使用 pnpm |
| 94 | +pnpm install |
| 95 | +``` |
| 96 | + |
| 97 | +### 3. 配置 |
| 98 | + |
| 99 | +#### 3.1 环境变量配置 |
| 100 | + |
| 101 | +创建 `.env` 文件在 `backend` 目录下: |
| 102 | + |
| 103 | +```env |
| 104 | +# DashScope API Key (用于大语言模型和嵌入模型) |
| 105 | +DASHSCOPE_API_KEY=your_dashscope_api_key |
| 106 | +
|
| 107 | +# 数据库配置 (如果使用 MySQL) |
| 108 | +DB_HOST=localhost |
| 109 | +DB_PORT=3306 |
| 110 | +DB_USER=root |
| 111 | +DB_PASSWORD=your_password |
| 112 | +DB_NAME=chatbot |
| 113 | +
|
| 114 | +# 其他配置 |
| 115 | +SECRET_KEY=your_secret_key |
| 116 | +``` |
| 117 | + |
| 118 | +#### 3.2 模型配置 |
| 119 | + |
| 120 | +修改 `backend/app/config/rag.yaml` 文件: |
| 121 | + |
| 122 | +```yaml |
| 123 | +# 聊天模型名称 |
| 124 | +chat_model_name: qwen3-max |
| 125 | + |
| 126 | +# 文本嵌入模型名称 |
| 127 | +text_embedding_model_name: text-embedding-v4 |
| 128 | +``` |
| 129 | +
|
| 130 | +#### 3.3 向量数据库配置 |
| 131 | +
|
| 132 | +修改 `backend/app/config/chroma.yaml` 文件: |
| 133 | + |
| 134 | +```yaml |
| 135 | +# 向量数据库配置 |
| 136 | +collection_name: rag_collection |
| 137 | +persist_directory: data/chromadb |
| 138 | +k: 3 |
| 139 | +
|
| 140 | +data_path: data |
| 141 | +md5_hex_store: data/md5_hex_store/md5_hex_store.txt |
| 142 | +allow_knowledge_file_types: ["txt", "pdf"] |
| 143 | +
|
| 144 | +# 文档切分配置 |
| 145 | +chunk_size: 200 |
| 146 | +chunk_overlap: 20 |
| 147 | +separators: ["\n\n", "\n", "。", "!", "?", "!", "?", " ", ""] |
| 148 | +``` |
| 149 | + |
| 150 | +### 4. 运行项目 |
| 151 | + |
| 152 | +#### 4.1 启动后端服务 |
| 153 | + |
| 154 | +```bash |
| 155 | +# 进入后端目录 |
| 156 | +cd backend |
| 157 | +
|
| 158 | +# 启动 FastAPI 服务 |
| 159 | +uvicorn main:app --reload |
| 160 | +``` |
| 161 | + |
| 162 | +服务将在 `http://localhost:8000` 运行。 |
| 163 | + |
| 164 | +#### 4.2 启动前端服务 |
| 165 | + |
| 166 | +```bash |
| 167 | +# 进入前端目录 |
| 168 | +cd front |
| 169 | +
|
| 170 | +# 启动开发服务器 |
| 171 | +npm run dev |
| 172 | +# 或使用 pnpm |
| 173 | +pnpm run dev |
| 174 | +``` |
| 175 | + |
| 176 | +前端将在 `http://localhost:5173` 运行。 |
| 177 | + |
| 178 | +#### 4.3 启动用户服务 (可选) |
| 179 | + |
| 180 | +```bash |
| 181 | +# 进入 Django 用户服务目录 |
| 182 | +cd DjangoUserService |
| 183 | +
|
| 184 | +# 启动 Django 服务 |
| 185 | +python manage.py runserver |
| 186 | +``` |
| 187 | + |
| 188 | +用户服务将在 `http://localhost:8000` 运行。 |
| 189 | + |
| 190 | +## 主要功能 |
| 191 | + |
| 192 | +### 1. 智能对话 |
| 193 | +- 基于 RAG 技术,结合文档内容进行问答 |
| 194 | +- 支持多轮对话 |
| 195 | +- 会话历史持久化 |
| 196 | + |
| 197 | +### 2. 文档管理 |
| 198 | +- 支持上传和管理文档 |
| 199 | +- 自动处理文档并生成向量嵌入 |
| 200 | +- 基于相似度检索相关文档 |
| 201 | + |
| 202 | +### 3. 用户系统 |
| 203 | +- 用户注册和登录 |
| 204 | +- 个人资料管理 |
| 205 | +- 会话历史管理 |
| 206 | + |
| 207 | +### 4. 多语言支持 |
| 208 | +- 中英文界面切换 |
| 209 | +- 支持多语言对话 |
| 210 | + |
| 211 | +## API 文档 |
| 212 | + |
| 213 | +### FastAPI 后端 API |
| 214 | + |
| 215 | +启动后端服务后,访问 `http://localhost:8000/docs` 查看自动生成的 API 文档。 |
| 216 | + |
| 217 | +### Django 用户服务 API |
| 218 | + |
| 219 | +启动用户服务后,访问 `http://localhost:8000/api/` 查看用户服务 API 文档。 |
| 220 | + |
| 221 | +## 配置说明 |
| 222 | + |
| 223 | +### 1. 数据库配置 |
| 224 | + |
| 225 | +#### MySQL 配置 |
| 226 | + |
| 227 | +在 `backend/app/config/db_config.py` 中配置 MySQL 连接: |
| 228 | + |
| 229 | +```python |
| 230 | +# MySQL 配置 |
| 231 | +DB_CONFIG = { |
| 232 | + "host": os.getenv("DB_HOST", "localhost"), |
| 233 | + "port": int(os.getenv("DB_PORT", "3306")), |
| 234 | + "user": os.getenv("DB_USER", "root"), |
| 235 | + "password": os.getenv("DB_PASSWORD", ""), |
| 236 | + "database": os.getenv("DB_NAME", "chatbot"), |
| 237 | +} |
| 238 | +``` |
| 239 | + |
| 240 | +### 2. API Key 配置 |
| 241 | + |
| 242 | +#### DashScope API Key |
| 243 | + |
| 244 | +在 `.env` 文件中设置: |
| 245 | + |
| 246 | +```env |
| 247 | +DASHSCOPE_API_KEY=your_dashscope_api_key |
| 248 | +``` |
| 249 | + |
| 250 | +### 3. 模型配置 |
| 251 | + |
| 252 | +在 `backend/app/config/rag.yaml` 中配置模型参数: |
| 253 | + |
| 254 | +```yaml |
| 255 | +# 聊天模型名称 |
| 256 | +chat_model_name: qwen3-max |
| 257 | +
|
| 258 | +# 文本嵌入模型名称 |
| 259 | +text_embedding_model_name: text-embedding-v4 |
| 260 | +``` |
| 261 | + |
| 262 | +### 4. 向量数据库配置 |
| 263 | + |
| 264 | +在 `backend/app/config/chroma.yaml` 中配置向量数据库参数: |
| 265 | + |
| 266 | +```yaml |
| 267 | +# 向量数据库配置 |
| 268 | +collection_name: rag_collection |
| 269 | +persist_directory: data/chromadb |
| 270 | +k: 3 |
| 271 | +
|
| 272 | +data_path: data |
| 273 | +md5_hex_store: data/md5_hex_store/md5_hex_store.txt |
| 274 | +allow_knowledge_file_types: ["txt", "pdf"] |
| 275 | +
|
| 276 | +# 文档切分配置 |
| 277 | +chunk_size: 200 |
| 278 | +chunk_overlap: 20 |
| 279 | +separators: ["\n\n", "\n", "。", "!", "?", "!", "?", " ", ""] |
| 280 | +``` |
| 281 | + |
| 282 | +## 部署说明 |
| 283 | + |
| 284 | +### 生产环境部署 |
| 285 | + |
| 286 | +1. **后端部署** |
| 287 | + - 使用 Gunicorn 作为 WSGI 服务器 |
| 288 | + - 配置 Nginx 作为反向代理 |
| 289 | + |
| 290 | +2. **前端部署** |
| 291 | + - 构建前端静态文件:`npm run build` |
| 292 | + - 将构建产物部署到 Nginx 或其他静态文件服务器 |
| 293 | + |
| 294 | +3. **数据库配置** |
| 295 | + - 生产环境建议使用 MySQL 或 PostgreSQL |
| 296 | + - 配置数据库连接字符串 |
| 297 | + |
| 298 | +## 开发指南 |
| 299 | + |
| 300 | +### 代码结构 |
| 301 | + |
| 302 | +- **backend/app/rag/**: RAG 相关功能,包括向量存储和检索 |
| 303 | +- **backend/app/agent/**: 智能代理,处理用户请求 |
| 304 | +- **backend/app/services/**: 服务层,提供会话管理等功能 |
| 305 | +- **backend/app/utils/**: 工具函数,包括配置加载、文件处理等 |
| 306 | +- **front/src/views/**: 前端页面 |
| 307 | +- **front/src/components/**: 前端组件 |
| 308 | + |
| 309 | +### 开发流程 |
| 310 | + |
| 311 | +1. **添加新功能** |
| 312 | + - 在对应的模块中添加代码 |
| 313 | + - 运行测试确保功能正常 |
| 314 | + - 更新文档 |
| 315 | + |
| 316 | +2. **调试技巧** |
| 317 | + - 使用 FastAPI 的自动重载功能:`uvicorn main:app --reload` |
| 318 | + - 使用 Vue 的热更新功能:`npm run dev` |
| 319 | + |
| 320 | +## 故障排除 |
| 321 | + |
| 322 | +### 常见问题 |
| 323 | + |
| 324 | +1. **API Key 错误** |
| 325 | + - 检查 `.env` 文件中的 DASHSCOPE_API_KEY 是否正确 |
| 326 | + - 确保 API Key 没有过期 |
| 327 | + |
| 328 | +2. **数据库连接失败** |
| 329 | + - 检查数据库配置是否正确 |
| 330 | + - 确保数据库服务正在运行 |
| 331 | + |
| 332 | +3. **向量数据库问题** |
| 333 | + - 检查 `data/chromadb` 目录是否存在 |
| 334 | + - 确保文件权限正确 |
| 335 | + |
| 336 | +4. **前端访问后端 API 失败** |
| 337 | + - 检查 CORS 配置 |
| 338 | + - 确保后端服务正在运行 |
| 339 | + |
0 commit comments