Files
yunmei 1fb1d278d5 feat: 完成 issue #46 RAG 知识库模板化接入(工艺规范/SOP/国标)
- core/rag-kb:领域 RAG 知识库按模板配置(PRD 5.4/7.3,EPIC #6 子任务)
- templating.py:知识源三类分类 + 模板命名推导 + 零依赖轻量 YAML 配置加载
- documents.py:文档段落抽取分块,chunk 携带 文档/章节/段落 溯源信息
- store.py:from_template_config 按模板自动构建 + 中英混合词频检索 + 类别过滤
- config/kb.template.yaml:ti-cl4 模板 RAG 库资产示例(工艺规范/SOP/国标)
- tests:29 用例全绿(模板化/分块溯源/检索排序/类别过滤/配置校验)
2026-08-04 16:31:31 +08:00

54 lines
2.4 KiB
Markdown
Raw Permalink 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.
# RAG KB —— 领域 RAG 知识库模板化接入
对应 PRD 5.4「④ LLM 网关 + RAG」与 Issue #46(EPIC #6 子任务)。
领域 RAG 知识库**按模板配置**(工艺规范 / SOP / 国标 三类知识源),
换行业只改模板资产(`config/kb.template.yaml` + 文档集),内核零改动。
检索返回**命中文档片段 + 来源**(引用溯源,PRD 5.4 强制要求)。
## 模块
| 文件 | 职责 |
|------|------|
| `templating.py` | 知识源分类(process/sop/standard)+ 模板命名推导(向量库 collection / 索引 / namespace)+ 轻量 YAML 配置加载(零第三方依赖) |
| `documents.py` | 文档模型 + 段落抽取分块:每段保留 `文档/章节/段落` 溯源信息 |
| `store.py` | 模板化知识库(`from_template_config` 按配置自动构建)+ 中英混合词频检索 + 类别过滤,返回带来源的命中 |
## 使用示例
```python
from rag_kb import KbTemplateConfig, RagKnowledgeBase
from rag_kb.templating import KbSourceConfig, KnowledgeSourceKind
# 模板 RAG 库配置(通常由 config/kb.template.yaml 加载)
config = KbTemplateConfig(
template="ti-cl4",
sources=[
KbSourceConfig(KnowledgeSourceKind.PROCESS, ["沸腾氯化工艺规范"]),
KbSourceConfig(KnowledgeSourceKind.SOP, ["沸腾氯化炉异常处置SOP"]),
KbSourceConfig(KnowledgeSourceKind.STANDARD, ["GB/T 有关氯气安全标准"]),
],
)
kb = RagKnowledgeBase.from_template_config(
config, loader=lambda title: read_object_storage(title)) # 文档加载器由部署侧注入
hits = kb.search("炉温骤升怎么处理", top_k=3) # 召回 + 溯源
for h in hits:
print(h.source, "→", h.text) # e.g. 沸腾氯化炉异常处置SOP §2.1 ¶3
```
## 验收口径(Issue #46)
- **模板化接入**:三类知识源(工艺规范/SOP/国标)由 `kb.template.yaml` 声明,
换行业只换模板资产,内核零改动(对齐 data-bus 模板化思想)。
- **引用溯源**:检索命中返回 `source`(标题 §章节 ¶段落)——PRD 5.4「答案强制引用溯源」。
- **类别过滤**:可按知识源类别限定检索范围(如只看 SOP)。
- **配置校验**:未知知识源类别 / 缺 template / 文档加载为空 直接报错,杜绝漂移。
## 测试
```bash
cd core/rag-kb
python -m unittest discover -s tests -v
```