guard: 判定请求体按 OpenAI 协议校正
1) 新增 judge_token_param / judge_temperature / judge_json_mode,默认值保持 deepseek 可直接跑,换 gpt-5、o 系列不再 400 全站 503;
2) base_url 归一化,写主机、写 /v1、写完整端点都落到 /v1/chat/completions;
3) system_prompt 缺 json 关键字时自动补一句,规避 json_object 的 400;
4) 请求体解析补 {type:input_text} 内容块,修掉换写法绕过输入审核;
5) 新增 config.example.yaml(占位密钥)与 3 组回归用例,verify 84/84。
This commit is contained in:
1 parent
68b3b36539
commit
1cc154dff8
4 files changed
+288
-10
No files matched your search
@@ -0,0 +1,99 @@
|
||||
upstream: http://127.0.0.1:8000
|
||||
listen_host: 0.0.0.0
|
||||
listen_port: 8080
|
||||
|
||||
limits:
|
||||
max_sessions: 4
|
||||
# 判定走远程 /chat/completions:单次 1~5s,最多切 8 段,留足余量
|
||||
hook_timeout_seconds: 45
|
||||
upstream_timeout_seconds: 120
|
||||
max_attachment_files: 8
|
||||
max_attachment_fields: 128
|
||||
max_attachment_text_bytes: 1048576
|
||||
max_attachment_unpacked_bytes: 33554432
|
||||
max_attachment_zip_entries: 2048
|
||||
attachment_timeout_seconds: 30
|
||||
max_attachment_reads: 64
|
||||
|
||||
# 不再注册本地守卫模型(models/guard_model 已停用)。
|
||||
# 判定统一走下面的 OpenAI 兼容接口,密钥只存在于本文件,不会进日志。
|
||||
|
||||
plugins:
|
||||
# ---------------------------------------------------------------------------
|
||||
# 输入 + 输出内容安全
|
||||
# 请求侧:判定不通过 → 请求体换成 stub(原文不发给模型)→ 响应侧返回 400
|
||||
# 响应侧:逐段审核 → 脱敏 → 放行;不安全/审核不可用 → OpenAI 错误体
|
||||
# ---------------------------------------------------------------------------
|
||||
- id: response-guard
|
||||
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
|
||||
config:
|
||||
# 判定端点 = base_url + /chat/completions;base_url 写下面三种都可以:
|
||||
# https://your-gateway.example → 自动补 /v1/chat/completions
|
||||
# https://your-gateway.example/v1 → /v1/chat/completions
|
||||
# https://your-gateway.example/v1/chat/completions(完整端点)
|
||||
base_url: https://ai.leapstella.com/v1
|
||||
# 复制本文件为 config.yaml 后填入你自己的密钥,config.yaml 已被 .gitignore 忽略
|
||||
api_key: "sk-REPLACE_ME"
|
||||
model: deepseek-flash
|
||||
# 拦截状态码:400 = OpenAI 内容策略拒绝的标准形态
|
||||
block_status: 400
|
||||
# incremental: 攒够 flush_chars 且遇到句边界就审核放行(有增量输出)
|
||||
# buffered : 整条响应结束再一次审核(1 次判定,但客户端看不到增量)
|
||||
stream_mode: incremental
|
||||
flush_chars: 512
|
||||
force_flush_bytes: 4096
|
||||
# 上限与判定能力对齐:8 段 × judge_chunk_bytes(16KB) = 128KB
|
||||
max_buffer_bytes: 131072
|
||||
# 正文之外还要审核工具调用参数时,改成数组即可(默认只审核正文):
|
||||
# content_path_stream: ["choices.*.delta.content",
|
||||
# "choices.*.delta.tool_calls.*.function.arguments"]
|
||||
# content_path_full: ["choices.*.message.content",
|
||||
# "choices.*.message.tool_calls.*.function.arguments"]
|
||||
judge_chunk_bytes: 16384
|
||||
judge_max_tokens: 512
|
||||
# deepseek 系列默认带思维链,判定用不到;none 可省 ~0.3s 且避免思维链吃光 token。
|
||||
# 换后端若报 400,删掉这一行即可。
|
||||
judge_reasoning_effort: none
|
||||
# ---- 换后端时才需要的三个开关,默认值对 DeepSeek 一类兼容服务都可直接跑 ----
|
||||
# 生成上限的参数名:OpenAI 的 gpt-5 / o 系列只收 max_completion_tokens,
|
||||
# 发 max_tokens 会 400。
|
||||
# judge_token_param: max_completion_tokens
|
||||
# temperature:o 系列 / gpt-5 只接受默认值 1,发 0 会 400;设 false 即不发送该字段。
|
||||
# judge_temperature: false
|
||||
# 后端不支持 response_format 时关掉,仅靠 system_prompt 约束输出 JSON。
|
||||
# judge_json_mode: false
|
||||
check_input: true
|
||||
check_output: true
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 全缓冲 PII 兜底(跨帧拆分的证件号/手机号)
|
||||
# 代价:流式退化为一次性输出,与 response_guard 的增量放行冲突,默认关闭
|
||||
# ---------------------------------------------------------------------------
|
||||
- id: stream-redact
|
||||
enabled: false
|
||||
file: plugins/stream_redact.lua
|
||||
match:
|
||||
paths: ["/v1/chat/completions"]
|
||||
config:
|
||||
content_path: choices.*.delta.content
|
||||
max_buffer_bytes: 262144
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 最后一条 user 消息打标;会重写整个 body,默认关闭
|
||||
# ---------------------------------------------------------------------------
|
||||
- id: request-rewrite
|
||||
enabled: false
|
||||
file: plugins/request_rewrite.lua
|
||||
match:
|
||||
paths: ["/v1/chat/completions"]
|
||||
config:
|
||||
prefix: "[checked] "
|
||||
Reference in new issue
Block a user