跳到主要内容

Guardrails.ai

使用 Guardrails.ai 为 LLM 输出添加检查。

前置条件

用法

  1. 设置 config.yaml
model_list:
- model_name: gpt-3.5-turbo
litellm_params:
model: gpt-3.5-turbo
api_key: os.environ/OPENAI_API_KEY

guardrails:
- guardrail_name: "guardrails_ai-guard"
litellm_params:
guardrail: guardrails_ai
guard_name: "gibberish_guard" # 👈 Guardrail AI guard name
mode: "post_call"
api_base: os.environ/GUARDRAILS_AI_API_BASE # 👈 Guardrails AI API Base. Defaults to "http://0.0.0.0:8000"
  1. 启动 LiteLLM 网关
litellm --config config.yaml --detailed_debug
  1. 测试请求

Langchain, OpenAI SDK 用法示例

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": ["guardrails_ai-guard"]
}'

✨ 按项目(API Key)控制防护栏

信息

✨ 这是仅限企业版的功能 联系我们获取免费试用

用此功能按项目控制运行哪些防护栏。在本教程中,我们只想为 1 个项目(API Key)运行以下防护栏

  • 防护栏: ["aporia-pre-guard", "aporia-post-guard"]

步骤 1 创建带有防护栏设置的 Key

curl -X POST 'http://0.0.0.0:4000/key/generate' \
-H 'Authorization: Bearer sk-1234' \
-H 'Content-Type: application/json' \
-D '{
"guardrails": ["guardrails_ai-guard"]
}
}'

步骤 2 使用新的 Key 测试

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"
}
]
}'