增加rag服务

This commit is contained in:
2026-09-09 14:19:17 +08:00
parent a3deaea4e8
commit 3dada33b3d
41 changed files with 1970 additions and 24 deletions
+88 -2
View File
@@ -10,7 +10,7 @@
## 项目概况
- **开发框架**:Spring Boot 3.3.2 + Spring Security 6 + MyBatis-Plus 3.5.7
- **开发框架**:Spring Boot 3.3.2 + Spring Security 6 + MyBatis-Plus 3.5.8
- **安全认证**:Spring Security + JWT 无状态认证,部分接口白名单免登录
- **权限管理**:基于 RBAC 模型,细粒度控制到接口方法和按钮级别
- **功能模块**:用户管理、角色管理、菜单管理、部门管理、字典管理、代码生成、文件管理、邮件、短信、支付(支付宝/微信)等
@@ -49,7 +49,7 @@ src/main/java/com/ichangzuo/
| JDK | 17 | |
| Spring Boot | 3.3.2 | 开发框架 |
| Spring Security | 6 | 安全认证 |
| MyBatis-Plus | 3.5.7 | ORM 框架 |
| MyBatis-Plus | 3.5.8 | ORM 框架 |
| Knife4j | 4.5.0 | 接口文档 |
| MapStruct | 1.5.5.Final | 对象映射 |
| Druid | 1.2.23 | 数据库连接池 |
@@ -104,6 +104,92 @@ mvn test
mvn clean package -DskipTests
```
## RAG 智能问答集成
系统集成了 Python FastAPI RAG 服务(端口 8990),提供 AI 自动问答能力。
### 架构
```
用户提问 (C++ 桌面端)
↓
Spring Boot (8989) — QuestionPollingService 定时轮询 state=0 的新问题
↓
RagClientService → FastAPI (8990/py/rag/*)
↓
LangGraph Pipeline: 嵌入 → 相似度匹配 → 知识库检索 → LLM 生成 → 置信度评分
↓
结果写回 MySQL mc_help_qa 表
```
### 核心组件
| 组件 | 路径 | 职责 |
|------|------|------|
| `RagClientService` | `service/RagClientService.java` | HTTP 客户端,调用 FastAPI `/py/rag/*` 端点(含 FAQ 上传/删除) |
| `QuestionPollingService` | `service/impl/QuestionPollingService.java` | 定时轮询(默认 10s),处理新问题,路由置信度 |
| `RagController` | `controller/RagController.java` | 管理端 API:知识库统计、文件上传/删除(按 category 路由 FAQ/手册) |
| `HelperController` | `controller/HelperController.java` | 问答管理:列表、编辑、入库/移出向量库 |
| `RagConfig` | `config/RagConfig.java` | HTTP 连接池配置(最大 20 连接) |
| `RagProperties` | `config/property/RagProperties.java` | 配置属性:`rag.baseUrl`、超时、批量大小 |
### RAG 端点(Spring Boot → FastAPI)
| Spring Boot 端点 | 转发到 FastAPI | 说明 |
|---|---|---|
| `POST /api/v1/rag/query` | `POST /py/rag/query` | RAG 智能问答 |
| `GET /api/v1/rag/kb/stats` | `GET /py/rag/stats` | 知识库统计 |
| `GET /api/v1/rag/kb/files` | MySQL 直读 | 文件列表(Java 侧管理,含 `fileCategory`) |
| `POST /api/v1/rag/kb/upload` | `POST /py/rag/addKF` 或 `/py/rag/addQAF` | 上传文档,按 `category` 路由:0=手册→addKF,1=FAQ→addQAF |
| `DELETE /api/v1/rag/kb/files/{fileId}` | `DELETE /py/rag/delKF/{fileId}` 或 `/py/rag/delQAF/{fileId}` | 删除文档,按 `fileCategory` 路由 |
### 问答管理端点
| 端点 | 说明 |
|---|---|
| `GET /api/v1/helper/listQnA/{id}` | 按类型获取问答列表 |
| `GET /api/v1/helper/pageQnA` | 分页查询问答 |
| `PUT /api/v1/helper/updateQnA` | 新增/编辑问答 |
| `PUT /api/v1/helper/setQnAByIds/{ids}` | 批量设置状态 |
| `POST /api/v1/helper/updateVectors` | 批量入库/移出向量库 |
### 问题处理流程
1. C++ 桌面端提交问题 → MySQL `mc_help_qa` (state=0)
2. `QuestionPollingService` 每 10s 轮询 → 批量 claim (state=0→1)
3. 重复问题拦截:与历史记录(state=3/4/5)比对,重复 → state=5 跳过
4. 逐条调用 `RagClientService.query()` → FastAPI RAG 流水线
5. 命中问题库(helpId 非空)→ 复制匹配状态与答案(匹配 state=4/6 时从 MySQL 取答案)
6. 未命中:置信度 ≥ 0.9 → 自动通过 (state=4);< 0.9 → 待人工编辑 (state=2)
7. 人工审核 → 编辑/批准/不处理/无效/发布
### 问题状态机
| state | 含义 | 说明 |
|-------|------|------|
| 0 | 未查阅 | C++ 桌面端提交的初始状态 |
| 1 | 已领取 | 轮询服务原子性 claim,处理中 |
| 2 | 待人工编辑 | RAG 置信度 < 0.9,转人工 |
| 3 | 不处理 | 粗口/违规,人工标记 |
| 4 | 已回答 | RAG 置信度 ≥ 0.9 自动通过,或 QV 匹配复制 |
| 5 | 无效 | 重复问题,自动跳过 |
| 6 | 公开 | 已发布,QV 匹配时从 MySQL 取答案 |
### 配置
```yaml
rag:
base-url: http://localhost:8990/py # FastAPI 地址
connect-timeout: 5000 # 连接超时 (ms)
read-timeout: 120000 # 读取超时 (ms)
batch-size: 20 # 批量操作大小
enabled: true # 是否启用 RAG
polling:
fixed-delay: 10000 # 轮询间隔 (ms)
batch-size: 2 # 每轮最多处理条数
```
## 接口文档
- `knife4j` 接口文档:[http://localhost:8989/doc.html](http://localhost:8989/doc.html)
- `swagger` 接口文档:[http://localhost:8989/swagger-ui/index.html](http://localhost:8989/swagger-ui/index.html)