跳至主要内容

✨ 企业版功能

提示

要获取许可证,请在此处与我们联系 这里

特性

阻止网络爬虫

要阻止网络爬虫索引代理服务器端点,请在您的litellm_config.yaml文件中将block_robots设置设置为true

litellm_config.yaml
general_settings:
block_robots: true

工作原理

启用后,/robots.txt端点将返回一个200状态码,内容如下

robots.txt
User-agent: *
Disallow: /

LLM请求所需的参数

当您希望强制所有请求包含某些参数时使用。例如,您需要所有请求都包含user["metadata"]["generation_name"]参数。

步骤1 在config.yaml中定义所有要强制的参数

这意味着["user"]["metadata"]["generation_name"]是所有发送到LiteLLM的LLM请求中必需的

general_settings:
master_key: sk-1234
enforced_params:
- user
- metadata.generation_name

步骤2 验证是否有效

curl --location 'https://:4000/chat/completions' \
--header 'Authorization: Bearer sk-5fmYeaUEbAMpwBNT-QpxyA' \
--header 'Content-Type: application/json' \
--data '{
"model": "gpt-3.5-turbo",
"messages": [
{
"role": "user",
"content": "hi"
}
]
}'

预期响应

{"error":{"message":"Authentication Error, BadRequest please pass param=user in request body. This is a required param","type":"auth_error","param":"None","code":401}}% 

控制可用的公共、私有路由

请参阅控制公共和私有路由,了解有关配置公共路由、仅管理员路由、允许的路由和通配符模式的详细文档。

支出跟踪

按标签查看支出

/spend/tags 请求格式

curl -X GET "http://0.0.0.0:4000/spend/tags" \
-H "Authorization: Bearer sk-1234"

/spend/tags响应格式

[
{
"individual_request_tag": "model-anthropic-claude-v2.1",
"log_count": 6,
"total_spend": 0.000672
},
{
"individual_request_tag": "app-ishaan-local",
"log_count": 4,
"total_spend": 0.000448
},
{
"individual_request_tag": "app-ishaan-prod",
"log_count": 2,
"total_spend": 0.000224
}
]
提示

有关全面的支出跟踪功能,包括预算、警报和详细分析,请查看支出跟踪

防护栏 - 密钥检测/删除

❓ 使用此功能来删除发送到LLM的API密钥、密钥。

例如,如果您想删除以下请求中的OPENAI_API_KEY的值

传入请求

{
"messages": [
{
"role": "user",
"content": "Hey, how's it going, API_KEY = 'sk_1234567890abcdef'",
}
]
}

审核后的请求

{
"messages": [
{
"role": "user",
"content": "Hey, how's it going, API_KEY = '[REDACTED]'",
}
]
}

用法

步骤1 将此添加到您的config.yaml

litellm_settings:
callbacks: ["hide_secrets"]

步骤2 使用--detailed_debug运行litellm代理,以查看服务器日志

litellm --config config.yaml --detailed_debug

步骤3 使用请求进行测试

发送此请求

curl --location 'https://:4000/chat/completions' \
--header 'Authorization: Bearer sk-1234' \
--header 'Content-Type: application/json' \
--data '{
"model": "llama3",
"messages": [
{
"role": "user",
"content": "what is the value of my open ai key? openai_api_key=sk-1234998222"
}
]
}'

预计会在您的litellm服务器日志中看到以下警告

LiteLLM Proxy:WARNING: secret_detection.py:88 - Detected and redacted secrets in message: ['Secret Keyword']

您还可以看到从litellm发送到API提供商的原始请求

POST Request Sent from LiteLLM:
curl -X POST \
https://api.groq.com/openai/v1/ \
-H 'Authorization: Bearer gsk_mySVchjY********************************************' \
-d {
"model": "llama3-8b-8192",
"messages": [
{
"role": "user",
"content": "what is the time today, openai_api_key=[REDACTED]"
}
],
"stream": false,
"extra_body": {}
}

每个API密钥的密钥检测开启/关闭

❓ 当您需要为每个API密钥打开/关闭防护栏时使用此功能

步骤1 使用hide_secrets关闭创建密钥

👉 使用/key/generate/key/update设置"permissions": {"hide_secrets": false}

这意味着hide_secrets防护栏对于此API密钥的所有请求都已关闭

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

步骤 2 使用新的 Key 进行测试

curl --location 'http://0.0.0.0:4000/chat/completions' \
--header 'Authorization: Bearer sk-jNm1Zar7XfNdZXp49Z1kSQ' \
--header 'Content-Type: application/json' \
--data '{
"model": "llama3",
"messages": [
{
"role": "user",
"content": "does my openai key look well formatted OpenAI_API_KEY=sk-1234777"
}
]
}'

预计会在您的回调中看到sk-1234777在您的服务器日志中。

信息

