Skip to main content

PII 掩码 - LiteLLM 网关(已弃用版本)

danger

此版本已弃用,请使用我们新的 Presidio PII 掩码集成

LiteLLM 支持使用 Microsoft Presidio 进行 PII 掩码处理。

快速开始

步骤 1. 添加环境变量

export PRESIDIO_ANALYZER_API_BASE="http://localhost:5002"
export PRESIDIO_ANONYMIZER_API_BASE="http://localhost:5001"

步骤 2. 在 config.yaml 中设置为回调

litellm_settings: 
callbacks = ["presidio", ...] # 例如 ["presidio", custom_callbacks.proxy_handler_instance]

步骤 3. 启动代理

litellm --config /path/to/config.yaml

这将掩码化输入到 llm 提供者的数据

输出解析

LLM 响应有时可能包含被掩码的令牌。

对于 Presidio 的 'replace' 操作,LiteLLM 可以检查 LLM 响应并将掩码令牌替换为用户提交的值。

只需设置 litellm.output_parse_pii = True,即可启用此功能。

litellm_settings:
output_parse_pii: true

预期流程:

  1. 用户输入:"hello world, my name is Jane Doe. My number is: 034453334"

  2. LLM 输入:"hello world, my name is [PERSON]. My number is: [PHONE_NUMBER]"

  3. LLM 响应:"Hey [PERSON], nice to meet you!"

  4. 用户响应:"Hey Jane Doe, nice to meet you!"

即时识别器

通过将 JSON 文件传递给代理,向 Presidio /analyze 发送即时识别器

示例 即时识别器

litellm_settings: 
callbacks: ["presidio"]
presidio_ad_hoc_recognizers: "./hooks/example_presidio_ad_hoc_recognizer.json"

当你运行代理时,可以看到这一点:

litellm --config /path/to/config.yaml --debug

发起一个聊天完成请求,示例:

{
"model": "azure-gpt-3.5",
"messages": [{"role": "user", "content": "John Smith AHV number is 756.3026.0705.92. Zip code: 1334023"}]
}

并在日志中搜索以 Presidio PII Masking 开头的任何日志,示例:

Presidio PII Masking: Redacted pii message: <PERSON> AHV number is <AHV_NUMBER>. Zip code: <US_DRIVER_LICENSE>

按密钥开关

关闭给定密钥的 PII 掩码。

通过在生成密钥时设置 permissions: {"pii": false} 来实现。

curl --location 'http://0.0.0.0:4000/key/generate' \
--header 'Authorization: Bearer sk-1234' \
--header 'Content-Type: application/json' \
--data '{
"permissions": {"pii": false}
}'

按请求开关

代理支持两种请求级别的 PII 控制:

  • no-pii: 可选(bool) - 允许用户按请求关闭 PII 掩码。
  • output_parse_pii: 可选(bool) - 允许用户按请求关闭 PII 输出解析。

使用方法

步骤 1. 创建具有 PII 权限的密钥

为给定密钥设置 allow_pii_controls 为 true。这将允许用户设置请求级别的 PII 控制。

curl --location 'http://0.0.0.0:4000/key/generate' \
--header 'Authorization: Bearer my-master-key' \
--header 'Content-Type: application/json' \
--data '{
"permissions": {"allow_pii_controls": true}
}'

步骤 2. 关闭 PII 输出解析

import os
from openai import OpenAI

client = OpenAI(
# 这是默认设置,可以省略
api_key=os.environ.get("OPENAI_API_KEY"),
base_url="http://0.0.0.0:4000"
)

chat_completion = client.chat.completions.create(
messages=[
{
"role": "user",
"content": "My name is Jane Doe, my number is 8382043839",
}
],
model="gpt-3.5-turbo",
extra_body={
"content_safety": {"output_parse_pii": False}
}
)

步骤 3: 查看响应

{
"id": "chatcmpl-8c5qbGTILZa1S4CK3b31yj5N40hFN",
"choices": [
{
"finish_reason": "stop",
"index": 0,
"message": {
"content": "Hi [PERSON], what can I help you with?",
"role": "assistant"
}
}
],
"created": 1704089632,
"model": "gpt-35-turbo",
"object": "chat.completion",
"system_fingerprint": null,
"usage": {
"completion_tokens": 47,
"prompt_tokens": 12,
"total_tokens": 59
},
"_response_ms": 1753.426
}

仅在日志记录时启用

仅在记录到 Langfuse 等时应用 PII 掩码。

不在实际的 llm api 请求/响应上应用。

note

目前仅适用于

  • /chat/completion 请求
  • 在 'success' 日志记录上
  1. 设置 config.yaml
litellm_settings:
presidio_logging_only: true

model_list:
- model_name: gpt-3.5-turbo
litellm_params:
model: gpt-3.5-turbo
api_key: os.environ/OPENAI_API_KEY
  1. 启动代理
litellm --config /path/to/config.yaml
  1. 测试它!
curl -X POST 'http://0.0.0.0:4000/chat/completions' \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer sk-1234' \
-D '{
"model": "gpt-3.5-turbo",
"messages": [
{
"role": "user",
"content": "嗨,我的名字是简!"
}
]
}'

预期记录的回复

嗨,我的名字是<PERSON>!
优云智算