将原先内联在 gateway.py 的薄弱 InferenceBackend 提炼为正式抽象基类(ABC),
对齐 PRD 5.6「⑥ 部署底座」契约,为 #44/#45/#58/#59 各类后端提供统一接入点。
实现内容:
- 新增 core/llm-gateway/backends.py:
· InferenceBackend(ABC):PRD 要求的四个生命周期方法 load_model / infer /
health_check / unload(均幂等),能力声明 capabilities,并保留 generate()
向后兼容(转发到 infer().text)
· 值对象 BackendCapabilities(streaming/max_concurrency/on_premises/modalities)
/ BackendHealth(healthy/detail/checked_at)/ InferResult(text+审计元信息)
· LocalBackend / CloudBackend 占位实现迁移至此并继承新 ABC,补齐生命周期
· default_registry + build_backend:配置驱动切换后端(未知 name 报错并提示已知项)
- gateway.py:删除内联定义,改为从 backends.py 再导出,LLMGateway.ask() 调用路径不变
- __init__.py:再导出新符号(BackendCapabilities/BackendHealth/InferResult/
build_backend/default_registry),InferenceBackend 现为 ABC
设计原则:业务代码仅依赖接口,不感知硬件;切换后端 = 换实现 + 改配置,业务零改动。
测试:core/llm-gateway 全量 97 个用例通过(新增 27 + 既有 70,零回归)。
运行:python -m unittest discover -s tests -v(在 core/llm-gateway 目录下)
82 lines
2.7 KiB
Python
82 lines
2.7 KiB
Python
# -*- coding: utf-8 -*-
|
||
"""iAOP-Core · LLM 网关(LLM Gateway)—— 混合 LLM 的安全出站防线与编排。
|
||
|
||
对应 PRD 5.4「④ LLM 网关 + RAG」与 EPIC #6:
|
||
本地 70B(敏感/核心)+ 云端 API(脱敏/通用)混合,敏感数据**本地闭环**。
|
||
|
||
模块组成:
|
||
- dlp DLP 敏感数据拦截引擎(Issue #48):出站内容(query / RAG context /
|
||
模型输出)发往云端前做敏感规则检查,命中即拦截(目标 100% 拦截),
|
||
全量审计。
|
||
- router 敏感度路由规则引擎(Issue #43 雏形):敏感度分级路由(local/cloud/
|
||
block),模板配置驱动,DLP 拦截即 fail-closed 转 block。
|
||
- prompts Prompt 版本管理(Issue #47 雏形):semver 版本库、运行时绑定、
|
||
一键回滚、变更审计。
|
||
- hallucination 幻觉/事实性校验中间件(Issue #47 雏形):引用溯源 +
|
||
高利害信度阈值 → 人工确认。
|
||
- gateway 混合网关主编排(EPIC #6 主体):路由 → 生成 → 溯源校验 →
|
||
DLP 出站防线,端到端闭环。
|
||
- backends 推理后端抽象(Issue #57,PRD 5.6):``InferenceBackend`` 抽象接口
|
||
(``load_model / infer / health_check / unload``),5090 实现(Triton/ONNX)
|
||
与昇腾实现(ACL/CANN)均实现该接口;业务代码仅依赖接口,不感知硬件。
|
||
|
||
测试:`python -m unittest discover -s tests -v`(在 core/llm-gateway 目录下执行)。
|
||
"""
|
||
__version__ = "0.2.0"
|
||
|
||
from .dlp import (
|
||
DLP_DEFAULT_RULES,
|
||
DlpEngine,
|
||
DlpHit,
|
||
DlpResult,
|
||
DlpRule,
|
||
DlpRuleKind,
|
||
)
|
||
from .router import (
|
||
RouteDecision,
|
||
RouteTarget,
|
||
RouterRule,
|
||
SensitivityRouter,
|
||
)
|
||
from .prompts import (
|
||
PromptChange,
|
||
PromptRegistry,
|
||
PromptVersion,
|
||
validate_semver,
|
||
)
|
||
from .hallucination import (
|
||
GuardVerdict,
|
||
HallucinationGuard,
|
||
)
|
||
from .backends import (
|
||
BackendCapabilities,
|
||
BackendHealth,
|
||
InferResult,
|
||
InferenceBackend,
|
||
build_backend,
|
||
default_registry,
|
||
)
|
||
from .gateway import (
|
||
CloudBackend,
|
||
GatewayResult,
|
||
LLMGateway,
|
||
LocalBackend,
|
||
)
|
||
|
||
__all__ = [
|
||
# dlp
|
||
"DlpRuleKind", "DlpRule", "DlpHit", "DlpResult", "DlpEngine", "DLP_DEFAULT_RULES",
|
||
# router
|
||
"RouteTarget", "RouterRule", "RouteDecision", "SensitivityRouter",
|
||
# prompts
|
||
"PromptVersion", "PromptChange", "PromptRegistry", "validate_semver",
|
||
# hallucination
|
||
"GuardVerdict", "HallucinationGuard",
|
||
# backends (Issue #57)
|
||
"BackendCapabilities", "BackendHealth", "InferResult", "InferenceBackend",
|
||
"build_backend", "default_registry",
|
||
# gateway
|
||
"LocalBackend", "CloudBackend",
|
||
"GatewayResult", "LLMGateway",
|
||
]
|