跳到主要内容

入门 - 端到端教程

LiteLLM 代理的端到端教程,旨在

  • 添加 Azure OpenAI 模型
  • 发起成功的 /chat/completion 调用
  • 生成虚拟密钥
  • 设置虚拟密钥的 RPM 限制

先决条件

  • 安装 LiteLLM Docker 镜像 LiteLLM CLI (pip 包)
docker pull ghcr.io/berriai/litellm:main-latest

查看所有 Docker 镜像

1. 添加模型

使用 config.yaml 文件控制 LiteLLM 代理。

在 config.yaml 中设置您的 Azure 模型。

model_list:
- model_name: gpt-3.5-turbo
litellm_params:
model: azure/my_azure_deployment
api_base: os.environ/AZURE_API_BASE
api_key: "os.environ/AZURE_API_KEY"
api_version: "2024-07-01-preview" # [OPTIONAL] litellm uses the latest azure api_version by default

模型列表规范

  • model_name (str) - 此字段应包含接收到的模型名称。
  • litellm_params (dict) 查看所有 LiteLLM 参数
    • model (str) - 指定发送到 litellm.acompletion / litellm.aembedding 等的模型名称。这是 LiteLLM 用于在后端路由到正确的模型 + 提供商逻辑的标识符。
    • api_key (str) - 身份验证所需的 API 密钥。可以通过使用 os.environ/ 从环境变量中获取。
    • api_base (str) - 您的 Azure 部署的 API 基础 URL。
    • api_version (str) - 调用 Azure OpenAI API 时使用的 API 版本。在此处获取最新的推理 API 版本。

2. 发起成功的 /chat/completion 调用

LiteLLM 代理与 OpenAI 100% 兼容。通过 /chat/completions 路由测试您的 Azure 模型。

2.1 启动代理

将步骤 1 中的 config.yaml 保存为 litellm_config.yaml

docker run \
-v $(pwd)/litellm_config.yaml:/app/config.yaml \
-e AZURE_API_KEY=d6*********** \
-e AZURE_API_BASE=https://openai-***********/ \
-p 4000:4000 \
ghcr.io/berriai/litellm:main-latest \
--config /app/config.yaml --detailed_debug

# RUNNING on http://0.0.0.0:4000

确认您的 config.yaml 已正确挂载

Loaded config YAML (api_key and environment_variables are not shown):
{
"model_list": [
{
"model_name ...

2.2 发起调用

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": "system",
"content": "You are a helpful math tutor. Guide the user through the solution step by step."
},
{
"role": "user",
"content": "how can I solve 8x + 7 = -23"
}
]
}'

预期响应

{
"id": "chatcmpl-2076f062-3095-4052-a520-7c321c115c68",
"choices": [
{
"finish_reason": "stop",
"index": 0,
"message": {
"content": "I am gpt-3.5-turbo",
"role": "assistant",
"tool_calls": null,
"function_call": null
}
}
],
"created": 1724962831,
"model": "gpt-3.5-turbo",
"object": "chat.completion",
"system_fingerprint": null,
"usage": {
"completion_tokens": 20,
"prompt_tokens": 10,
"total_tokens": 30
}
}

3. 生成虚拟密钥

通过代理的虚拟密钥跟踪消费并控制模型访问

3.1 设置数据库

要求

  • 需要一个 Postgres 数据库(例如 Supabase, Neon 等)
model_list:
- model_name: gpt-3.5-turbo
litellm_params:
model: azure/my_azure_deployment
api_base: os.environ/AZURE_API_BASE
api_key: "os.environ/AZURE_API_KEY"
api_version: "2024-07-01-preview" # [OPTIONAL] litellm uses the latest azure api_version by default

general_settings:
master_key: sk-1234
database_url: "postgresql://<user>:<password>@<host>:<port>/<dbname>" # 👈 KEY CHANGE

将 config.yaml 保存为 litellm_config.yaml(在 3.2 中使用)。


什么是 general_settings

这些是 LiteLLM 代理服务器的设置。

在此处查看所有通用设置。

  1. master_key (str)

    • 描述:
      • 设置一个 master key,这是您的代理管理员密钥 - 您可以使用它来创建其他密钥(🚨 必须以 sk- 开头)。
    • 用法:
      • 在 config.yaml 中设置general_settings:master_key 下设置您的主密钥,例如 - master_key: sk-1234
      • 设置环境变量 设置 LITELLM_MASTER_KEY
  2. database_url (str)

    • 描述:
      • 设置 database_url,这是连接到您的 Postgres 数据库的字符串,litellm 使用它来生成密钥、用户和团队。
    • 用法:
      • 在 config.yaml 中设置general_settings:database_url 下设置您的数据库 URL,例如 - database_url: "postgresql://..."
      • 在您的环境变量中设置 DATABASE_URL=postgresql://<user>:<password>@<host>:<port>/<dbname>

