springAiDemo/README.md
kennethcheng 43891f876e feat: 添加 RAG 文档问答功能
- 新增 DocumentController 提供文档导入 API
- 新增 DocumentService 实现文档读取、分割、向量化存储
- 新增 RagConfig 配置 TokenTextSplitter
- 添加 doris_intro.md 作为示例 RAG 文档
- 启用 milvus-sdk-java 依赖
- 配置 SiliconFlow Embedding 服务 (BAAI/bge-large-zh-v1.5)
- 配置 Milvus 向量数据库连接
2026-04-19 14:18:42 +08:00

276 lines
7.8 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Spring AI Demo
> 🤖 一个简洁优雅的 Spring AI 对话演示项目,基于 Spring Boot 3.2.0 与 Spring AI 1.0.0-M3 构建支持流式响应、Markdown 渲染与 RAG 文档问答。
[![Spring Boot](https://img.shields.io/badge/Spring%20Boot-3.2.0-brightgreen)](https://spring.io/projects/spring-boot)
[![Spring AI](https://img.shields.io/badge/Spring%20AI-1.0.0--M3-blue)](https://spring.io/projects/spring-ai)
[![JDK](https://img.shields.io/badge/JDK-17%2B-orange)](https://openjdk.org/)
[![License](https://img.shields.io/badge/License-MIT-yellow)](LICENSE)
## ✨ 特性
- 🌐 **智能对话** - 基于 OpenAI/Ollama 的自然语言交互
- 📡 **流式响应** - 实时逐字输出,体验流畅
- 📝 **Markdown 支持** - 完整渲染代码块、表格、列表等格式
- 👨‍💻 **代码高亮** - Highlight.js 自动语言检测与语法着色
- 📚 **RAG 文档问答** - 文档向量化存储,智能语义检索
- 🗃️ **向量数据库** - Milvus 分布式向量数据库集成
- 🔍 **Embedding 服务** - SiliconFlow BAAI/bge-large-zh-v1.5
- 📔 **PDF 解析** - Apache PDFBox 文档处理支持
- 🎨 **精美界面** - 深色主题响应式设计
## 🚀 快速开始
### 环境要求
- JDK 17+
- Maven 3.8+
- [Ollama](https://ollama.ai/) 本地服务
- Milvus 向量数据库(可选,用于 RAG
### 1. 安装 Ollama
```bash
# macOS/Linux
curl -fsSL https://ollama.ai/install.sh | sh
# 拉取模型
ollama pull gpt-oss:120b-cloud
# 启动服务 (默认端口 11434)
ollama serve
```
### 2. 启动项目
```bash
mvn spring-boot:run
```
### 3. 访问应用
- **Web 界面**: http://localhost:8080
- **健康检查**: http://localhost:8080/api/chat/test
## 🔧 技术栈
### 后端
| 组件 | 技术 | 版本 |
|:---|:---|:---|
| 基础框架 | Spring Boot | 3.2.0 |
| AI 框架 | Spring AI | 1.0.0-M3 |
| AI 模型 | OpenAI/Ollama | - |
| Embedding | SiliconFlow BAAI/bge-large-zh-v1.5 | - |
| 向量数据库 | Milvus | 2.3.4 |
| 响应式编程 | Spring WebFlux | 3.2.0 |
| 文档处理 | Apache PDFBox | 2.0.29 |
### 前端
| 技术 | 用途 |
|:---|:---|
| HTML5 + CSS3 | 页面结构与样式 |
| Marked.js | Markdown 解析渲染 |
| Highlight.js | 代码语法高亮 |
## 📁 项目结构
```
springAiDemo/
├── src/
│ └── main/
│ ├── java/com/demo/
│ │ ├── MyApplication.java # Spring Boot 启动入口
│ │ ├── config/
│ │ │ └── RagConfig.java # RAG 文本分割配置
│ │ ├── controller/
│ │ │ ├── ChatController.java # AI 聊天 REST API
│ │ │ └── DocumentController.java # 文档导入 REST API
│ │ └── service/
│ │ └── DocumentService.java # 文档处理服务
│ └── resources/
│ ├── application.yaml # 应用配置
│ └── static/ # 前端静态资源
│ ├── index.html # 主页面
│ ├── css/style.css # 深色主题样式
│ └── js/app.js # 流式响应逻辑
├── data/
│ └── doris_intro.md # RAG 示例文档
├── pom.xml
└── README.md
```
## 💬 API 文档
### 聊天接口
| 方法 | 端点 | 描述 | 参数 |
|:---|:---|:---|:---|
| GET | `/api/chat/test` | 健康检查 | - |
| GET | `/api/chat/ai` | 同步 AI 对话 | `msg` |
| GET | `/api/chat/ai/stream` | 流式 AI 对话 | `msg` |
### 文档接口
| 方法 | 端点 | 描述 | 参数 |
|:---|:---|:---|:---|
| GET | `/api/documents/import` | 导入文档到向量库 | - |
### 请求示例
**同步对话:**
```bash
curl "http://localhost:8080/api/chat/ai?msg=什么是Spring AI"
```
**流式对话:**
```bash
curl "http://localhost:8080/api/chat/ai/stream?msg=讲一个故事"
```
**导入 RAG 文档:**
```bash
curl http://localhost:8080/api/documents/import
```
## 📚 RAG 文档问答
### 功能说明
将本地文档导入 Milvus 向量数据库,实现基于语义理解的智能问答。
### 使用步骤
1.`.md``.txt` 文档放入 `data/` 目录
2. 启动应用
3. 调用导入接口:
```bash
curl http://localhost:8080/api/documents/import
```
4. 通过聊天界面提问相关问题
### 文档处理配置
```yaml
document:
data-path: data # 文档目录
chunk-size: 400 # 分割块大小 (tokens)
min-chunk-size: 200 # 最小块大小
max-num-chunk: 10000 # 最大块数量
```
## 🎨 界面预览
🧩 **现代化深色主题**
- 渐变标题与毛玻璃效果
- 流畅的消息气泡动画
- Markdown 代码块高亮
- 流式响应动画指示器
📝 **Markdown 支持**
- 代码块与行内代码
- 加粗、斜体、删除线
- 有序/无序列表
- 引用块、表格、链接
## 🛠️ 配置说明
### AI 对话配置
```yaml
spring:
ai:
openai:
base-url: http://localhost:11434 # Ollama 服务地址
chat:
options:
model: gpt-oss:120b-cloud # 对话模型
temperature: 0.7
```
### Embedding 配置
```yaml
spring:
ai:
openai:
embedding:
api-key: your-siliconflow-api-key
base-url: https://api.siliconflow.cn
model: BAAI/bge-large-zh-v1.5
dimensions: 1024
enabled: true
```
### 向量数据库配置
```yaml
spring:
ai:
vectorstore:
milvus:
host: 192.168.50.103
port: 19530
database-name: doris_docs
collection-name: vector_store
embedding-dimension: 1024
index-type: IVF_FLAT
metric-type: COSINE
```
## 🎬 架构图
```
┌─────────────┐ ┌──────────────────┐ ┌─────────────────┐
│ Client │────▶│ ChatController │────▶│ ChatClient │
│ (HTTP) │ │ (Spring MVC) │ │ (Spring AI) │
└─────────────┘ └──────────────────┘ └────────┬────────┘
┌──────────────────┐ │
│ DocumentService │ │
│ (文档处理) │ ▼
└────────┬─────────┘ ┌─────────────────┐
│ │ Ollama/OpenAI │
▼ │ (LLM Provider) │
┌──────────────────┐ └─────────────────┘
│ Milvus Store │
│ (向量存储) │
└──────────────────┘
```
## 📄 预留功能
以下依赖已集成,可按需启用:
- 📔 **PDF 文档解析** - Apache PDFBox 文档处理
## 👷 已知限制
1. 暂无对话历史持久化
2. 暂未实现请求频率限制
3. Milvus 连接信息硬编码在配置中
## 📖 更新日志
### v1.0.0 (2026-04-19)
- 🎉 初始版本发布
- 支持流式 AI 对话
- Markdown 渲染与代码高亮
- 深色主题 Web 界面
### v1.1.0 (2026-04-19)
- ✨ 新增 RAG 文档问答功能
- ✨ 新增 DocumentController 文档导入 API
- ✨ 新增 DocumentService 文档处理服务
- ✨ 配置 SiliconFlow Embedding 服务
- ✨ 集成 Milvus 向量数据库
## 📗 License
MIT © 2026 Spring AI Demo
---
🚀 **Made with Spring AI**