由于api key=sk-jNm1Zar7XfNdZXp49Z1kSQ具有"permissions": {"hide_secrets": false},因此未运行hide_secrets防护栏检查。

内容审核

使用LLM Guard进行内容审核

在您的环境中设置LLM Guard API基本地址

LLM_GUARD_API_BASE = "http://0.0.0.0:8192" # deployed llm guard api

llmguard_moderations添加为回调

litellm_settings:
callbacks: ["llmguard_moderations"]

现在您可以轻松地进行测试

  • 发出常规/chat/completion调用

  • 检查您的代理日志中是否有包含LLM Guard:的任何语句

预期结果

LLM Guard: Received response - {"sanitized_prompt": "hello world", "is_valid": true, "scanners": { "Regex": 0.0 }}

每个密钥打开/关闭

1. 更新配置

litellm_settings:
callbacks: ["llmguard_moderations"]
llm_guard_mode: "key-specific"

2. 创建新密钥

curl --location 'https://:4000/key/generate' \
--header 'Authorization: Bearer sk-1234' \
--header 'Content-Type: application/json' \
--data '{
"models": ["fake-openai-endpoint"],
"permissions": {
"enable_llm_guard_check": true # 👈 KEY CHANGE
}
}'

# Returns {..'key': 'my-new-key'}

3. 测试它!

curl --location 'http://0.0.0.0:4000/v1/chat/completions' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer my-new-key' \ # 👈 TEST KEY
--data '{"model": "fake-openai-endpoint", "messages": [
{"role": "system", "content": "Be helpful"},
{"role": "user", "content": "What do you know?"}
]
}'

每个请求打开/关闭

1. 更新配置

litellm_settings:
callbacks: ["llmguard_moderations"]
llm_guard_mode: "request-specific"

2. 创建新密钥

curl --location 'https://:4000/key/generate' \
--header 'Authorization: Bearer sk-1234' \
--header 'Content-Type: application/json' \
--data '{
"models": ["fake-openai-endpoint"],
}'

# Returns {..'key': 'my-new-key'}

3. 测试它!

import openai
client = openai.OpenAI(
api_key="sk-1234",
base_url="http://0.0.0.0:4000"
)

# request sent to model set on litellm proxy, `litellm --model`
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages = [
{
"role": "user",
"content": "this is a test request, write a short poem"
}
],
extra_body={ # pass in any provider-specific param, if not supported by openai, https://docs.litellm.com.cn/docs/completion/input#provider-specific-params
"metadata": {
"permissions": {
"enable_llm_guard_check": True # 👈 KEY CHANGE
},
}
}
)

print(response)

使用LlamaGuard进行内容审核

当前适用于Sagemaker的LlamaGuard端点。

如何在config.yaml中启用此功能

litellm_settings:
callbacks: ["llamaguard_moderations"]
llamaguard_model_name: "sagemaker/jumpstart-dft-meta-textgeneration-llama-guard-7b"

确保您在环境中拥有相关的密钥,例如:

os.environ["AWS_ACCESS_KEY_ID"] = ""
os.environ["AWS_SECRET_ACCESS_KEY"] = ""
os.environ["AWS_REGION_NAME"] = ""

自定义LlamaGuard提示

要修改llama guard评估的不安全类别,只需创建此类别列表的您自己的版本

将您的代理指向它

callbacks: ["llamaguard_moderations"]
llamaguard_model_name: "sagemaker/jumpstart-dft-meta-textgeneration-llama-guard-7b"
llamaguard_unsafe_content_categories: /path/to/llamaguard_prompt.txt

使用Google文本审核进行内容审核

需要将您的GOOGLE_APPLICATION_CREDENTIALS设置为您的.env(与VertexAI相同)。

如何在config.yaml中启用此功能

litellm_settings:
callbacks: ["google_text_moderation"]

设置自定义置信度阈值

Google审核将测试与几个类别进行比较。来源

设置全局默认置信度阈值

默认情况下,此值设置为0.8。但您可以在config.yaml中覆盖此设置。

litellm_settings: 
google_moderation_confidence_threshold: 0.4

设置特定于类别的置信度阈值

在您的config.yaml中设置特定于类别的置信度阈值。如果没有设置,将使用全局默认值。

litellm_settings: 
toxic_confidence_threshold: 0.1

以下是特定于类别的数值

类别设置
"toxic"toxic_confidence_threshold: 0.1
"insult"insult_confidence_threshold: 0.1
"profanity"profanity_confidence_threshold: 0.1
"derogatory"derogatory_confidence_threshold: 0.1
"sexual"sexual_confidence_threshold: 0.1
"death_harm_and_tragedy"death_harm_and_tragedy_threshold: 0.1
"violent"violent_threshold: 0.1
"firearms_and_weapons"firearms_and_weapons_threshold: 0.1
"public_safety"public_safety_threshold: 0.1
"health"health_threshold: 0.1
"religion_and_belief"religion_and_belief_threshold: 0.1
"illicit_drugs"illicit_drugs_threshold: 0.1
"war_and_conflict"war_and_conflict_threshold: 0.1
"politics"politics_threshold: 0.1
"finance"finance_threshold: 0.1
"legal"legal_threshold: 0.1

