实施 AI 安全护栏并保护 PII
本指南介绍如何使用 ai-prompt-guard、ai-aws-content-moderation 和 ai-request-rewrite,通过 API7 AI 网关实施分层 AI 安全控制。
概览
在网关层实施安全护栏最为有效,因为策略可以集中管理并跨应用一致执行。实用的纵深防御模型包含三层:
- 提示词过滤:在调用模型前阻止提示词注入和禁止的指令。
- 内容审核:检测有害内容类别并拒绝高风险请求。
- PII 脱敏:在请求发往模型服务提供方前遮蔽敏感数据。
前置条件
-
安装 Docker。
-
安装 cURL,用于发送请求并验证服务。
-
拥有一个正在运行的 API7 网关实例。
-
从控制台获取令牌,并保存到环境变量:
export API_KEY=your-dashboard-token # 请替换为你的控制台令牌 -
将
{gateway_group_id}替换为网关组 ID。如果正在按照快速入门操作,请使用default。 -
如果使用 Admin API 示例,请创建或复用一个服务。如果尚无服务,请按照创建或复用服务操作,然后保存其 ID:
export SERVICE_ID=your-service-id # 请替换为你的服务 ID
提示词保护
使用 ai-prompt-guard 应用基于 PCRE 的允许和拒绝模式。此示例仅检查用户消息(match_all_roles: false)和最新消息(match_all_conversation_history: false)。匹配拒绝模式时,以 HTTP 400 拒绝请求。
- Admin API
- ADC
allow_pattern='(?i)^(what|how|why|explain|summarize|translate)\\b'
deny_pattern='(?i)(ignore\\s+all\\s+previous\\s+instructions|reveal\\s+system\\s+prompt|bypass\\s+guardrails)'
curl -k "https://localhost:7443/apisix/admin/routes?gateway_group_id={gateway_group_id}" -X PUT \
-H "X-API-KEY: ${API_KEY}" \
--data-binary @- <<EOF
{
"id": "ai-guardrails-prompt-protection",
"service_id": "$SERVICE_ID",
"paths": ["/ai/chat"],
"plugins": {
"ai-prompt-guard": {
"allow_patterns": ["$allow_pattern"],
"deny_patterns": ["$deny_pattern"],
"match_all_roles": false,
"match_all_conversation_history": false
},
"ai-proxy": {
"provider": "openai",
"auth": { "header": { "Authorization": "Bearer $OPENAI_API_KEY" } },
"options": { "model": "gpt-4o" }
}
}
}
EOF
❶ allow_patterns 使用 PCRE 语法定义允许的提示词形式。
❷ deny_patterns 阻止已知注入和绕过策略的短语。
❸ match_all_roles: false 和 match_all_conversation_history: false 将匹配范围限定为最新的 user 消息。
services:
- name: AI Prompt Protection
routes:
- uris:
- /ai/chat
name: ai-guardrails-prompt-protection
plugins:
ai-prompt-guard:
allow_patterns:
- (?i)^(what|how|why|explain|summarize|translate)\b
deny_patterns:
- (?i)(ignore\s+all\s+previous\s+instructions|reveal\s+system\s+prompt|bypass\s+guardrails)
match_all_roles: false
match_all_conversation_history: false
ai-proxy:
provider: openai
auth:
header:
Authorization: "Bearer ${OPENAI_API_KEY}"
options:
model: gpt-4o
❶ allow_patterns 使用 PCRE 语法定义允 许的提示词形式。
❷ deny_patterns 阻止已知注入和绕过策略的短语。
❸ match_all_roles: false 和 match_all_conversation_history: false 将匹配范围限定为最新的 user 消息。
adc sync -f adc.yaml
完整配置说明请参阅 ai-prompt-guard。
内容审核
内容审核为有害或滥用性文本增加第二层过滤。
AWS Comprehend 集成
使用 ai-aws-content-moderation 按 0–1 阈值对六个审核类别评分。可在 moderation_categories 中设置分类阈值,并通过 moderation_threshold 设置总体有害性阈值。超过任一阈值的请求可使用自定义状态码和消息阻止。
- Admin API
- ADC
curl -k "https://localhost:7443/apisix/admin/routes?gateway_group_id={gateway_group_id}" -X PUT \
-H "X-API-KEY: ${API_KEY}" \
--data-binary @- <<EOF
{
"id": "ai-guardrails-content-moderation",
"service_id": "$SERVICE_ID",
"paths": ["/ai/chat"],
"plugins": {
"ai-aws-content-moderation": {
"comprehend": {
"access_key_id": "$AWS_ACCESS_KEY_ID",
"secret_access_key": "$AWS_SECRET_ACCESS_KEY",
"region": "us-east-1"
},
"moderation_categories": {
"PROFANITY": 0.5,
"HATE_SPEECH": 0.5,
"INSULT": 0.5,
"HARASSMENT_OR_ABUSE": 0.5,
"SEXUAL": 0.5,
"VIOLENCE_OR_THREAT": 0.5
},
"moderation_threshold": 0.5
},
"ai-proxy": {
"provider": "openai",
"auth": { "header": { "Authorization": "Bearer $OPENAI_API_KEY" } },
"options": { "model": "gpt-4o" }
}
}
}
EOF
❶ comprehend 提供调用 Comprehend API 的 AWS 凭证,必须配置 access_key_id、secret_access_key 和 region。
❷ 为 PROFANITY、HATE_SPEECH、INSULT、HARASSMENT_OR_ABUSE、SEXUAL 和 VIOLENCE_OR_THREAT 配置分类阈值。
❸ moderation_threshold 定义总体有害性阈值;使用 moderation_categories 在全局阈值之外实施特定类别阈值。
services:
- name: AI Content Moderation
routes:
- uris:
- /ai/chat
name: ai-guardrails-content-moderation
plugins:
ai-aws-content-moderation:
comprehend:
access_key_id: ${AWS_ACCESS_KEY_ID}
secret_access_key: ${AWS_SECRET_ACCESS_KEY}
region: us-east-1
moderation_categories:
PROFANITY: 0.5
HATE_SPEECH: 0.5
INSULT: 0.5
HARASSMENT_OR_ABUSE: 0.5
SEXUAL: 0.5
VIOLENCE_OR_THREAT: 0.5
moderation_threshold: 0.5
ai-proxy:
provider: openai
auth:
header:
Authorization: "Bearer ${OPENAI_API_KEY}"
options:
model: gpt-4o
❶ comprehend 提供调用 Comprehend API 的 AWS 凭证,必须配置 access_key_id、secret_access_key 和 region。
❷ 为 PROFANITY、HATE_SPEECH、INSULT、HARASSMENT_OR_ABUSE、SEXUAL 和 VIOLENCE_OR_THREAT 配置分类阈值。
❸ moderation_threshold 定义总体有害性阈值;使用 moderation_categories 在全局阈值之外实施特定类别阈值。
adc sync -f adc.yaml
完整配置说明请参阅 ai-aws-content-moderation。
自定义审核服务
如果使用自定义审核技术栈,可以通过 ai-request-rewrite 调用专用审核模型,在转发到主大语言模型路由前拒绝或净化内容。此方式适用于自定义分类体系、语言覆盖或组织专用策略。
PII 脱敏
PII 保护可防止姓名、电话号码、账户标 识符等敏感字段意外暴露给外部模型服务提供方。网关侧脱敏也有助于满足 GDPR、HIPAA 和 SOC 2 合规控制。
请求侧 PII 遮蔽
使用 ai-request-rewrite 把传入提示词发送给独立模型,在请求到达主大语言模型前检测并遮蔽 PII。
- Admin API
- ADC
curl -k "https://localhost:7443/apisix/admin/routes?gateway_group_id={gateway_group_id}" -X PUT \
-H "X-API-KEY: ${API_KEY}" \
--data-binary @- <<EOF
{
"id": "ai-guardrails-pii-request",
"service_id": "$SERVICE_ID",
"paths": ["/ai/chat"],
"plugins": {
"ai-request-rewrite": {
"provider": "openai",
"auth": {
"header": {
"Authorization": "Bearer $OPENAI_API_KEY"
}
},
"options": {
"model": "gpt-4o"
},
"prompt": "Detect and redact PII in the incoming user text. Replace emails with [REDACTED_EMAIL], phone numbers with [REDACTED_PHONE], payment card numbers with [REDACTED_CARD], and government identifiers with [REDACTED_ID]. Return only sanitized text."
},
"ai-proxy": {
"provider": "openai",
"auth": { "header": { "Authorization": "Bearer $OPENAI_API_KEY" } },
"options": { "model": "gpt-4o" }
}
}
}
EOF
❶ provider 设置用于重写决策的模型后端。
❷ auth 配置调用重写模型的凭证。
❸ options.model 选择重写模型;prompt 定义遮蔽指令。
services:
- name: AI Request-Side PII Masking
routes:
- uris:
- /ai/chat
name: ai-guardrails-pii-request
plugins:
ai-request-rewrite:
provider: openai
auth:
header:
Authorization: "Bearer ${OPENAI_API_KEY}"
options:
model: gpt-4o
prompt: >-
Detect and redact PII in the incoming user text. Replace emails with
[REDACTED_EMAIL], phone numbers with [REDACTED_PHONE], payment card
numbers with [REDACTED_CARD], and government identifiers with
[REDACTED_ID]. Return only sanitized text.
ai-proxy:
provider: openai
auth:
header:
Authorization: "Bearer ${OPENAI_API_KEY}"
options:
model: gpt-4o
❶ provider 设置用于重写决策的模型后端。
❷ auth 配置调用重写模型的凭证。
❸ options.model 选择重写模型;prompt 定义遮蔽指令。
adc sync -f adc.yaml
响应侧 PII 过滤
同一 ai-request-rewrite 模式也可在模型输出返回客户端前进行净化。使用面向响应的重写提示词遮蔽生成的 PII,例如模型响应中的姓名、电话号码和 ID。
{
"ai-request-rewrite": {
"provider": "openai",
"auth": {
"header": {
"Authorization": "Bearer YOUR_API_KEY"
}
},
"options": {
"model": "gpt-4o"
},
"prompt": "Review generated text and mask any detected PII before returning it to clients."
}
}
完整配置说明请参阅 ai-request-rewrite。
组合安全护栏
大多数部署应在同一路由组合三类控制。实用的执行顺序如下:
- 首先使用
ai-prompt-guard尽早拒绝明显的提示词注入。 - 使用
ai-request-rewrite净化请求内容并移除 PII。 - 使用
ai-aws-content-moderation对有害内容评分并阻止。 - 最后由
ai-proxy将通过审核的流量转发到目标大语言模型。
插件执行顺序由插件优先级(阶段和内部排序)决定,而不是配置中的排列顺序。以上顺序反映各插件既定优先级下的预期运行时行为。
- Admin API
- ADC
allow_pattern='(?i)^.{1,4000}$'
deny_pattern='(?i)(ignore\\s+all\\s+previous\\s+instructions|reveal\\s+system\\s+prompt|developer\\s+mode)'
curl -k "https://localhost:7443/apisix/admin/routes?gateway_group_id={gateway_group_id}" -X PUT \
-H "X-API-KEY: ${API_KEY}" \
--data-binary @- <<EOF
{
"id": "ai-guardrails-combined",
"service_id": "$SERVICE_ID",
"paths": ["/ai/chat"],
"plugins": {
"ai-prompt-guard": {
"allow_patterns": ["$allow_pattern"],
"deny_patterns": ["$deny_pattern"],
"match_all_roles": false,
"match_all_conversation_history": false
},
"ai-request-rewrite": {
"provider": "openai",
"auth": {
"header": {
"Authorization": "Bearer $OPENAI_API_KEY"
}
},
"options": {
"model": "gpt-4o"
},
"prompt": "Redact PII from user content before forwarding to the target model."
},
"ai-aws-content-moderation": {
"comprehend": {
"access_key_id": "$AWS_ACCESS_KEY_ID",
"secret_access_key": "$AWS_SECRET_ACCESS_KEY",
"region": "us-east-1"
},
"moderation_categories": {
"PROFANITY": 0.5,
"HATE_SPEECH": 0.5,
"INSULT": 0.5,
"HARASSMENT_OR_ABUSE": 0.5,
"SEXUAL": 0.5,
"VIOLENCE_OR_THREAT": 0.5
},
"moderation_threshold": 0.5
},
"ai-proxy": {
"provider": "openai",
"auth": { "header": { "Authorization": "Bearer $OPENAI_API_KEY" } },
"options": { "model": "gpt-4o" }
}
}
}
EOF
❶ ai-prompt-guard 执行第一轮提示词过滤,并以 HTTP 400 拒绝匹配拒绝模式的请求。
❷ ai-request-rewrite 在调用模型前净化提示词内容并遮蔽 PII。
❸ ai-aws-content-moderation 在 comprehend 块提供 AWS Comprehend 凭证,并在转发流量前实施有害性和滥用阈值。
services:
- name: AI Combined Guardrails
routes:
- uris:
- /ai/chat
name: ai-guardrails-combined
plugins:
ai-prompt-guard:
allow_patterns:
- (?i)^.{1,4000}$
deny_patterns:
- (?i)(ignore\s+all\s+previous\s+instructions|reveal\s+system\s+prompt|developer\s+mode)
match_all_roles: false
match_all_conversation_history: false
ai-request-rewrite:
provider: openai
auth:
header:
Authorization: "Bearer ${OPENAI_API_KEY}"
options:
model: gpt-4o
prompt: Redact PII from user content before forwarding to the target model.
ai-aws-content-moderation:
comprehend:
access_key_id: ${AWS_ACCESS_KEY_ID}
secret_access_key: ${AWS_SECRET_ACCESS_KEY}
region: us-east-1
moderation_categories:
PROFANITY: 0.5
HATE_SPEECH: 0.5
INSULT: 0.5
HARASSMENT_OR_ABUSE: 0.5
SEXUAL: 0.5
VIOLENCE_OR_THREAT: 0.5
moderation_threshold: 0.5
ai-proxy:
provider: openai
auth:
header:
Authorization: "Bearer ${OPENAI_API_KEY}"
options:
model: gpt-4o
❶ ai-prompt-guard 执行第一轮提示词过滤,并以 HTTP 400 拒绝匹配拒绝模式的请求。
❷ ai-request-rewrite 在调用模型前净化提示词内容并遮蔽 PII。
❸ ai-aws-content-moderation 在 comprehend 块提供 AWS Comprehend 凭证,并在转发流量前实施有害性和滥用阈值。
adc sync -f adc.yaml
后续步骤
- 提示词工程和模板 — 在安全检查前规范提示词。
- 基于 Token 的限流和配额管理 — 为受保护路由添加预算控制。
- AI 可观测性 和成本跟踪 — 监控审核结果、Token 用量和延迟。