fix(boundary): 修复多库边界问题、版本管理及删除清理

多库检索与存储修复:
- RRF 融合去重改用 (collection, chunk_id) 复合键,修复同名文件结果被吞
- DocStore 存储路径加 collection 前缀,修复跨库同名切片数据覆盖
- search_multiple 去重改用复合键
- chunk_id 解析改用 rsplit 兼容下划线文件名

上传与版本管理修复:
- 同名文件上传改为覆盖模式,自动清理旧切片
- 修复首次上传不创建版本记录
- 修复覆盖上传版本号回退到 v1
- sync ADDED 分支改用动态版本号生成
- _generate_version_id 改为基于全部版本递增
- 废止/恢复操作同步 SQLite 版本记录
- mark_document_as_superseded 改为仅更新 SQLite

删除清理修复:
- 删除文档时同步清理 SQLite 版本记录和变更日志
- 删除向量库时同步清理该库所有版本记录
- cleanup 改为清理 SQLite 记录而非 ChromaDB

测试:
- test_version_management.py: 27 条版本管理单元测试
- test_edge_cases.py: 28 条边界用例测试
- test_upload_dedup.py: 5 条上传去重测试
- e2e_risk_test.py: 27 条端到端风险测试

文档:
- 新增风险边界问题修复注意事项.md(面向后端的对接文档)
- 新增向量库边界风险分析.md
- 更新多篇现有文档
This commit is contained in:
lacerate551
2026-06-04 23:58:44 +08:00
parent a1a0814633
commit cb75b9b274
50 changed files with 6385 additions and 6248 deletions

View File

