Skip to content

处理器管道

LLM Fence 的核心请求处理通过可插拔管道实现。处理器配置存储在运行配置 processors 字段中,由管理端或 /api/admin/config/processors/* 更新。

三阶段

text
Client -> Auth -> PreProcessor -> Adapter -> Upstream
                              <- PostProcessor <-
                         Observer async
阶段用途
PreProcessor请求转发前校验或改写
PostProcessor响应返回前改写或补齐
Observer请求完成后旁路记录或异步分析

当前处理器

名称阶段说明
client_guardPreProcessor根据请求特征识别客户端类型,并校验 Key 的 allowed_clients
model_guardPreProcessor校验 Key 的 allowed_models
ip_guardPreProcessor校验 Key 的 allowed_ips
format_converterPre/Post/StreamOpenAI 与 Anthropic 格式互转
stream_usage_optionsStream处理流式用量相关选项
request_loggerObserver异步写入 request_logs
auditObserver旁路 AI 审计,写入 audit_logs

配置

audit 外,普通处理器只接受 enabled 字段:

json
{
  "client_guard": { "enabled": true },
  "model_guard": { "enabled": true },
  "ip_guard": { "enabled": true },
  "request_logger": { "enabled": true },
  "format_converter": { "enabled": true },
  "stream_usage_options": { "enabled": true }
}

如果给这些处理器添加额外字段,运行配置校验会拒绝提交。旧版 client_guard.detection 已被兼容性归一化移除,不再作为有效配置。

client_guard

识别的客户端类型:

  • codex / codex-lenient
  • cc / cc-lenient
  • audit
  • unknown

Key 策略示例:

json
{
  "allowed_clients": ["codex", "cc"]
}

model_guard

Key 策略示例:

json
{
  "allowed_models": ["gpt-4o", "deepseek-chat"]
}

白名单为空或未配置时不限制模型。

ip_guard

IP 来源优先级:

  1. Key-Client-IP
  2. X-Forwarded-For 的第一个地址
  3. 直连 remote_addr

Key 策略示例:

json
{
  "allowed_ips": ["10.0.0.0/8", "192.168.1.100"]
}

format_converter

Provider 使用 supported_formatsauto_convert 决定是否需要转换。

当客户端请求格式不在上游原生 supported_formats 中,且 auto_convert = trueformat_converter.enabled = true 时,LLM Fence 会转换到 supported_formats[0] 对应的上游格式。

request_logger

异步记录:

  • 本地 request_id 和上游 upstream_request_id
  • Key 名称和 Key ID
  • 模型、上游模型、Provider、endpoint
  • 流式标记、耗时、TTFT、客户端类型
  • Token 用量和错误来源

audit

审计是独立配置较多的 Observer,见 AI 审计模块