Files
agent-safe-middleware/config.yaml
T
chenleixu b82e44ec32 feat: Docker 打包、交付链路与命名脱敏
镜像与构建
- 六阶段 Dockerfile(base/wheels/deps/manifest/core|guard),core 不含 torch
- 构建期自证:gen_manifest --strict 拦未登记模块,core 阶段重算 sha256
- compose 四服务 host 网络(llm.url 强制回环),config.core/local 可挂载覆盖

无 docker 环境的四段链路
- pull_oci_image.py      直拉 OCI 镜像 rootfs(纯标准库,走加速器)
- replay_image_build.sh  按 stage chroot 重放 Dockerfile
- run_image_chroot.sh    等价 docker run(uid 10001 + 入口 + host 网络)
- pack_image.py          封成 docker save 格式包,自带 diff_id 校验

交付
- make_package.sh 生成自包含目录:镜像包 + 溯源清单 + 构建输入 + 脚本 + 源码快照
- 文档:从零复现指南、docker 打包方案、镜像构建限制与替代方案、交付包 README

命名
- Qwen 品牌名统一替换为 large_model / small_model / large_model_guard,
  覆盖 vLLM 服务名、模型目录、配置、脚本、测试与文档
- 模型架构字段(architectures / model_type / tokenizer_class)属上游定义,
  改名会导致加载失败,保留原值;check_local_model.py 的架构判断同理

验证:smoke 6/6;test.py 20 通过 0 失败 1 警告;gen --strict 67 文件 / 22 进镜像
2026-11-27 18:20:16 +00:00

142 lines
5.2 KiB
YAML
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 上游主模型:8001 vLLM large_model(原 8000 ShieldAgent 已从方案移除)。
# 客户端请求体里的 model 字段需与 8001 的 --served-model-name 一致(large_model)。
upstream: http://127.0.0.1:8001
listen_host: 0.0.0.0
listen_port: 8080
limits:
max_sessions: 4
# 本地模型串行推理(max_concurrency 固定 1),按单段最慢几十毫秒到数秒留
hook_timeout_seconds: 120
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
# 两个守卫都走本机 vLLM(backend: vllm),宿主不再加载任何权重,启动即就绪。
# 判定契约没变:宿主仍按 "Safety: / Categories:" 解析,换模型/换启动参数导致
# 格式漂移就是整条判定 503。url 必须是本机回环,否则配置期就被拒。
llm:
- id: guard_model
type: guard
backend: vllm
url: http://127.0.0.1:8003
served_model: small_model
# 0.6B 的 chat template 不认 enable_thinking,这里不要设
limits:
max_input_tokens: 8192
max_new_tokens: 64
timeout_seconds: 10
max_concurrency: 16
queue_size: 64
- id: guard_8b
type: guard
backend: vllm
url: http://127.0.0.1:8005
served_model: large_model_guard
load:
enable_thinking: false # 必须;否则输出 <think> 推理,解析必挂
limits:
max_input_tokens: 4096 # 与 --max-model-len 对齐
max_new_tokens: 64
timeout_seconds: 20
max_concurrency: 8
queue_size: 32
plugins:
# 旧协议遗留,已停用:依赖宿主没有的 host.files,且未声明 llm.allow
- id: file-content-review
enabled: false
file: plugins/file_content_review.lua
match:
paths: ["/upload"]
config:
model: guard_model
allowed_extensions: [txt, doc, docx]
max_review_bytes: 8192
max_files: 8
# 旧协议遗留,已停用:外部审核服务白名单转发
- id: network-review
enabled: false
file: plugins/network_review.lua
match:
paths: ["/chat/stream"]
network:
allow:
- domain: review.example.com
scheme: https
ports: [443]
timeout_seconds: 5
max_response_bytes: 1048576
config:
url: https://review.example.com/check
fields: [messages]
headers: {x-api-key: "injected-by-admin"}
- id: response-guard
file: plugins/response_guard.lua
match:
paths: ["/v1/chat/completions"]
# 必须与 llm 段的 id 一致,否则 host.llm.call 一定失败;
# guard_8b 不写进 allow,8005 连探活都不会做,复核直接 LLMFailure
llm:
allow: [guard_model, guard_8b]
config:
model: guard_model
block_status: 400
# 二级复核:0.6B 判 Unsafe 后再问 8B。relax=只放松(8B 只能免罪,不能加罪),
# override=8B 终裁;review_scope 里加 Controversial 会显著增加调用量。
review_model: guard_8b
review_mode: relax
review_scope: [Unsafe]
# 类别名必须写全:8B 拼出 Violence 这种变体,parse_guard_output 会直接失败
review_prefix: |
You are a content safety classifier. Read the text and reply with EXACTLY two lines:
Safety: Safe|Unsafe|Controversial
Categories: Violent|Non-violent Illegal Acts|Sexual Content or Sexual Acts|PII|Suicide & Self-Harm|Unethical Acts|Politically Sensitive Topics|Copyright Violation|Jailbreak, or None
# buffered 则整条响应只判一次,但客户端看不到增量
stream_mode: incremental
flush_chars: 512
force_flush_bytes: 4096
# 8 段 × judge_chunk_bytes
max_buffer_bytes: 65536
judge_chunk_bytes: 8192
check_input: true
check_output: true
# Controversial 也按不安全处理;误伤多时设 false
block_controversial: true
# 多轮对话:相邻同角色消息先合并成一"轮",当前轮 = 最后一轮 user,
# 更早的轮次按字节压成有界上下文附在前面,历史不重复送检。
# 只按字节限量(最近的历史优先),避免几句短消息把关键轮次挤出窗口;
# 0 表示只判当前轮,历史完全不进判定。
multiturn_context_bytes: 2048
# 拦截后替换给上游的占位请求:客户端没给 model 时用它兜底
# stub_model: your-upstream-model
# 拦截后发往上游的是 {"role":"user","content":"ping"},回复必定被丢弃换成 400。
# 实测:=16 时上游要跑满 16 token(原版 large_model 全花在 <think> 上)→ 883ms;
# =1 只需 prefill + 1 token → ~80ms。F3 端到端 1.84s → 约 1.0s。
stub_max_tokens: 1
- 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
- id: request-rewrite
enabled: false
file: plugins/request_rewrite.lua
match:
paths: ["/v1/chat/completions"]
config:
prefix: "[checked] "