3.2 启动代理

docker run \
-v $(pwd)/litellm_config.yaml:/app/config.yaml \
-e AZURE_API_KEY=d6*********** \
-e AZURE_API_BASE=https://openai-***********/ \
-p 4000:4000 \
ghcr.io/berriai/litellm:main-latest \
--config /app/config.yaml --detailed_debug

3.3 创建带有 RPM 限制的密钥

创建一个 rpm_limit: 1 的密钥。这将仅允许使用此密钥对代理的调用每分钟进行 1 次请求。

curl -L -X POST 'http://0.0.0.0:4000/key/generate' \
-H 'Authorization: Bearer sk-1234' \
-H 'Content-Type: application/json' \
-d '{
"rpm_limit": 1
}'

查看完整的 API 规范

预期响应

{
"key": "sk-12..."
}

3.4 测试!

使用步骤 3.3 中的虚拟密钥

第 1 次调用 - 预计会成功!

curl -X POST 'http://0.0.0.0:4000/chat/completions' \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer sk-12...' \
-d '{
"model": "gpt-3.5-turbo",
"messages": [
{
"role": "system",
"content": "You are a helpful math tutor. Guide the user through the solution step by step."
},
{
"role": "user",
"content": "how can I solve 8x + 7 = -23"
}
]
}'

预期响应

{
"id": "chatcmpl-2076f062-3095-4052-a520-7c321c115c68",
"choices": [
...
}

第 2 次调用 - 预计会失败!

为什么这次调用失败了?

我们将虚拟密钥的每分钟请求数 (RPM) 限制设置为 1。此限制已被超出。

curl -X POST 'http://0.0.0.0:4000/chat/completions' \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer sk-12...' \
-d '{
"model": "gpt-3.5-turbo",
"messages": [
{
"role": "system",
"content": "You are a helpful math tutor. Guide the user through the solution step by step."
},
{
"role": "user",
"content": "how can I solve 8x + 7 = -23"
}
]
}'

预期响应

{
"error": {
"message": "Max parallel request limit reached. Hit limit for api_key: daa1b272072a4c6841470a488c5dad0f298ff506e1cc935f4a181eed90c182ad. tpm_limit: 100, current_tpm: 29, rpm_limit: 1, current_rpm: 2.",
"type": "None",
"param": "None",
"code": "429"
}
}

故障排除

非 root Docker 镜像?

如果您需要以非 root 用户运行 Docker 镜像,请使用此链接

SSL 验证问题 / 连接错误。

如果您看到

ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self-signed certificate in certificate chain (_ssl.c:1006)

Connection Error.

您可以通过以下方式禁用 SSL 验证

model_list:
- model_name: gpt-3.5-turbo
litellm_params:
model: azure/my_azure_deployment
api_base: os.environ/AZURE_API_BASE
api_key: "os.environ/AZURE_API_KEY"
api_version: "2024-07-01-preview"

litellm_settings:
ssl_verify: false # 👈 KEY CHANGE

(数据库) 所有连接尝试失败

如果您看到

httpx.ConnectError: All connection attempts failed                                                                        

ERROR: Application startup failed. Exiting.
3:21:43 - LiteLLM Proxy:ERROR: utils.py:2207 - Error getting LiteLLM_SpendLogs row count: All connection attempts failed

这可能是数据库权限问题。

  1. 验证数据库用户权限问题

尝试创建一个新的数据库。

STATEMENT: CREATE DATABASE "litellm"

如果您收到

ERROR: permission denied to create 

这表明您有权限问题。

  1. 授予权限给您的数据库用户

它应该看起来像这样

psql -U postgres
CREATE DATABASE litellm;

在 CloudSQL 上,这是

GRANT ALL PRIVILEGES ON DATABASE litellm TO your_username;

什么是 litellm_settings

LiteLLM 代理使用 LiteLLM Python SDK 处理 LLM API 调用。

litellm_settings 是 LiteLLM Python SDK 的模块级参数(相当于在 SDK 上执行 litellm.<some_param>)。您可以在此处查看所有参数。

支持与创始人交流

Chat on WhatsApp Chat on Discord