入门指南 - 端到端教程
LiteLLM Proxy 的端到端教程,内容包括:
- 添加一个 Azure OpenAI 模型
- 成功调用 /chat/completion
- 生成一个虚拟密钥
- 设置虚拟密钥的 RPM 限制
前提条件
- 安装 LiteLLM Docker 镜像
docker pull ghcr.io/berriai/litellm:main-latest
1. 添加模型
通过 config.yaml 文件控制 LiteLLM Proxy。
使用你的 Azure 模型设置你的 config.yaml。
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 默认使用最新的 Azure api_version
模型列表规范
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 基础。api_version
(str
) - 调用 Azure 的 OpenAI API 时要使用的 API 版本。获取最新的推理 API 版本 在这里。
有用的链接
2. 成功调用 /chat/completion
LiteLLM Proxy 100% 兼容 OpenAI。通过 /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
# 正在运行于 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
}
}
有用的链接
- 所有支持的 LLM API 提供者(OpenAI/Bedrock/Vertex/等)
- 通过 OpenAI SDK、Langchain 等调用 LiteLLM Proxy
- 所有 API 端点的 Swagger
- 其他/非聊天完成端点
- 传递 VertexAI、Bedrock 等的参数
3. 生成一个虚拟密钥
通过虚拟密钥跟踪支出并控制代理的模型访问权限
3.1 设置数据库
要求
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 默认使用最新的 Azure api_version
general_settings:
master_key: sk-1234
database_url: "postgresql://<user>:<password>@<host>:<port>/<dbname>" # 👈 关键更改
将 config.yaml 保存为 litellm_config.yaml
(用于 3.2)。
什么是 general_settings
?
这些是 LiteLLM 代理服务器的设置。
在此处查看所有通用设置 here。
master_key
(str
)- 描述:
- 设置一个
master key
,这是你的代理管理员密钥 - 你可以使用它来创建其他密钥(🚨 必须以sk-
开头)。
- 设置一个
- 用法:
- 在 config.yaml 中设置 在
general_settings:master_key
下设置你的主密钥,例如 -master_key: sk-1234
- 设置环境变量 设置
LITELLM_MASTER_KEY
- 在 config.yaml 中设置 在
- 描述:
database_url
(str)- 描述:
- 设置一个
database_url
,这是连接到你的 Postgres 数据库的 URL,LiteLLM 使用它来生成密钥、用户、团队。
- 设置一个
- 用法:
- 在 config.yaml 中设置 在
general_settings:database_url
下设置你的主密钥,例如 -database_url: "postgresql://..."
- 在环境中设置
DATABASE_URL=postgresql://<user>:<password>@<host>:<port>/<dbname>
- 在 config.yaml 中设置 在
- 描述:
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
的密钥。这将只允许每分钟对该代理进行一次调用。
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
}'
预期响应
{
"key": "sk-12..."
}
3.4 测试一下!
使用步骤 3.3 中的虚拟密钥
第一次调用 - 预计会成功!
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": [
...
}
第二次调用 - 预计会失败!
为什么这次调用失败了?
我们将虚拟密钥的每分钟请求数(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 镜像,请使用 this。
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 # 👈 关键更改
什么是 litellm_settings
?
LiteLLM 代理使用 LiteLLM Python SDK 来处理 LLM API 调用。
litellm_settings
是 LiteLLM Python SDK 的模块级参数(相当于在 SDK 上执行 litellm.<some_param>
)。你可以在此处查看所有参数 here
支持并与创始人交流
我们的邮箱 ✉️ ishaan@berri.ai / krrish@berri.ai