跳到主要内容

Aim Security

快速入门

1. 创建新的 Aim Guard

前往 Aim 应用 创建新的 Guard。

出现提示时,选择 API 选项,并为您的 Guard 命名。

注意

如果您想在本地部署您的 Guard,您可以在创建 Guard 之前通过安装 Aim Outpost 来启用此选项。

2. 配置您的 Aim Guard 策略

在新创建的 Guard 页面中,您可以找到此 Guard 的提示策略中心的引用。

您可以决定启用哪些检测项,并为每个检测项设置阈值。

信息

当将 LiteLLM 与虚拟密钥结合使用时,可以通过在创建 Guard 时指定虚拟密钥别名,直接在 Aim 的 Guard 页面中设置特定于密钥的策略。

只有您的虚拟密钥别名(而非实际密钥密文)会发送给 Aim。

3. 在您的 LiteLLM config.yaml 中添加 Aim Guardrail

guardrails 部分下定义您的 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: aim-protected-app
litellm_params:
guardrail: aim
mode: [pre_call, post_call] # "During_call" is also available
api_key: os.environ/AIM_API_KEY
api_base: os.environ/AIM_API_BASE # Optional, use only when using a self-hosted Aim Outpost

api_key 下,插入您获得的 API 密钥。该密钥可以在 Guard 页面中找到。您也可以将 AIM_API_KEY 设置为环境变量。

默认情况下,api_base 设置为 https://api.aim.security。如果您使用的是自托管的 Aim Outpost,您可以将 api_base 设置为您 Outpost 的 URL。

4. 启动 LiteLLM 网关

litellm --config config.yaml

5. 发送您的第一个请求

注意

以下示例取决于您在 Guard 中启用了 PII 检测。您可以调整请求内容以匹配不同 Guard 的策略。

注意

当将 LiteLLM 与虚拟密钥结合使用时,需要在 Authorization 头部中包含虚拟密钥。

curl -i http://localhost:4000/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-3.5-turbo",
"messages": [
{"role": "user", "content": "hi my email is ishaan@berri.ai"}
],
"guardrails": ["aim-protected-app"]
}'

如果配置正确,由于 ishaan@berri.ai 会被 Aim Guard 检测为 PII,您将收到一个类似以下的响应,状态码为 400 Bad Request

{
"error": {
"message": "\"ishaan@berri.ai\" detected as email",
"type": "None",
"param": "None",
"code": "400"
}
}

高级

Aim Guard 提供特定于用户的 Guardrail 策略,使您能够将量身定制的策略应用于单个用户。要使用此功能,请通过设置请求的 x-aim-user-email 头部,在请求负载中包含最终用户的电子邮件。

curl -i http://localhost:4000/v1/chat/completions \
-H "Content-Type: application/json" \
-H "x-aim-user-email: ishaan@berri.ai" \
-d '{
"model": "gpt-3.5-turbo",
"messages": [
{"role": "user", "content": "hi what is the weather"}
],
"guardrails": ["aim-protected-app"]
}'