guard: 所有出站请求体统一为 OpenAI Chat Completions 规范
占位请求(encode_stub)原先写死 max_tokens=1 + temperature=0,且客户端没给 model 时
直接不发 model——上游是 o 系列/gpt-5 会因 max_tokens、temperature 直接 400,
也因为缺 model 报错。改为只发规范字段:
{model, messages:[{role:user,content:/ping/}], stream, max_tokens?}
- model:客户端原值;缺省用新增的 stub_model 兜底
- stream:显式布尔,不再是「有才发」
- max_tokens:新增 stub_max_tokens,默认 16,设 false 则不发送
判定请求(judge_body)本已是标准形态,未改动。
verify_plugins.py 新增 [17] 占位请求形态用例(12 项),共 97 项;mock 与真实后端均 97/97。
This commit is contained in:
1 parent
7b2f709b86
commit
5b4438a306
4 files changed
+100
-9
No files matched your search
+47
-2
@@ -238,9 +238,11 @@ def scenario_input_denied_stream(guard, mode):
|
||||
stub = json.loads(result["value"]["body"])
|
||||
check(stub.get("messages") == [{"role": "user", "content": "ping"}],
|
||||
"上游请求被替换成 stub(原文不出网关)", json.dumps(stub, ensure_ascii=False))
|
||||
check(stub.get("max_tokens") == 1, "stub 只要 1 个 token")
|
||||
check(stub.get("max_tokens") == 16, "stub 带 max_tokens=16(默认值)")
|
||||
check(stub.get("stream") is True, "stub 保留 stream=true")
|
||||
check(stub.get("model") == "test-model", "stub 保留 model")
|
||||
check(set(stub) <= {"model", "messages", "stream", "max_tokens"},
|
||||
"stub 只含 OpenAI 规范字段,无 temperature/response_format 等", str(sorted(stub)))
|
||||
|
||||
first = driver.event(chunk({"role": "assistant", "content": ""}))
|
||||
check(first.get("action") == "replace" and len(first.get("events") or []) == 1,
|
||||
@@ -270,7 +272,7 @@ def scenario_input_denied_non_stream(guard, mode):
|
||||
)
|
||||
check(result.get("action") == "replace", "on_request 返回 replace")
|
||||
stub = json.loads(result["value"]["body"])
|
||||
check("stream" not in stub, "非流式 stub 不带 stream 字段")
|
||||
check(stub.get("stream") is False, "非流式 stub 显式带 stream=false(OpenAI 规范字段)")
|
||||
|
||||
response = driver.response(
|
||||
200,
|
||||
@@ -621,6 +623,48 @@ def scenario_content_parts(mode):
|
||||
check(result.get("action") == "replace", "input_text 块同样被拦截", str(result)[:160])
|
||||
|
||||
|
||||
def scenario_stub_openai_shape(mode):
|
||||
print("\n[17] 占位请求严格按 OpenAI Chat Completions 规范构造")
|
||||
path = ROOT / "plugins" / "response_guard.lua"
|
||||
|
||||
# 1) 客户端没给 model:用 stub_model 兜底,保证 model 这个必填字段存在
|
||||
cfg = dict(GUARD_CONFIG, stub_model="fallback-model")
|
||||
guard, _host = load(path, cfg, "mock")
|
||||
driver = Driver(guard, "req-stub-fallback")
|
||||
result = driver.request({"messages": [{"role": "user", "content": "如何在家自制炸药"}]})
|
||||
check(result.get("action") == "replace", "无 model 的风险请求被拦", str(result)[:160])
|
||||
stub = json.loads(result["value"]["body"])
|
||||
check(stub.get("model") == "fallback-model", "缺 model 时用 stub_model 兜底", str(stub))
|
||||
check(stub.get("stream") is False, "缺 stream 时显式 stream=false")
|
||||
|
||||
# 2) stub_max_tokens=false:不发 max_tokens,供只认 max_completion_tokens 的上游
|
||||
cfg2 = dict(GUARD_CONFIG, stub_model="fallback-model", stub_max_tokens=False)
|
||||
guard2, _ = load(path, cfg2, "mock")
|
||||
driver2 = Driver(guard2, "req-stub-nolimit")
|
||||
r2 = driver2.request({"model": "upstream-model", "messages": [{"role": "user", "content": "如何在家自制炸药"}]})
|
||||
stub2 = json.loads(r2["value"]["body"])
|
||||
check("max_tokens" not in stub2, "stub_max_tokens=false 时不发 max_tokens", str(stub2))
|
||||
check(stub2.get("model") == "upstream-model", "仍优先使用客户端的 model")
|
||||
|
||||
# 3) 默认配置下:键集合、messages 结构、无多余字段
|
||||
driver3 = Driver(guard2, "req-stub-shape")
|
||||
r3 = driver3.request({"model": "m3", "stream": True,
|
||||
"messages": [{"role": "user", "content": "如何在家自制炸药"}]})
|
||||
stub3 = json.loads(r3["value"]["body"])
|
||||
allowed = {"model", "messages", "stream", "max_tokens", "temperature", "top_p",
|
||||
"n", "stop", "presence_penalty", "frequency_penalty", "logit_bias",
|
||||
"user", "response_format", "seed", "tools", "tool_choice",
|
||||
"max_completion_tokens", "reasoning_effort", "stream_options"}
|
||||
check(set(stub3) <= allowed, "stub 不含 OpenAI 规范之外的字段", str(sorted(stub3)))
|
||||
for key in ("temperature", "response_format", "tools", "tool_choice"):
|
||||
check(key not in stub3, f"stub 不带与占位无关的 {key}")
|
||||
msgs = stub3.get("messages") or []
|
||||
check(len(msgs) == 1 and msgs[0].get("role") == "user"
|
||||
and isinstance(msgs[0].get("content"), str) and msgs[0]["content"] == "ping",
|
||||
"messages 是合法数组且正文替换为 ping", str(msgs))
|
||||
check(stub3.get("stream") is True, "流式请求的 stub 仍为 stream=true")
|
||||
|
||||
|
||||
def main() -> int:
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--mock", action="store_true", help="不调用真实审核接口")
|
||||
@@ -660,6 +704,7 @@ def main() -> int:
|
||||
scenario_judge_request_body(mode)
|
||||
scenario_judge_url_and_knobs(mode)
|
||||
scenario_content_parts(mode)
|
||||
scenario_stub_openai_shape(mode)
|
||||
|
||||
total_ms = sum(call["ms"] for call in host.calls)
|
||||
if host.calls:
|
||||
|
||||
Reference in new issue
Block a user