Java — 基于 Retrieval-Augmented Generation 的智能知识库问答系统
cd rag-knowledge-base
docker-compose up -d# Windows PowerShell:
$env:DEEPSEEK_API_KEY="sk-你的真实key"
# Windows CMD:
set DEEPSEEK_API_KEY=sk-你的真实key或者直接修改 src/main/resources/application.yml,将 DEEPSEEK_API_KEY 环境变量替换为你的真实 Key。
注意:只需配置 Chat API Key。Embedding 模型用的是 BGE-Small-ZH,本地运行无需 API Key。
cd rag-knowledge-base
mvn spring-boot:run首次启动 BGE 模型会自动下载 ONNX 文件 (~45MB),稍等片刻。
看到 Started RagApplication 即启动成功。
后端地址: http://localhost:8081
cd rag-knowledge-base\rag-frontend
npm install # 仅首次需要
npm run dev前端地址: http://localhost:5173
- 浏览器打开
http://localhost:5173 - 创建一个知识库(设置名称、描述、切片参数用默认即可)
- 点击进入知识库,拖拽上传一个 txt 或 md 文件
- 等待状态自动刷新:PENDING → 解析中 → 切片中 → 向量化中 → 已完成
- 点击"AI 问答",输入问题开始对话
| 层级 | 技术 | 说明 |
|---|---|---|
| JDK | Java 21 | LTS |
| 框架 | Spring Boot 3.4.3 | MVC + 异步 |
| ORM | MyBatis-Plus 3.5.5 | Lambda 查询 |
| LLM | LangChain4j 1.0.0-beta1 | RAG 抽象层 |
| 对话模型 | DeepSeek Chat API | OpenAI 兼容 |
| 向量模型 | BGE-Small-ZH | 本地运行,512 维,零 API 成本 |
| 向量库 | PostgreSQL + pgvector | Docker 一键部署 |
| 文档解析 | Apache Tika 2.9.2 | PDF/Word/Markdown/TXT |
| 前端 | Vue 3 + Element Plus | Vite 构建 |
┌──────────────────────────────────────────────────────┐
│ Vue 3 前端 (:5173) │
│ 知识库管理 │ 文档上传 │ 语义搜索 │ AI问答 │ 对话历史 │
└───────────────────────┬──────────────────────────────┘
│ REST API (:8081)
┌───────────────────────▼──────────────────────────────┐
│ Spring Boot 后端 │
│ │
│ ┌─────────┐ ┌─────────┐ ┌─────────┐ ┌────────┐ │
│ │知识库CRUD│ │文档服务 │ │搜索服务 │ │RAG对话 │ │
│ └─────────┘ └────┬────┘ └────┬────┘ └───┬────┘ │
│ │ │ │ │
│ ┌─────────────────▼────────────▼───────────▼──────┐ │
│ │ RAG 核心管线 │ │
│ │ 文档 → Tika解析 → 切片 → BGE向量化 → pgvector │ │
│ │ 问题 → 向量化 → 检索 → 提示词 → DeepSeek生成 │ │
│ └────────────────────────────────────────────────┘ │
└───────────────────────┬──────────────────────────────┘
│
┌───────────────┼───────────────┐
▼ ▼
┌───────────────┐ ┌─────────────────┐
│ PostgreSQL │ │ DeepSeek API │
│ + pgvector │ │ (Chat only) │
│ (本地 Docker) │ │ (远程 HTTPS) │
└───────────────┘ └─────────────────┘
| 表名 | 说明 |
|---|---|
knowledge_bases |
知识库(名称、切片参数) |
documents |
文档(文件名、类型、处理状态) |
document_chunks |
文档切片(内容 + embedding_id) |
kb_{id}_embeddings |
向量表(LangChain4j 自动创建,每知识库一张) |
conversations |
对话会话 |
messages |
消息记录(含来源引用 JSONB) |
PENDING → PARSING → CHUNKING → EMBEDDING → COMPLETED
↘ FAILED
| 状态 | 含义 |
|---|---|
| PENDING | 等待处理 |
| PARSING | Tika 解析文档内容 |
| CHUNKING | 将文档切分为片段(800字/段,100字重叠) |
| EMBEDDING | BGE-Small-ZH 生成向量 + 存入 pgvector |
| COMPLETED | 处理完成,可以搜索和问答 |
| FAILED | 处理失败(查看 errorMessage 字段) |
| 方法 | 路径 | 说明 |
|---|---|---|
| POST | /api/knowledge-bases |
创建 |
| GET | /api/knowledge-bases |
列表(分页) |
| GET | /api/knowledge-bases/{id} |
详情 |
| PUT | /api/knowledge-bases/{id} |
更新 |
| DELETE | /api/knowledge-bases/{id} |
删除 |
| 方法 | 路径 | 说明 |
|---|---|---|
| POST | /api/knowledge-bases/{kbId}/documents |
上传文件 |
| GET | /api/knowledge-bases/{kbId}/documents |
列表 |
| DELETE | /api/knowledge-bases/{kbId}/documents/{docId} |
删除 |
| POST | /api/knowledge-bases/{kbId}/documents/{docId}/reprocess |
重新处理 |
| 方法 | 路径 | 说明 |
|---|---|---|
| POST | /api/knowledge-bases/{kbId}/search |
语义搜索 |
| POST | /api/knowledge-bases/{kbId}/chat |
AI 问答 |
| 方法 | 路径 | 说明 |
|---|---|---|
| GET | /api/knowledge-bases/{kbId}/conversations |
列表 |
| DELETE | /api/knowledge-bases/{kbId}/conversations/{convId} |
删除 |
统一响应: { code: 200, message: "success", data: {...}, timestamp: ... }
rag-knowledge-base/
├── pom.xml
├── docker-compose.yml # PostgreSQL + pgvector
├── sql/
│ ├── init.sql # 建表 DDL
│ └── cleanup-vectors.sql # 清理旧向量表(维度迁移用)
├── src/main/java/com/rag/
│ ├── RagApplication.java
│ ├── config/
│ │ ├── LangChain4jConfig.java # DeepSeek Chat + BGE Embedding + PgVector
│ │ ├── MyBatisPlusConfig.java
│ │ ├── WebConfig.java # CORS
│ │ └── VectorTableMigration.java # 启动时自动清理旧向量表
│ ├── controller/ # 5 个 REST 控制器
│ ├── service/ # 服务接口
│ ├── service/impl/ # 核心 RAG 实现
│ ├── mapper/ # MyBatis-Plus Mapper
│ ├── entity/ # 5 个实体类
│ ├── dto/ # 请求/响应 DTO
│ └── exception/ # 全局异常处理
├── src/main/resources/
│ └── application.yml
└── rag-frontend/ # Vue 3 前端
├── vite.config.ts
└── src/
├── views/ # 5 个页面
├── api/ # Axios API 封装
├── components/ # 布局组件
├── router/ # Vue Router
├── types/ # TS 类型定义
└── utils/ # Axios 实例 + 拦截器