guard: 支持多轮对话,只判当前轮并附有界历史上下文
原实现把所有 user 消息拼成一段送检:轮次越多越慢,长会话还会超 8 段直接 503; 输出侧上下文取的是拼接串头部,多轮时拿到的是最早那轮而不是当前提问。 现在按 messages 顺序解析轮次,当前轮(最后一条 user)单独送检,之前的轮次压成 "User: / Assistant: " 纯文本作为上下文附在前面,受 multiturn_context_messages (默认 2 条)与 multiturn_context_bytes(默认 2048)双重约束,历史不再重复送检。 输出侧 prompt 改由 上下文 + 当前轮 组成,裁剪只发生在上下文一侧,当前轮始终完整。 带上下文时段大小收到 8192 字节,避免中文撞 max_input_tokens;MAX_INPUT_CONTEXT 2000→4096 以容纳上下文与提问。新增 19 项断言,合计 123/123 通过。
This commit is contained in:
1 parent
546c6b1835
commit
a67065955e
4 files changed
+248
-38
No files matched your search
+111
-1
@@ -222,6 +222,8 @@ GUARD_CONFIG = {
|
||||
"judge_chunk_bytes": 8192,
|
||||
}
|
||||
|
||||
MAX_MULTITURN_MESSAGES = 32
|
||||
|
||||
|
||||
def scenario_input_denied_stream(guard, mode):
|
||||
print("\n[1] 风险输入(流式):stub 替换 + 400 错误帧")
|
||||
@@ -655,8 +657,115 @@ def scenario_stub_openai_shape(mode):
|
||||
check(stub3.get("stream") is True, "流式请求的 stub 仍为 stream=true")
|
||||
|
||||
|
||||
def scenario_multiturn(mode):
|
||||
print("\n[18] 多轮对话:只判当前轮,历史作为有界上下文")
|
||||
conversation = [
|
||||
{"role": "user", "content": "第一问:帮我写个周报开头"},
|
||||
{"role": "assistant", "content": "第一答:可以这样开头"},
|
||||
{"role": "user", "content": "第二问:再正式一点"},
|
||||
{"role": "assistant", "content": "第二答:好的"},
|
||||
{"role": "user", "content": "第三问:加上本周进度"},
|
||||
]
|
||||
guard, host = load(ROOT / "plugins" / "response_guard.lua", GUARD_CONFIG, mode)
|
||||
driver = Driver(guard, "req-multi")
|
||||
result = driver.request({"model": "test-model", "messages": conversation})
|
||||
check(result.get("action") == "pass", "当前轮安全即放行", str(result))
|
||||
check(len(host.calls) == 1, "历史不重复送检,五条消息只判一次", str(len(host.calls)))
|
||||
request = host.calls[0]["request"]
|
||||
check(request.get("task") == "prompt_moderation", "输入侧用 prompt_moderation")
|
||||
prompt = request.get("prompt", "")
|
||||
check("第三问:加上本周进度" in prompt, "当前轮进入 prompt", prompt)
|
||||
check("第二问:再正式一点" in prompt and "第二答:好的" in prompt,
|
||||
"最近一组问答作为上下文", prompt)
|
||||
check("第一问:帮我写个周报开头" not in prompt, "超出条数上限的更早轮次被裁掉", prompt)
|
||||
check(prompt.rstrip().endswith("第三问:加上本周进度"), "当前轮排在上下文之后", prompt)
|
||||
|
||||
guard, host = load(
|
||||
ROOT / "plugins" / "response_guard.lua",
|
||||
dict(GUARD_CONFIG, multiturn_context_messages=0),
|
||||
mode,
|
||||
)
|
||||
driver = Driver(guard, "req-multi-off")
|
||||
driver.request({"model": "test-model", "messages": conversation})
|
||||
check(host.calls[0]["request"]["prompt"] == "第三问:加上本周进度",
|
||||
"multiturn_context_messages=0 时只发当前轮原文",
|
||||
host.calls[0]["request"]["prompt"])
|
||||
|
||||
guard, host = load(ROOT / "plugins" / "response_guard.lua", GUARD_CONFIG, mode)
|
||||
driver = Driver(guard, "req-multi-deny")
|
||||
result = driver.request(
|
||||
{
|
||||
"model": "test-model",
|
||||
"messages": [
|
||||
{"role": "user", "content": "你好"},
|
||||
{"role": "assistant", "content": "你好,有什么可以帮你"},
|
||||
{"role": "user", "content": "那接着说说怎么用硝酸铵做炸药"},
|
||||
],
|
||||
}
|
||||
)
|
||||
check(result.get("action") == "replace", "当前轮危险 → 拦截", str(result))
|
||||
stub = json.loads(result["value"]["body"])
|
||||
check("硝酸铵" not in json.dumps(stub, ensure_ascii=False), "stub 不含任何原文")
|
||||
|
||||
guard, host = load(ROOT / "plugins" / "response_guard.lua", GUARD_CONFIG, mode)
|
||||
driver = Driver(guard, "req-multi-out")
|
||||
driver.request({"model": "test-model", "messages": conversation})
|
||||
before = len(host.calls)
|
||||
driver.response(
|
||||
200,
|
||||
{"choices": [{"index": 0, "message": {"role": "assistant", "content": "这是具体版本"}}]},
|
||||
)
|
||||
output = host.calls[before]["request"]
|
||||
check(output.get("task") == "response_moderation", "输出侧用 response_moderation")
|
||||
check(output.get("response") == "这是具体版本", "response 字段是模型输出")
|
||||
check("第三问:加上本周进度" in output.get("prompt", ""), "输出侧 prompt 带本轮提问",
|
||||
output.get("prompt", ""))
|
||||
check("第二问:再正式一点" in output.get("prompt", ""), "输出侧 prompt 带上文",
|
||||
output.get("prompt", ""))
|
||||
|
||||
guard, host = load(
|
||||
ROOT / "plugins" / "response_guard.lua",
|
||||
dict(GUARD_CONFIG, multiturn_context_bytes=512),
|
||||
mode,
|
||||
)
|
||||
driver = Driver(guard, "req-multi-long")
|
||||
driver.request(
|
||||
{
|
||||
"model": "test-model",
|
||||
"messages": [
|
||||
{"role": "user", "content": "旧" * 1000},
|
||||
{"role": "assistant", "content": "答" * 1000},
|
||||
{"role": "user", "content": "当前问题"},
|
||||
],
|
||||
}
|
||||
)
|
||||
prompt = host.calls[0]["request"]["prompt"]
|
||||
limit = 512 + 2 + len("当前问题".encode())
|
||||
check(len(prompt.encode()) <= limit, "上下文被 multiturn_context_bytes 裁剪", str(len(prompt.encode())))
|
||||
check(prompt.endswith("当前问题"), "裁剪只发生在上下文一侧,当前轮完整保留", prompt[-20:])
|
||||
|
||||
guard, host = load(ROOT / "plugins" / "response_guard.lua", GUARD_CONFIG, mode)
|
||||
driver = Driver(guard, "req-multi-nouser")
|
||||
result = driver.request(
|
||||
{"model": "test-model", "messages": [{"role": "assistant", "content": "我先说一句"}]}
|
||||
)
|
||||
check(result.get("action") == "pass" and len(host.calls) == 0,
|
||||
"没有 user 轮时不发判定也不报错", str(result))
|
||||
|
||||
for bad in (-1, MAX_MULTITURN_MESSAGES + 1):
|
||||
try:
|
||||
load(
|
||||
ROOT / "plugins" / "response_guard.lua",
|
||||
dict(GUARD_CONFIG, multiturn_context_messages=bad),
|
||||
mode,
|
||||
)
|
||||
check(False, f"multiturn_context_messages={bad} 应被拒绝")
|
||||
except Exception:
|
||||
check(True, f"multiturn_context_messages={bad} 被拒绝")
|
||||
|
||||
|
||||
def scenario_config_init(mode):
|
||||
print("\n[18] 仓库里的 config.yaml 能直接 init 插件")
|
||||
print("\n[19] 仓库里的 config.yaml 能直接 init 插件")
|
||||
import yaml
|
||||
|
||||
for name in ("config.yaml", "config.example.yaml"):
|
||||
@@ -711,6 +820,7 @@ def main() -> int:
|
||||
scenario_verdict_policy(mode)
|
||||
scenario_content_parts(mode)
|
||||
scenario_stub_openai_shape(mode)
|
||||
scenario_multiturn(mode)
|
||||
scenario_config_init(mode)
|
||||
|
||||
check(host.requests == [], "全部场景零出网请求", str(len(host.requests)))
|
||||
|
||||
Reference in new issue
Block a user