Swagger文档 - 自定义路由 + 品牌

信息

需要LiteLLM企业版密钥才能使用。获取免费的2周许可证这里

在您的环境中设置LiteLLM密钥

LITELLM_LICENSE=""

自定义标题 + 描述

在您的环境中设置

DOCS_TITLE="TotalGPT"
DOCS_DESCRIPTION="Sample Company Description"

自定义路由

隐藏用户管理员路由。

在您的环境中设置

DOCS_FILTERED="True" # only shows openai routes to user

启用阻止的用户列表

如果使用此用户ID发出任何调用,它将被拒绝 - 如果您希望用户选择退出AI功能,请使用此功能

litellm_settings: 
callbacks: ["blocked_user_check"]
blocked_user_list: ["user_id_1", "user_id_2", ...] # can also be a .txt filepath e.g. `/relative/path/blocked_list.txt`

如何测试

user=<user_id>设置为可能选择退出的用户的用户ID。

import openai
client = openai.OpenAI(
api_key="sk-1234",
base_url="http://0.0.0.0:4000"
)

# request sent to model set on litellm proxy, `litellm --model`
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages = [
{
"role": "user",
"content": "this is a test request, write a short poem"
}
],
user="user_id_1"
)

print(response)

通过API使用

阻止所有客户ID的调用

curl -X POST "http://0.0.0.0:4000/customer/block" \
-H "Authorization: Bearer sk-1234" \
-D '{
"user_ids": [<user_id>, ...]
}'

取消阻止用户ID的调用

curl -X POST "http://0.0.0.0:4000/user/unblock" \
-H "Authorization: Bearer sk-1234" \
-D '{
"user_ids": [<user_id>, ...]
}'

启用禁止的关键字列表

litellm_settings: 
callbacks: ["banned_keywords"]
banned_keywords_list: ["hello"] # can also be a .txt file - e.g.: `/relative/path/keywords.txt`

测试此功能

curl --location 'http://0.0.0.0:4000/chat/completions' \
--header 'Content-Type: application/json' \
--data ' {
"model": "gpt-3.5-turbo",
"messages": [
{
"role": "user",
"content": "Hello world!"
}
]
}
'

公共AI中心

与用户分享可用模型和代理的公共页面

了解更多

[BETA] AWS 密钥管理器 - 密钥解密

这是一个测试版功能,可能会发生变化。

步骤 1. 在环境变量中添加 USE_AWS_KMS

USE_AWS_KMS="True"

步骤 2. 在环境变量中添加 LITELLM_SECRET_AWS_KMS_ 到加密的密钥

LITELLM_SECRET_AWS_KMS_DATABASE_URL="AQICAH.."

LiteLLM 会找到它并在运行时使用解密后的 DATABASE_URL="postgres://.." 值。

步骤 3. 启动代理

$ litellm

它是如何工作的?

  • 密钥解密在服务器启动之前运行。 代码
  • 它将解密后的值添加到 Python 进程的 os.environ 中。

注意: 在 Python 脚本中使用 os.environ 设置环境变量不会使该变量可通过 SSH 会话或任何独立于 Python 脚本启动的新进程访问。 以这种方式设置的环境变量仅影响当前进程及其子进程。

设置 LiteLLM 代理上的最大请求/响应大小

如果您想为您的代理服务器设置最大请求/响应大小,请使用此功能。 如果请求大小超过该大小,则会被拒绝 + 触发 Slack 警报

用法

步骤 1. 设置 max_request_size_mbmax_response_size_mb

在此示例中,我们为 max_request_size_mb 设置了一个非常低的限制,并预计它会被拒绝

信息

在生产环境中,我们建议将 max_request_size_mb / max_response_size_mb 设置为大约 32 MB

model_list:
- model_name: fake-openai-endpoint
litellm_params:
model: openai/fake
api_key: fake-key
api_base: https://exampleopenaiendpoint-production.up.railway.app/
general_settings:
master_key: sk-1234

# Security controls
max_request_size_mb: 0.000000001 # 👈 Key Change - Max Request Size in MB. Set this very low for testing
max_response_size_mb: 100 # 👈 Key Change - Max Response Size in MB

步骤 2. 使用 /chat/completions 请求进行测试

curl https://:4000/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-1234" \
-d '{
"model": "fake-openai-endpoint",
"messages": [
{"role": "user", "content": "Hello, Claude!"}
]
}'

请求的预期响应 我们预计这将会失败,因为请求大小超过了 max_request_size_mb

{"error":{"message":"Request size is too large. Request size is 0.0001125335693359375 MB. Max size is 1e-09 MB","type":"bad_request_error","param":"content-length","code":400}}