feat: TODO

This commit is contained in:
李如威 2025-07-09 01:04:56 +08:00
parent 9b9265b46a
commit 03ee2525ab
1 changed files with 61 additions and 42 deletions

103
README.md
View File

@ -2,6 +2,15 @@
一个高效、简洁的 RAG (Retrieval-Augmented Generation) 服务,基于 FastAPI 构建。 一个高效、简洁的 RAG (Retrieval-Augmented Generation) 服务,基于 FastAPI 构建。
## TODO
- [ ] 提高检索准确度
- [ ] 知识库隔离
- [ ] 支持 doc
- [ ] 支持 docx
- [ ] 支持 excel
- [ ] 支持 csv
## 功能特性 ## 功能特性
- 🚀 **高性能 API 服务** - 基于 FastAPI 构建 - 🚀 **高性能 API 服务** - 基于 FastAPI 构建
@ -101,11 +110,13 @@ python run_tests.py all
## API 接口 ## API 接口
### 健康检查 ### 健康检查
``` ```
GET /api/health GET /api/health
``` ```
### 上传文档 ### 上传文档
``` ```
POST /api/upload POST /api/upload
Content-Type: multipart/form-data Content-Type: multipart/form-data
@ -115,6 +126,7 @@ Content-Type: multipart/form-data
``` ```
### 查询问答 ### 查询问答
``` ```
POST /api/chat POST /api/chat
Content-Type: application/json Content-Type: application/json
@ -127,6 +139,7 @@ Content-Type: application/json
``` ```
### 流式聊天问答 🆕 ### 流式聊天问答 🆕
``` ```
POST /api/chat/stream POST /api/chat/stream
Content-Type: application/json Content-Type: application/json
@ -145,6 +158,7 @@ Content-Type: application/json
``` ```
### 获取文档列表 ### 获取文档列表
``` ```
GET /api/documents GET /api/documents
@ -260,17 +274,17 @@ def stream_chat(question):
json={'question': question, 'top_k': 3}, json={'question': question, 'top_k': 3},
stream=True stream=True
) )
for line in response.iter_lines(): for line in response.iter_lines():
if line: if line:
# 解析 Server-Sent Events 格式 # 解析 Server-Sent Events 格式
if line.startswith(b'data: '): if line.startswith(b'data: '):
data = json.loads(line[6:]) data = json.loads(line[6:])
# 打印文本内容 # 打印文本内容
if data.get('content'): if data.get('content'):
print(data['content'], end='', flush=True) print(data['content'], end='', flush=True)
# 处理最终数据块 # 处理最终数据块
if data.get('is_final'): if data.get('is_final'):
print(f"\n\n处理时间: {data.get('processing_time', 0):.2f}秒") print(f"\n\n处理时间: {data.get('processing_time', 0):.2f}秒")
@ -286,53 +300,53 @@ stream_chat("详细解释文档的主要观点")
```javascript ```javascript
// 流式聊天问答 - 前端实现 // 流式聊天问答 - 前端实现
async function streamChat(question) { async function streamChat(question) {
const response = await fetch('/api/chat/stream', { const response = await fetch("/api/chat/stream", {
method: 'POST', method: "POST",
headers: { headers: {
'Content-Type': 'application/json', "Content-Type": "application/json",
}, },
body: JSON.stringify({ body: JSON.stringify({
question: question, question: question,
top_k: 3 top_k: 3,
}) }),
}); });
const reader = response.body.getReader(); const reader = response.body.getReader();
const decoder = new TextDecoder(); const decoder = new TextDecoder();
while (true) { while (true) {
const { done, value } = await reader.read(); const { done, value } = await reader.read();
if (done) break;
const chunk = decoder.decode(value); if (done) break;
const lines = chunk.split('\n');
for (const line of lines) { const chunk = decoder.decode(value);
if (line.startsWith('data: ')) { const lines = chunk.split("\n");
try {
const data = JSON.parse(line.slice(6)); for (const line of lines) {
if (line.startsWith("data: ")) {
// 显示文本内容 try {
if (data.content) { const data = JSON.parse(line.slice(6));
document.getElementById('chat-output').innerHTML += data.content;
} // 显示文本内容
if (data.content) {
// 处理最终数据块 document.getElementById("chat-output").innerHTML += data.content;
if (data.is_final) { }
console.log(`处理时间: ${data.processing_time}秒`);
console.log(`参考来源: ${data.sources.length}个文档`); // 处理最终数据块
} if (data.is_final) {
} catch (e) { console.log(`处理时间: ${data.processing_time}秒`);
console.error('解析数据失败:', e); console.log(`参考来源: ${data.sources.length}个文档`);
} }
} } catch (e) {
console.error("解析数据失败:", e);
} }
}
} }
}
} }
// 使用示例 // 使用示例
streamChat('请解释文档的主要内容'); streamChat("请解释文档的主要内容");
``` ```
## 开发指南 ## 开发指南
@ -350,6 +364,7 @@ streamChat('请解释文档的主要内容');
### 配置文件 ### 配置文件
主要配置在 `config.py` 中: 主要配置在 `config.py` 中:
- LLM 模型配置 - LLM 模型配置
- 向量模型设置 - 向量模型设置
- 数据库路径 - 数据库路径
@ -360,20 +375,23 @@ streamChat('请解释文档的主要内容');
### 常见问题 ### 常见问题
1. **服务启动失败** 1. **服务启动失败**
```bash ```bash
# 检查端口是否被占用 # 检查端口是否被占用
lsof -i :8000 lsof -i :8000
# 检查依赖是否完整安装 # 检查依赖是否完整安装
pip install -r requirements.txt pip install -r requirements.txt
``` ```
2. **文档上传失败** 2. **文档上传失败**
- 检查文件格式是否支持 (PDF, TXT) - 检查文件格式是否支持 (PDF, TXT)
- 确认文件大小不超过限制 - 确认文件大小不超过限制
- 检查磁盘空间是否充足 - 检查磁盘空间是否充足
3. **查询响应慢** 3. **查询响应慢**
- 检查向量数据库索引状态 - 检查向量数据库索引状态
- 考虑调整 `top_k` 参数 - 考虑调整 `top_k` 参数
- 监控系统资源使用情况 - 监控系统资源使用情况
@ -411,6 +429,7 @@ python tests/performance_monitor.py
## 支持 ## 支持
如有问题或建议,请: 如有问题或建议,请:
1. 查看 [TESTING_README.md](TESTING_README.md) 测试文档 1. 查看 [TESTING_README.md](TESTING_README.md) 测试文档
2. 运行 `python run_tests.py quick` 进行快速诊断 2. 运行 `python run_tests.py quick` 进行快速诊断
3. 提交 Issue 或 Pull Request 3. 提交 Issue 或 Pull Request