feat: 完成 issue #6 LLM 网关 + RAG 模板化(混合网关主编排)

This commit is contained in:
2026-08-04 18:02:00 +08:00
parent fa5523a37d
commit 5d937e8efd
12 changed files with 1707 additions and 20 deletions
+52 -6
View File
@@ -1,6 +1,6 @@
# iAOP-Core · LLM 网关(LLM Gateway)
对应 PRD 5.4「④ LLM 网关 + RAG」与 EPIC #6(Issue #48 等子任务):
对应 PRD 5.4「④ LLM 网关 + RAG」与 EPIC #6:
本地 70B(敏感/核心)+ 云端 API(脱敏/通用)**混合**,安全分级路由,
**敏感数据本地闭环,仅脱敏/公开内容可走云端 API**(数据不出厂)。
@@ -8,13 +8,23 @@
```
core/llm-gateway/
├── __init__.py 包入口(导出 DLP 引擎 API)
├── __init__.py 包入口(导出各模块 API)
├── dlp.py DLP 敏感数据拦截引擎(Issue #48)
├── router.py 敏感度路由规则引擎(Issue #43 雏形)
├── prompts.py Prompt 版本管理(Issue #47 雏形)
├── hallucination.py 幻觉/事实性校验中间件(Issue #47 雏形)
├── gateway.py 混合网关主编排(EPIC #6 主体交付)
├── config/
│ └── dlp.template.yaml 模板 DLP 规则资产(ti-cl4 示例,换行业只改它)
│ ├── dlp.template.yaml 模板 DLP 规则资产(ti-cl4 示例)
│ ├── router.template.yaml 模板敏感度路由规则资产
│ └── prompts.template.yaml 模板提示词版本库资产
└── tests/
├── _bootstrap.py 测试引导(目录含连字符,挂载包名 llm_gateway)
└── test_dlp.py DLP 引擎单元测试
├── test_dlp.py DLP 引擎单元测试
├── test_router.py 路由引擎单元测试
├── test_prompts.py Prompt 版本库单元测试
├── test_hallucination.py 幻觉/事实性校验单元测试
└── test_gateway.py 网关主编排端到端单元测试
```
## DLP 敏感数据拦截(Issue #48)
@@ -70,7 +80,43 @@ cd core/llm-gateway
python -m unittest discover -s tests -v
```
## 混合网关主编排(EPIC #6 主体)
`LLMGateway.ask()` 串起完整闭环:**敏感度路由 → 本地/云端生成 → 引用溯源校验
→ DLP 出站防线**,覆盖 PRD 5.4 用户操作流程(提问 → 路由判断敏感级 →
本地/云端生成 → RAG 溯源校验 → 返回带引用的答案;异常转人工)。
```python
from llm_gateway import LLMGateway
from llm_gateway.dlp import DlpEngine
from llm_gateway.router import SensitivityRouter
from llm_gateway.prompts import PromptRegistry
gw = LLMGateway(
dlp=DlpEngine(),
router=SensitivityRouter.from_template_config("config/router.template.yaml"),
prompts=PromptRegistry.from_template_config("config/prompts.template.yaml"),
high_stakes_names=["alarm_explain"], # 高利害模板启用信度阈值
)
result = gw.ask("炉温偏高怎么处理", rag_context=["沸腾氯化炉异常处置SOP"])
print(result.route.target) # local / cloud / block
print(result.verdict.action) # pass / human_review / unsupported
if result.needs_human:
... # 转人工确认(PRD 5.4 异常时转人工)
```
- **敏感度路由**(router.py,Issue #43 雏形):模板配置驱动,DLP 拦截
fail-closed 强制 block,未知内容保守走本地(数据不出厂);
- **Prompt 版本管理**(prompts.py,Issue #47 雏形):semver 版本库、
运行时绑定(可复现)、一键回滚、变更审计;
- **幻觉/事实性校验**(hallucination.py,Issue #47 雏形):`[来源: X]`
引用溯源强制校验 + 高利害信度阈值 → 人工确认;
- **推理后端抽象**(gateway.py 内 `InferenceBackend`):业务代码只依赖
接口,本地 70B / 云端 API 具体接入由子任务 #44 / #45 实现。
## 后续子任务(EPIC #6 拆分,待扩展)
- 敏感度路由(准确率 ≥ 96.5%)与路由准确率评估脚本(Issue #49);
- Prompt 版本管理与幻觉/事实性校验中间件(Issue #47)。
- 敏感度路由调优与路由准确率评估脚本(Issue #49,本版已提供评估入口);
- 本地 70B 模型接入与推理封装(Issue #44);
- 云端 API(Qwen/DeepSeek)接入与安全网关(Issue #45);
- Prompt 版本管理 + 幻觉校验中间件完善(Issue #47,本版已提供核心)。