guard: 判定改回宿主本地守卫模型,数据不出机器

原先判定走远程 /v1/chat/completions,用户原文会出境,且 2s 级延迟、抖动大。改为 host.llm.call:
- 输入 {task=prompt_moderation, prompt=指令+正文}
- 输出 {task=response_moderation, prompt=指令, response=模型输出}
宿主契约只接受这两个键集合,多一个键即 InvalidModelRequest;返回 {safety,categories,refusal},safety 仅 Safe/Unsafe/Controversial。本地判定套不了 OpenAI messages 结构——OpenAI 格式只保留在对外接口。

配置:启用 llm 段(models/guard_model),插件声明 llm.allow: [guard_model],
移除 network 白名单与 judge_* 远程参数;新增 block_controversial。
按本地推理重设资源:hook_timeout 45→120、judge_chunk_bytes 16384→8192、
max_buffer_bytes 131072→65536、max_new_tokens=64、模型 timeout=60。

新增 tools/check_local_model.py 做部署前自检(模型目录完整性、llm.allow 与
llm.id 一致、torch/transformers 依赖、设备)。模型目录缺失时 load_settings
直接抛错,不会降级放行。

verify_plugins.py 的假宿主改为复刻 middleware/llm.py 的严格契约,并断言全程
零出网;用例扩到 103 项,全部通过。已用假模型目录验证 load_settings 与
LLMRegistry 能正确建立 guard_model adapter。
This commit is contained in:
Автомата Калашникова committed 2026-11-27 19:17:17 +08:00
1 parent ec6f5fc811
commit d239cf584c
5 files changed
+436 -456

No files matched your search

+28 -39
View File
@@ -4,8 +4,8 @@ listen_port: 8080
limits:
max_sessions: 4
# 单次判定 1~5s,最多切 8 段
hook_timeout_seconds: 45
# 本地模型串行推理(max_concurrency 固定 1),按单段最慢几十毫秒到数秒留
hook_timeout_seconds: 120
upstream_timeout_seconds: 120
max_attachment_files: 8
max_attachment_fields: 128
@@ -15,20 +15,23 @@ limits:
attachment_timeout_seconds: 30
max_attachment_reads: 64
# 本地守卫模型已停用(模型文件不存在,启用会导致启动失败),判定走下面的 OpenAI 接口
# llm:
# - id: guard_model
# type: guard
# file: models/guard_model
# load: {device: cpu}
# limits:
# max_input_tokens: 8192
# max_new_tokens: 32
# timeout_seconds: 30
# queue_size: 8
# 本地守卫模型。file 必须是 HuggingFace 目录:config.json + 权重 + tokenizer。
# 目录不存在时宿主启动即失败(不会降级成放行)。部署前跑 tools/check_local_model.py。
llm:
- id: guard_model
type: guard
file: models/guard_model
load:
device: cpu
dtype: auto
limits:
max_input_tokens: 8192
max_new_tokens: 64
timeout_seconds: 60
queue_size: 32
plugins:
# 旧协议遗留,依赖已停用的 guard_model,且宿主没有 host.files
# 旧协议遗留,已停用:依赖宿主没有的 host.files,且未声明 llm.allow
- id: file-content-review
enabled: false
file: plugins/file_content_review.lua
@@ -40,7 +43,7 @@ plugins:
max_review_bytes: 8192
max_files: 8
# 旧协议遗留,外部审核服务白名单转发
# 旧协议遗留,已停用:外部审核服务白名单转发
- id: network-review
enabled: false
file: plugins/network_review.lua
@@ -62,39 +65,25 @@ plugins:
file: plugins/response_guard.lua
match:
paths: ["/v1/chat/completions"]
network:
allow:
- domain: ai.leapstella.com
scheme: https
ports: [443]
timeout_seconds: 20
max_response_bytes: 1048576
# 必须与 llm 段的 id 一致,否则 host.llm.call 一定失败
llm:
allow: [guard_model]
config:
# 主机、/v1、完整端点三种写法都可以
base_url: https://ai.leapstella.com/v1
# 部署时填入自己的密钥;仓库里只有占位符
api_key: "sk-REPLACE_ME"
# 比 deepseek-flash 快且抖动小,实测 1.0~1.3s
model: deepseek-v4-pro
model: guard_model
block_status: 400
# buffered 则整条响应只判一次,但客户端看不到增量
stream_mode: incremental
flush_chars: 512
force_flush_bytes: 4096
# 8 段 × judge_chunk_bytes(16KB)
max_buffer_bytes: 131072
judge_chunk_bytes: 16384
judge_max_tokens: 512
# 关掉思维链,省时延也避免 content 被思维链挤空
judge_reasoning_effort: none
# 8 段 × judge_chunk_bytes
max_buffer_bytes: 65536
judge_chunk_bytes: 8192
check_input: true
check_output: true
# 换 gpt-5 / o 系列时需要,DeepSeek 类服务不用改
# judge_token_param: max_completion_tokens
# judge_temperature: false
# judge_json_mode: false
# Controversial 也按不安全处理;误伤多时设 false
block_controversial: true
# 拦截后替换给上游的占位请求:客户端没给 model 时用它兜底
# stub_model: deepseek-v4-pro
# stub_model: your-upstream-model
# stub_max_tokens: false
- id: stream-redact