跳到主要内容

Guard

Aidy 内置了 Guard 检测能力,可通过 GuardPluginGlobalConfigGuardPluginConfig 配置。

全局配置

check_service_url

用于指定 Guard 检测服务的 URL,例如 http://localhost:8080

备注

检测服务直连检测模型 API,如对应检测 API URL 为 http://localhost:8080/v1/chat/responses,则此处应配置为 http://localhost:8080

timeout

检测服务请求超时时间,默认 30s。类型为 google.protobuf.Duration,可使用字符串形式配置,如 "5s""1500ms"

当检测服务响应超时时,将跳过 guard 检测流程

路由配置

路由配置用于指定特定路由的 Guard 检测配置。请参见 路由配置 的 Guard 节。

输出

检测结果将与请求/响应日志一同输出,具体请看 日志文档 中的 guard 节。

行为

当检测到违规请求时,支持 block 和 log_only 两种模式,前者会阻断请求,而后者仅记录违规日志。

对于阻断的模式下,又有两种模式,一种是模拟输出的形式,一种是报错的形式。

对于 Chat 采用模拟输出的形式,会模拟 stream / non-stream 返回错误信息;对于 Embeddings 则采用报错的形式,返回 HTTP 412 错误。

阻断 - 报错

在阻断 + 报错模式下,Aidy 会返回如下的响应信息给客户端:

HTTP/1.1 412 Precondition Failed
Content-Type: application/json
X-Aidy-Request-Id: 01k58m8a3n490faqvfpt3n5m73
Date: Tue, 16 Sep 2025 06:44:05 GMT
Content-Length: 134
Connection: close

{"error":{"code":null,"message":"您的请求包含不安全内容,无法生成回复","param":null,"type":"security_guard_error"}}

当前,embedding 采用这一方式阻断;Chat 后续会支持使用这一方式阻断。