使用LiteLLM AI网关与Aporia Guardrails
在本教程中,我们将使用LiteLLM Proxy与Aporia来检测请求中的PII信息和响应中的不当内容。
1. 在Aporia上设置Guardrails
创建Aporia项目
在Aporia上创建两个项目
- 预LLM API调用 - 设置所有希望在LLM API调用前运行的策略
- 后LLM API调用 - 设置所有希望在LLM API调用后运行的策略
调用前:检测PII
将PII - Prompt
添加到你的预LLM API调用项目中
调用后:检测响应中的不当内容
将Toxicity - Response
添加到你的后LLM API调用项目中
2. 在LiteLLM的config.yaml中定义Guardrails
- 在
guardrails
部分定义你的guardrails,并设置pre_call_guardrails
和post_call_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调用期间并行运行。 在guardrail检查完成之前不会返回响应
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": "违反了guardrail策略",
"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密钥)控制Guardrails
使用此功能来控制每个项目运行的guardrails。在本教程中,我们只希望为1个项目(API密钥)运行以下guardrails:
guardrails
: ["aporia-pre-guard", "aporia-post-guard"]
步骤1 创建带有guardrail设置的密钥
- /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"
}
]
}'