@@ -106,12 +106,25 @@ CONFLICT_TEMPLATES = {
### 3.1 核心模块位置
多源信息融合的核心逻辑位于 `core/agentic.py`
Agentic RAG 已拆分为多个子模块(位于 `core/` 目录),入口仍为 `core/agentic.py`
| 子模块 | 职责 |
|--------|------|
| `core/agentic.py` | 入口文件,组合所有 Mixin提供 `AgenticRAG.process()` 主流程 |
| `core/agentic_base.py` | 常量定义、共享配置API Key 读取、模型选择等) |
| `core/agentic_search.py` | 检索 Mixin — 知识库检索、网络搜索(`_web_search` |
| `core/agentic_answer.py` | 答案生成 Mixin — 多源融合答案生成(`_generate_fused_answer` |
| `core/agentic_query.py` | 查询重写 Mixin — 查询改写、实体补全、专业术语映射 |
| `core/agentic_context.py` | 上下文处理 Mixin — 上下文压缩、去重、Token 控制 |
| `core/agentic_quality.py` | 质量评估 Mixin — 置信度门控、质量评估、推理反思 |
| `core/agentic_meta.py` | 元问题处理 Mixin — 元问题判断和知识库元数据回答 |
| `core/agentic_citation.py` | 引用处理 Mixin — 来源提取、引用构建、引用附加 |
| `core/agentic_media.py` | 富媒体处理 Mixin — 图表查找、图片提取、富媒体附加 |
```python
from core.agentic import AgenticRAG
# 初始化(自动检测网络搜索和图谱配置)
# 初始化(自动检测网络搜索配置,通过 .env 环境变量注入
rag = AgenticRAG()
# 处理查询(自动融合多源信息)
@@ -132,14 +145,14 @@ context = {
'title': '标题', # 网络特有
'date': '2024-01-01' # 时间信息
},
'source_type': '知识库' or '网络搜索' or '知识图谱',
'source_type': '知识库' or '网络搜索',
'query': '检索用的查询词'
}
```
### 3.3 融合答案生成
`AgenticRAG._generate_fused_answer()` 方法处理多源融合:
`AgenticRAG._generate_fused_answer()` 方法处理多源融合(位于 `core/agentic_answer.py``AnswerMixin` 中)
```python
def _generate_fused_answer(self, query: str, contexts: list, allowed_levels: list = None) -> str:
@@ -156,7 +169,6 @@ def _generate_fused_answer(self, query: str, contexts: list, allowed_levels: lis
# 分离不同来源
kb_contexts = [c for c in contexts if c.get('source_type') == self.SOURCE_KB]
web_contexts = [c for c in contexts if c.get('source_type') == self.SOURCE_WEB]
graph_contexts = [c for c in contexts if c.get('source_type') == self.SOURCE_GRAPH]
# ...
```
@@ -172,7 +184,6 @@ Agentic RAG 的 `_think()` 方法决定下一步操作:
|------|------|----------|
| `kb_search` | 检索知识库 | 首次检索、内部文档、公司制度 |
| `web_search` | 网络搜索 | 实时信息、外部知识、最新政策 |
| `graph_search` | 图谱检索 | 实体关系、多跳推理 |
| `answer` | 生成答案 | 信息足够 |
| `rewrite` | 改写查询 | 查询词不准确 |
| `decompose` | 分解问题 | 多个子问题 |
@@ -186,7 +197,6 @@ Agentic RAG 的 `_think()` 方法决定下一步操作:
2. 检索优先级
- 首轮优先检索知识库kb_search
- 涉及部门职责、流程步骤 → 图谱检索graph_search
- 实时信息、外部知识 → 网络搜索web_search
3. 知识库结果评估(关键!)
@@ -204,13 +214,19 @@ Agentic RAG 的 `_think()` 方法决定下一步操作:
### 5.1 配置网络搜索
```python
# config.py 中添加
SERPER_API_KEY = "your-serper-api-key" # Google搜索API
#
BING_API_KEY = "your-bing-api-key" # Bing搜索API
API Key 通过 `.env` 文件(开发环境)或 `deploy/.env.production`(生产环境)环境变量注入,不在代码中硬编码:
```bash
# .env开发环境
SERPER_API_KEY=your-serper-api-key
# deploy/.env.production生产环境
ENABLE_WEB_SEARCH=true
SERPER_API_KEY=your-serper-api-key
```
系统通过 `core/agentic_base.py` 读取环境变量,自动判断是否启用网络搜索功能。
### 5.2 运行命令
```bash
@@ -304,10 +320,20 @@ curl -X POST http://localhost:5001/chat \
| 文件 | 说明 |
|------|------|
| `core/agentic.py` | Agentic RAG 核心,包含信息融合逻辑 |
| `core/agentic.py` | Agentic RAG 入口,组合所有 Mixin 子模块 |
| `core/agentic_base.py` | 常量定义与共享配置API Key、模型名等 |
| `core/agentic_search.py` | 检索 Mixin — 知识库检索、网络搜索 |
| `core/agentic_answer.py` | 答案生成 Mixin — 多源融合答案生成 |
| `core/agentic_query.py` | 查询重写 Mixin — 查询改写与术语映射 |
| `core/agentic_context.py` | 上下文处理 Mixin — 压缩、去重、Token 控制 |
| `core/agentic_quality.py` | 质量评估 Mixin — 置信度门控与推理反思 |
| `core/agentic_meta.py` | 元问题处理 Mixin — 知识库元数据问答 |
| `core/agentic_citation.py` | 引用处理 Mixin — 来源提取与引用构建 |
| `core/agentic_media.py` | 富媒体处理 Mixin — 图表查找与图片提取 |
| `core/engine.py` | 检索引擎封装 |
| `knowledge/manager.py` | 多向量库管理 |
| `knowledge/router.py` | 知识库路由 |
| `knowledge/manager.py` | 多向量库管理(组合各 Mixin |
| `knowledge/search.py` | 多源融合检索与 RRF 打分 |
| `knowledge/router.py` | 知识库智能路由 |
---
@@ -315,5 +341,6 @@ curl -X POST http://localhost:5001/chat \
| 日期 | 版本 | 变更内容 |
|------|------|----------|
| 2026-06-04 | 3.0 | 更新 Agentic RAG 子模块拆分说明;配置改为 .env 环境变量注入;移除图谱检索内容 |
| 2026-04-13 | 2.0 | 更新代码路径agentic_rag_v2.py → core/agentic.py |
| 2025-03-30 | 1.0 | 初始版本 |