API7 网关 AI Agent Skill:AI 内容审核插件
概览
API7 企业版提供两个内容安全审核插件,用于过滤有害内容,覆盖 LLM 请求和响应:
| 插件 | 服务提供方 | 请求 | 响应 | 流式传输 |
|---|---|---|---|---|
ai-aws-content-moderation | AWS Comprehend | ✅ | ❌ | ❌ |
ai-aliyun-content-moderation | Aliyun Moderation Plus | ✅ | ✅ | ✅ |
两者都必须与 ai-proxy 或 ai-proxy-multi 一起使用。
适用场景
- 在内容到达 LLM 前拦截有毒、仇恨或色情内容
- 在有害 LLM 响应到达客户端前进行过滤(仅阿里云)
- 通过可配置阈值执行内容策略
- 直接在服务或路由上应用一致的审核策略
插件执行顺序
ai-prompt-template (priority 1071)
ai-prompt-decorator (priority 1070)
ai-aws-content-moderation (priority 1050) ← 在 ai-proxy 之前运行
ai-proxy (priority 1040)
ai-aliyun-content-moderation (priority 1029) ← 在 ai-proxy 之后运行
插件 1:ai-aws-content-moderation
使用 AWS Comprehend detectToxicContent API 对请求内容进行评分。
配置参考
| 字段 | 类型 | 是否必填 | 默认值 | 说明 |
|---|---|---|---|---|
comprehend.access_key_id | string | 是 | — | AWS 访问密钥 ID |
comprehend.secret_access_key | string | 是 | — | AWS 秘密访问密钥 |
comprehend.region | string | 是 | — | AWS 区域,例如 us-east-1 |
moderation_categories | object | 否 | — | 按类别设置阈值(0-1) |
moderation_threshold | number | 否 | 0.5 | 整体有害内容阈值(0-1) |
分步操作:AWS 内容审核
所有运行时资源都必须使用 --gateway-group 或 -g 限定到某个网关组。
a7 route create -g default -f - <<'EOF'
{
"id": "moderated-chat",
"uri": "/v1/chat/completions",
"methods": ["POST"],
"plugins": {
"ai-aws-content-moderation": {
"comprehend": {
"access_key_id": "AKIAIOSFODNN7EXAMPLE",
"secret_access_key": "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY",
"region": "us-east-1"
},
"moderation_categories": {
"HATE_SPEECH": 0.3,
"VIOLENCE_OR_THREAT": 0.2
}
},
"ai-proxy": {
"provider": "openai",
"auth": {
"header": {
"Authorization": "Bearer sk-your-key"
}
},
"options": {
"model": "gpt-4"
}
}
}
}
EOF
插件 2:ai-aliyun-content-moderation
使用阿里云内容安全增强版,支持请求审核、响应审核和实时流式审核。
配置参考
| 字段 | 类型 | 是否必填 | 默认值 | 说明 |
|---|---|---|---|---|
endpoint | string | 是 | — | 阿里云服务端点 URL |
region_id | string | 是 | — | 阿里云区域,例如 cn-shanghai |
access_key_id | string | 是 | — | 阿里云访问密钥 ID |
access_key_secret | string | 是 | — | 阿里云访问密钥 |
check_request | boolean | 否 | true | 启用请求审核 |
check_response | boolean | 否 | false | 启用响应审核 |
risk_level_bar | string | 否 | high | 阈值:none、low、medium、high、max |