Aporia
使用Aporia检测请求中的PII和响应中的不当言论
1. 在Aporia上设置防护措施
创建Aporia项目
在Aporia上创建两个项目
- 预LLM API调用 - 设置所有需要在LLM API调用前运行的策略
- 后LLM API调用 - 设置所有需要在LLM API调用后运行的策略
调用前:检测PII
将PII - Prompt添加到您的预LLM API调用项目中
调用后:检测响应中的不当言论
将Toxicity - Response添加到您的后LLM API调用项目中
2. 在LiteLLM的config.yaml中定义防护措施
- 在
guardrails部分定义您的防护措施
model_list:
- model_name: gpt-3.5-turbo
litellm_params:
model: openai/gpt-3.5-turbo
api_key: os.environ/OPENAI_API_KEY
guardrails:
- guardrail_name: "aporia-pre-guard"
litellm_params:
guardrail: aporia # 支持的值: "aporia", "lakera"
mode: "during_call"
api_key: os.environ/APORIA_API_KEY_1
api_base: os.environ/APORIA_API_BASE_1
- guardrail_name: "aporia-post-guard"
litellm_params:
guardrail: aporia # 支持的值: "aporia", "lakera"
mode: "post_call"
api_key: os.environ/APORIA_API_KEY_2
api_base: os.environ/APORIA_API_BASE_2
mode支持的值
pre_call在LLM调用之前运行,针对输入post_call在LLM调用之后运行,针对输入和输出during_call在LLM调用期间运行,针对输入 与pre_call相同,但在LLM调用期间并行运行。响应不会返回,直到防护措施检查完成
3. 启动LiteLLM网关
litellm --config config.yaml --detailed_debug
4. 测试请求
- 调用失败
- 调用成功
预期此调用会失败,因为请求中的ishaan@berri.ai是PII
curl -i http://localhost:4000/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-npnwjPQciVRok5yNZgKmFQ" \
-d '{
"model": "gpt-3.5-turbo",
"messages": [
{"role": "user", "content": "hi my email is ishaan@berri.ai"}
],
"guardrails": ["aporia-pre-guard", "aporia-post-guard"]
}'
预期失败响应
{
"error": {
"message": {
"error": "违反防护措施策略",
"aporia_ai_response": {
"action": "block",
"revised_prompt": null,
"revised_response": "Aporia检测并阻止了PII",
"explain_log": null
}
},
"type": "None",
"param": "None",
"code": "400"
}
}
curl -i http://localhost:4000/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-npnwjPQciVRok5yNZgKmFQ" \
-d '{
"model": "gpt-3.5-turbo",
"messages": [
{"role": "user", "content": "hi what is the weather"}
],
"guardrails": ["aporia-pre-guard", "aporia-post-guard"]
}'
5. ✨ 按项目(API密钥)控制防护措施
info
✨ 这是企业版功能联系我们获取免费试用
使用此功能按项目控制运行的防护措施。在本教程中,我们只想为1个项目(API密钥)运行以下防护措施:
guardrails: ["aporia-pre-guard", "aporia-post-guard"]
步骤1 创建带有防护措施设置的密钥
- /key/generate
- /key/update
curl -X POST 'http://0.0.0.0:4000/key/generate' \
-H 'Authorization: Bearer sk-1234' \
-H 'Content-Type: application/json' \
-D '{
"guardrails": ["aporia-pre-guard", "aporia-post-guard"]
}
}'
curl --location 'http://0.0.0.0:4000/key/update' \
--header 'Authorization: Bearer sk-1234' \
--header 'Content-Type: application/json' \
--data '{
"key": "sk-jNm1Zar7XfNdZXp49Z1kSQ",
"guardrails": ["aporia-pre-guard", "aporia-post-guard"]
}
}'
步骤2 使用新密钥测试
curl --location 'http://0.0.0.0:4000/chat/completions' \
--header 'Authorization: Bearer sk-jNm1Zar7XfNdZXp49Z1kSQ' \
--header 'Content-Type: application/json' \
--data '{
"model": "gpt-3.5-turbo",
"messages": [
{
"role": "user",
"content": "my email is ishaan@berri.ai"
}
]
}'