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
+77
-10
@@ -7,6 +7,9 @@
|
||||
-- 请求 : POST /v1/chat/completions {model, messages[], stream}
|
||||
-- 非流式 : choices[*].message.content
|
||||
-- 流式 : choices[*].delta.content + 终止帧 data: [DONE]
|
||||
-- 判定请求体(见 judge_body):model / messages[{role,content}] / stream=false
|
||||
-- 固定为 JSON 数组与显式 false;temperature、生成上限参数名、response_format、
|
||||
-- reasoning_effort 按后端能力配置,默认值对 DeepSeek 一类兼容服务可直接跑。
|
||||
--
|
||||
-- 输出契约(客户端只会看到 OpenAI 形态)
|
||||
-- 非流式拦截 : HTTP 400 + {"error":{"message","type":"invalid_request_error",
|
||||
@@ -539,24 +542,38 @@ local function judge_user_payload(kind, context, part, index, total)
|
||||
return table.concat(lines, "\n")
|
||||
end
|
||||
|
||||
local function judge_call(payload_text)
|
||||
local request_body = {
|
||||
-- OpenAI Chat Completions 请求体。
|
||||
-- messages 必须是真正的 JSON 数组(Lua 里写成稠密整数表即可,宿主按数组编码);
|
||||
-- 可选字段按后端能力开关,默认组合与 DeepSeek 一类兼容服务一致:
|
||||
-- temperature=0 / max_tokens / response_format=json_object / stream=false
|
||||
-- gpt-5、o 系列只认 max_completion_tokens 且拒绝 temperature=0,换后端时改配置。
|
||||
local function judge_body(payload_text)
|
||||
local body = {
|
||||
model = config.model,
|
||||
messages = {
|
||||
{ role = "system", content = config.system_prompt },
|
||||
{ role = "user", content = payload_text },
|
||||
},
|
||||
temperature = 0,
|
||||
max_tokens = config.judge_max_tokens,
|
||||
-- 判定只解析完整 JSON 响应体,显式声明不流式,避免后端默认开流
|
||||
stream = false,
|
||||
response_format = { type = "json_object" },
|
||||
}
|
||||
-- 推理型模型可以在判定前刷掉大量思维链;后端支持时设成 none 可显著降时延,
|
||||
if config.judge_temperature ~= nil then
|
||||
body.temperature = config.judge_temperature
|
||||
end
|
||||
body[config.judge_token_param] = config.judge_max_tokens
|
||||
if config.judge_json_mode then
|
||||
body.response_format = { type = "json_object" }
|
||||
end
|
||||
-- 推理型模型会在判定前刷掉大量思维链;后端支持时设成 none 可显著降时延,
|
||||
-- 同时避免 max_tokens 被思维链吃光导致 content 为空。不支持就留空。
|
||||
if config.judge_reasoning_effort then
|
||||
request_body.reasoning_effort = config.judge_reasoning_effort
|
||||
body.reasoning_effort = config.judge_reasoning_effort
|
||||
end
|
||||
return body
|
||||
end
|
||||
|
||||
local function judge_call(payload_text)
|
||||
local request_body = judge_body(payload_text)
|
||||
local ok_encode, body = pcall(host.json.encode, request_body)
|
||||
if not ok_encode or type(body) ~= "string" then
|
||||
return nil, "judge_request_encode_failed"
|
||||
@@ -622,12 +639,16 @@ end
|
||||
-- 请求解析
|
||||
-- ---------------------------------------------------------------------------
|
||||
|
||||
-- 正文既可能是字符串,也可能是内容块数组。只取文本块:
|
||||
-- chat 风格 {type="text"},Responses 风格 {type="input_text"} 都要认,
|
||||
-- 否则客户端换一种写法就能绕过输入审核(图片块没有 text 字段,天然跳过)。
|
||||
local function message_text(content)
|
||||
if type(content) == "string" then return content end
|
||||
if type(content) ~= "table" then return nil end
|
||||
local parts = {}
|
||||
for _, part in ipairs(content) do
|
||||
if type(part) == "table" and part.type == "text" and type(part.text) == "string" then
|
||||
if type(part) == "table" and type(part.text) == "string"
|
||||
and (part.type == "text" or part.type == "input_text") then
|
||||
parts[#parts + 1] = part.text
|
||||
end
|
||||
end
|
||||
@@ -657,7 +678,9 @@ local function collect_user_text(decoded)
|
||||
return table.concat(texts, "\n\n---\n\n")
|
||||
end
|
||||
|
||||
-- 判定不通过时发往上游的最小请求:原文不出网关,也不浪费一次生成长文本
|
||||
-- 判定不通过时发往上游的最小请求:原文不出网关,也不浪费一次生成长文本。
|
||||
-- 只需要一个 200 让宿主走正常响应通道,正文随即被 on_response / on_event 换掉;
|
||||
-- 部分网关会把 max_tokens 抬到自己的下限(实测 1 会被抬到 128),不影响结果。
|
||||
local function encode_stub(state)
|
||||
local body = {
|
||||
messages = { { role = "user", content = "ping" } },
|
||||
@@ -760,6 +783,20 @@ local function boolean(value, default)
|
||||
error("response_guard: boolean config value must be true or false")
|
||||
end
|
||||
|
||||
-- base_url 的三种写法都要落到同一个端点:
|
||||
-- https://host -> https://host/v1/chat/completions
|
||||
-- https://host/v1 -> https://host/v1/chat/completions
|
||||
-- https://host/v1/chat/completions(完整端点) -> 原样使用
|
||||
local function build_judge_url(base_url)
|
||||
if base_url:match("/chat/completions$") then return base_url end
|
||||
if base_url:match("^https?://[^/]+$") then return base_url .. "/v1/chat/completions" end
|
||||
return base_url .. "/chat/completions"
|
||||
end
|
||||
|
||||
local function has_json_keyword(text)
|
||||
return text:lower():find("json", 1, true) ~= nil
|
||||
end
|
||||
|
||||
function plugin.init(ctx, options)
|
||||
if type(options) ~= "table" then
|
||||
error("response_guard config must be a table")
|
||||
@@ -774,6 +811,7 @@ function plugin.init(ctx, options)
|
||||
error("response_guard: config.base_url must start with http:// or https://")
|
||||
end
|
||||
base_url = base_url:gsub("/+$", "")
|
||||
local judge_url = build_judge_url(base_url)
|
||||
|
||||
if type(o.api_key) ~= "string" or o.api_key == "" then
|
||||
error("response_guard requires config.api_key")
|
||||
@@ -786,11 +824,37 @@ function plugin.init(ctx, options)
|
||||
error("response_guard requires config.model")
|
||||
end
|
||||
|
||||
-- response_format=json_object 的后端要求 messages 里出现 "json",
|
||||
-- 自定义 system_prompt 漏了这个词会被 400 顶回来,这里补一句兜底。
|
||||
local json_mode = boolean(o.judge_json_mode, true)
|
||||
local system_prompt = o.system_prompt
|
||||
if system_prompt == nil then system_prompt = DEFAULT_SYSTEM_PROMPT end
|
||||
if type(system_prompt) ~= "string" or system_prompt == "" then
|
||||
error("response_guard: config.system_prompt must be a non-empty string")
|
||||
end
|
||||
if json_mode and not has_json_keyword(system_prompt) then
|
||||
system_prompt = system_prompt .. "\nReply with a single JSON object."
|
||||
end
|
||||
|
||||
-- 生成上限的参数名:OpenAI 新模型只认 max_completion_tokens
|
||||
local token_param = o.judge_token_param or "max_tokens"
|
||||
if token_param ~= "max_tokens" and token_param ~= "max_completion_tokens" then
|
||||
error("response_guard: config.judge_token_param must be 'max_tokens' or 'max_completion_tokens'")
|
||||
end
|
||||
|
||||
-- temperature:数字即显式设置;false 表示不发送该字段(o 系列/gpt-5 只接受默认值)
|
||||
local temperature = o.judge_temperature
|
||||
if temperature == nil then
|
||||
temperature = 0
|
||||
elseif temperature == false then
|
||||
temperature = nil
|
||||
else
|
||||
local number = tonumber(temperature)
|
||||
if not number or number < 0 or number > 2 then
|
||||
error("response_guard: config.judge_temperature must be a number within 0..2, or false")
|
||||
end
|
||||
temperature = number
|
||||
end
|
||||
|
||||
-- 单个路径字符串或多路径数组都接受;多路径用于同时覆盖正文与工具调用参数
|
||||
local stream_paths = parse_paths(o.content_path_stream, DEFAULT_STREAM_PATH)
|
||||
@@ -825,10 +889,13 @@ function plugin.init(ctx, options)
|
||||
end
|
||||
|
||||
config = {
|
||||
judge_url = base_url .. "/chat/completions",
|
||||
judge_url = judge_url,
|
||||
api_key = o.api_key,
|
||||
model = o.model,
|
||||
system_prompt = system_prompt,
|
||||
judge_json_mode = json_mode,
|
||||
judge_token_param = token_param,
|
||||
judge_temperature = temperature,
|
||||
judge_max_tokens = positive_int(o.judge_max_tokens, 512, "judge_max_tokens"),
|
||||
judge_reasoning_effort = reasoning_effort,
|
||||
judge_chunk_bytes = judge_chunk_bytes,
|
||||
|
||||
Reference in new issue
Block a user