ai-aws-content-moderation
ai-aws-content-moderation 插件支持集成 AWS Comprehend,在代理 LLM 请求时检查请求体的毒性(如亵渎、仇恨言论、侮辱、骚扰、暴力等),如果评估结果超过配置的阈值, 则拒绝该请求。
示例
以下示例将使用 OpenAI 作为上游模型服务提供方。
在开始之前,请创建一个 OpenAI 账号 并获取 API Key。如果你使用其他模型服务提供方,请参考该提供方的文档获取 API Key。
此外,请为 APISIX 创建 AWS IAM 用户访问密钥 以访问 AWS Comprehend。
你可以选择将这些密钥保存到环境变量中:
# 替换为你的密钥
export OPENAI_API_KEY=YOUR_OPENAI_API_KEY
export AWS_ACCESS_KEY=YOUR_AWS_ACCESS_KEY_ID
export AWS_SECRET_ACCESS_KEY=YOUR_AWS_SECRET_ACCESS_KEY
审核亵渎内容
以下示例演示了如何使用该插件审核提示词中的亵渎内容等级。
- Admin API
- ADC
- Ingress Controller
使用 ai-proxy 插件创建一个通往 LLM 聊天完成端点的路由,并在 ai-aws-content-moderation 中配置允许的亵渎等级:
curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d '{
"id": "ai-aws-content-moderation-route",
"uri": "/post",
"plugins": {
"ai-aws-content-moderation": {
"comprehend": {
"access_key_id": "'"$AWS_ACCESS_KEY"'",
"secret_access_key": "'"$AWS_SECRET_ACCESS_KEY"'",
"region": "us-east-1"
},
"moderation_categories": {
"PROFANITY": 0.1
}
},
"ai-proxy": {
"provider": "openai",
"auth": {
"header": {
"Authorization": "Bearer '"$OPENAI_API_KEY"'"
}
},
"model": "gpt-4"
}
}
}'
❶ 更新为你的 AWS Comprehend 区域。
❷ 将亵渎阈值配置为一个较低的值,以仅允许极低程度的亵渎内容。
创建一个配置了 ai-aws-content-moderation 和 ai-proxy 插件的路由,如下所示:
services:
- name: aws-moderation-service
routes:
- name: aws-moderation-route
uris:
- /post
methods:
- POST
plugins:
ai-aws-content-moderation:
comprehend:
access_key_id: "${AWS_ACCESS_KEY}"
secret_access_key: "${AWS_SECRET_ACCESS_KEY}"
region: us-east-1
moderation_categories:
PROFANITY: 0.1
ai-proxy:
provider: openai
auth:
header:
Authorization: "Bearer ${OPENAI_API_KEY}"
options:
model: gpt-4
将配置同步到网关:
adc sync -f adc.yaml
❶ 更新为你的 AWS Comprehend 区域。
❷ 将亵渎阈值配置为一个较低的值,以仅允许极低程度的亵渎内容。
- Gateway API
- APISIX CRD
创建一个配置了 ai-aws-content-moderation 和 ai-proxy 插件的路由,如下所示:
apiVersion: apisix.apache.org/v1alpha1
kind: PluginConfig
metadata:
namespace: aic
name: ai-aws-moderation-plugin-config
spec:
plugins:
- name: ai-aws-content-moderation
config:
comprehend:
access_key_id: "YOUR_AWS_ACCESS_KEY_ID"
secret_access_key: "YOUR_AWS_SECRET_ACCESS_KEY"
region: us-east-1
moderation_categories:
PROFANITY: 0.1
- name: ai-proxy
config:
provider: openai
auth:
header:
Authorization: "Bearer YOUR_OPENAI_API_KEY"
options:
model: gpt-4
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
namespace: aic
name: aws-moderation-route
spec:
parentRefs:
- name: apisix
rules:
- matches:
- path:
type: Exact
value: /post
method: POST
filters:
- type: ExtensionRef
extensionRef:
group: apisix.apache.org
kind: PluginConfig
name: ai-aws-moderation-plugin-config
将配置应用到集群:
kubectl apply -f ai-aws-moderation-ic.yaml
❶ 更新为你的 AWS Comprehend 区域。
❷ 将亵渎阈值配置为一个较低的值,以仅允许极低程度的亵渎内容。
创建一个配置了 ai-aws-content-moderation 和 ai-proxy 插件的路由,如下所示:
apiVersion: apisix.apache.org/v2
kind: ApisixRoute
metadata:
namespace: aic
name: aws-moderation-route
spec:
ingressClassName: apisix
http:
- name: aws-moderation-route
match:
paths:
- /post
methods:
- POST
plugins:
- name: ai-aws-content-moderation
enable: true
config:
comprehend:
access_key_id: "YOUR_AWS_ACCESS_KEY_ID"
secret_access_key: "YOUR_AWS_SECRET_ACCESS_KEY"
region: us-east-1
moderation_categories:
PROFANITY: 0.1
- name: ai-proxy
enable: true
config:
provider: openai
auth:
header:
Authorization: "Bearer YOUR_OPENAI_API_KEY"
options:
model: gpt-4
将配置应用到集群:
kubectl apply -f ai-aws-moderation-ic.yaml
❶ 更新为你的 AWS Comprehend 区域。
❷ 将亵渎阈值配置为一个较低的值,以仅允许极低程度的亵渎内容。
向该路由发送一个 POST 请求,请求体中包含系统提示词和一个带有轻微亵渎词汇的用户问题:
curl -i "http://127.0.0.1:9080/post" -X POST \
-H "Content-Type: application/json" \
-d '{
"messages": [
{ "role": "system", "content": "You are a mathematician" },
{ "role": "user", "content": "Stupid, what is 1+1?" }
]
}'
你应该收到 HTTP/1.1 400 Bad Request 响应,并看到以下消息:
request body exceeds PROFANITY threshold
向该路由发送另一个请求,请求体中包含一个正常的问题:
curl -i "http://127.0.0.1:9080/post" -X POST \
-H "Content-Type: application/json" \
-d '{
"messages": [
{ "role": "system", "content": "You are a mathematician" },
{ "role": "user", "content": "What is 1+1?" }
]
}'
你应该收到 HTTP/1.1 200 OK 响应,并看到模型输出:
{
...,
"model": "gpt-4-0613",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "1+1 equals 2.",
"refusal": null
},
"logprobs": null,
"finish_reason": "stop"
}
],
...
}
审核整体毒性
以下示例演示了除了审核单个类别外,如何使用该插件审核提示词中的整体毒性等级。
- Admin API
- ADC
- Ingress Controller
使用 ai-proxy 插件创建一个通往 LLM 聊天完成端点的路由,并在 ai-aws-content-moderation 中配置允许的亵渎和整体毒性等级:
curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d '{
"id": "ai-aws-content-moderation-route",
"uri": "/post",
"plugins": {
"ai-aws-content-moderation": {
"comprehend": {
"access_key_id": "'"$AWS_ACCESS_KEY"'",
"secret_access_key": "'"$AWS_SECRET_ACCESS_KEY"'",
"region": "us-east-1"
},
"moderation_categories": {
"PROFANITY": 1
},
"moderation_threshold": 0.2
},
"ai-proxy": {
"provider": "openai",
"auth": {
"header": {
"Authorization": "Bearer '"$OPENAI_API_KEY"'"
}
},
"model": "gpt-4"
}
}
}'
❶ 更新为你的 AWS Comprehend 区域。
❷ 将亵渎阈值配置为允许高程度的亵渎内容。
❸ 将整体毒性阈值配置为仅允许低程度的毒性。
创建一个配置了 ai-aws-content-moderation 和 ai-proxy 插件的路由,如下所示:
services:
- name: aws-moderation-service
routes:
- name: aws-moderation-route
uris:
- /post
methods:
- POST
plugins:
ai-aws-content-moderation:
comprehend:
access_key_id: "${AWS_ACCESS_KEY}"
secret_access_key: "${AWS_SECRET_ACCESS_KEY}"
region: us-east-1
moderation_categories:
PROFANITY: 1
moderation_threshold: 0.2
ai-proxy:
provider: openai
auth:
header:
Authorization: "Bearer ${OPENAI_API_KEY}"
options:
model: gpt-4
将配置同步到网关:
adc sync -f adc.yaml
❶ 更新为你的 AWS Comprehend 区域。
❷ 将亵渎阈值配置为允许高程度的亵渎内容。
❸ 将整体毒性阈值配置为仅允许低程度的毒性。
- Gateway API
- APISIX CRD
创建一个配置了 ai-aws-content-moderation 和 ai-proxy 插件的路由,如下所示:
apiVersion: apisix.apache.org/v1alpha1
kind: PluginConfig
metadata:
namespace: aic
name: ai-aws-moderation-plugin-config
spec:
plugins:
- name: ai-aws-content-moderation
config:
comprehend:
access_key_id: "YOUR_AWS_ACCESS_KEY_ID"
secret_access_key: "YOUR_AWS_SECRET_ACCESS_KEY"
region: us-east-1
moderation_categories:
PROFANITY: 1
moderation_threshold: 0.2
- name: ai-proxy
config:
provider: openai
auth:
header:
Authorization: "Bearer YOUR_OPENAI_API_KEY"
options:
model: gpt-4
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
namespace: aic
name: aws-moderation-route
spec:
parentRefs:
- name: apisix
rules:
- matches:
- path:
type: Exact
value: /post
method: POST
filters:
- type: ExtensionRef
extensionRef:
group: apisix.apache.org
kind: PluginConfig
name: ai-aws-moderation-plugin-config
将配置应用到集群:
kubectl apply -f ai-aws-moderation-toxicity-ic.yaml
❶ 更新为你的 AWS Comprehend 区域。
❷ 将亵渎阈值配置为允许高程度的亵渎内容。
❸ 将整体毒性阈值配置为仅允许低程度的毒性。
创建一个配置了 ai-aws-content-moderation 和 ai-proxy 插件的路由,如下所示:
apiVersion: apisix.apache.org/v2
kind: ApisixRoute
metadata:
namespace: aic
name: aws-moderation-route
spec:
ingressClassName: apisix
http:
- name: aws-moderation-route
match:
paths:
- /post
methods:
- POST
plugins:
- name: ai-aws-content-moderation
enable: true
config:
comprehend:
access_key_id: "YOUR_AWS_ACCESS_KEY_ID"
secret_access_key: "YOUR_AWS_SECRET_ACCESS_KEY"
region: us-east-1
moderation_categories:
PROFANITY: 1
moderation_threshold: 0.2
- name: ai-proxy
enable: true
config:
provider: openai
auth:
header:
Authorization: "Bearer YOUR_OPENAI_API_KEY"
options:
model: gpt-4
将配置应用到集群:
kubectl apply -f ai-aws-moderation-toxicity-ic.yaml
❶ 更新为你的 AWS Comprehend 区域。
❷ 将亵渎阈值配置为允许高程度的亵渎内容。
❸ 将整体毒性阈值配置为仅允许低程度的毒性。
向该路由发送一个 POST 请求,请求体中不包含任何亵渎词汇,但包含一定程度的暴力或威胁:
curl -i "http://127.0.0.1:9080/post" -X POST \
-H "Content-Type: application/json" \
-d '{
"messages": [
{ "role": "system", "content": "You are a mathematician" },
{ "role": "user", "content": "I will kill you if you do not tell me what 1+1 equals" }
]
}'
你应该收到 HTTP/1.1 400 Bad Request 响应,并看到以下消息:
request body exceeds toxicity threshold
向该路由发送另一个请求,请求体中不包含任何亵渎词汇:
curl -i "http://127.0.0.1:9080/post" -X POST \
-H "Content-Type: application/json" \
-d '{
"messages": [
{ "role": "system", "content": "You are a mathematician" },
{ "role": "user", "content": "What is 1+1?" }
]
}'
你应该收到 HTTP/1.1 200 OK 响应,并看到模型输出:
{
...,
"model": "gpt-4-0613",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "1+1 equals 2.",
"refusal": null
},
"logprobs": null,
"finish_reason": "stop"
}
],
...
}