跳至主要内容

[BETA]LiteLLM 管理的文件与批量处理

信息

这是 LiteLLM 企业版的一项免费功能。

通过 litellm[proxy] 包或任何 litellm docker 镜像可用。

功能描述备注
代理
SDK需要 postgres 数据库存储文件 ID
在所有批量处理提供商中可用

概述

用途

  • 在多个 Azure Batch 部署之间进行负载均衡
  • 按密钥/用户/团队控制批量处理模型的访问(与聊天完成模型相同)

(代理管理员) 用法

以下是如何授予开发者对您的批量处理模型的访问权限。

1. 设置 config.yaml

  • 为每个模型指定 mode: batch:允许开发者了解这是一个批量处理模型。
litellm_config.yaml
model_list:
- model_name: "gpt-4o-batch"
litellm_params:
model: azure/gpt-4o-mini-general-deployment
api_base: os.environ/AZURE_API_BASE
api_key: os.environ/AZURE_API_KEY
model_info:
mode: batch # 👈 SPECIFY MODE AS BATCH, to tell user this is a batch model
- model_name: "gpt-4o-batch"
litellm_params:
model: azure/gpt-4o-mini-special-deployment
api_base: os.environ/AZURE_API_BASE_2
api_key: os.environ/AZURE_API_KEY_2
model_info:
mode: batch # 👈 SPECIFY MODE AS BATCH, to tell user this is a batch model

2. 创建虚拟密钥

create_virtual_key.sh
curl -L -X POST 'https://{PROXY_BASE_URL}/key/generate' \
-H 'Authorization: Bearer ${PROXY_API_KEY}' \
-H 'Content-Type: application/json' \
-d '{"models": ["gpt-4o-batch"]}'

您现在可以使用虚拟密钥访问批量处理模型(参见开发者流程)。

(开发者) 用法

以下是如何创建 LiteLLM 管理的文件,并使用该文件执行批量处理的 CRUD 操作。

1. 创建 request.jsonl

  • 通过 /model_group/info 查看可用模型
  • 查看所有 mode: batch 的模型
  • 将 .jsonl 中的 model 设置为 /model_group/info 返回的模型
request.jsonl
{"custom_id": "request-1", "method": "POST", "url": "/v1/chat/completions", "body": {"model": "gpt-4o-batch", "messages": [{"role": "system", "content": "You are a helpful assistant."},{"role": "user", "content": "Hello world!"}],"max_tokens": 1000}}
{"custom_id": "request-2", "method": "POST", "url": "/v1/chat/completions", "body": {"model": "gpt-4o-batch", "messages": [{"role": "system", "content": "You are an unhelpful assistant."},{"role": "user", "content": "Hello world!"}],"max_tokens": 1000}}

预期

  • LiteLLM 会将其转换为 Azure 部署特有的值(例如 gpt-4o-mini-general-deployment

2. 上传文件

指定 target_model_names: "<model-name>" 以启用 LiteLLM 管理的文件和请求验证。

model-name 应与 request.jsonl 中的 model-name 相同

create_batch.py
from openai import OpenAI

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

# Upload file
batch_input_file = client.files.create(
file=open("./request.jsonl", "rb"), # {"model": "gpt-4o-batch"} <-> {"model": "gpt-4o-mini-special-deployment"}
purpose="batch",
extra_body={"target_model_names": "gpt-4o-batch"}
)
print(batch_input_file)

文件写入到哪里?:

所有 gpt-4o-batch 部署(gpt-4o-mini-general-deployment, gpt-4o-mini-special-deployment)都将被写入。这使得在步骤 3 中可以在所有 gpt-4o-batch 部署之间进行负载均衡。

3. 创建 + 检索批量处理

create_batch.py
...
# Create batch
batch = client.batches.create(
input_file_id=batch_input_file.id,
endpoint="/v1/chat/completions",
completion_window="24h",
metadata={"description": "Test batch job"},
)
print(batch)

# Retrieve batch

batch_response = client.batches.retrieve(
batch_id
)
status = batch_response.status

4. 检索批量处理内容

create_batch.py
...

file_id = batch_response.output_file_id

file_response = client.files.content(file_id)
print(file_response.text)

5. 列出批量处理

create_batch.py
...

client.batches.list(limit=10, extra_body={"target_model_names": "gpt-4o-batch"})

[即将推出]取消批量处理

create_batch.py
...

client.batches.cancel(batch_id)

端到端示例

create_batch.py
import json
from pathlib import Path
from openai import OpenAI

"""
litellm yaml:

model_list:
- model_name: gpt-4o-batch
litellm_params:
model: azure/gpt-4o-my-special-deployment
api_key: ..
api_base: ..

---
request.jsonl:
{
{
...,
"body":{"model": "gpt-4o-batch", ...}}
}
}
"""

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

# Upload file
batch_input_file = client.files.create(
file=open("./request.jsonl", "rb"),
purpose="batch",
extra_body={"target_model_names": "gpt-4o-batch"}
)
print(batch_input_file)


# Create batch
batch = client.batches.create( # UPDATE BATCH ID TO FILE ID
input_file_id=batch_input_file.id,
endpoint="/v1/chat/completions",
completion_window="24h",
metadata={"description": "Test batch job"},
)
print(batch)
batch_id = batch.id

# Retrieve batch

batch_response = client.batches.retrieve( # LOG VIRTUAL MODEL NAME
batch_id
)
status = batch_response.status

print(f"status: {status}, output_file_id: {batch_response.output_file_id}")

# Download file
output_file_id = batch_response.output_file_id
print(f"output_file_id: {output_file_id}")
if not output_file_id:
output_file_id = batch_response.error_file_id

if output_file_id:
file_response = client.files.content(
output_file_id
)
raw_responses = file_response.text.strip().split("\n")

with open(
Path.cwd().parent / "unified_batch_output.json", "w"
) as output_file:
for raw_response in raw_responses:
json.dump(json.loads(raw_response), output_file)
output_file.write("\n")
## List Batch

list_batch_response = client.batches.list( # LOG VIRTUAL MODEL NAME
extra_query={"target_model_names": "gpt-4o-batch"}
)

## Cancel Batch

batch_response = client.batches.cancel( # LOG VIRTUAL MODEL NAME
batch_id
)
status = batch_response.status

print(f"status: {status}")

常见问题

我的文件写入到哪里?

当指定 target_model_names 时,文件会写入到所有与 target_model_names 匹配的部署。

无需额外的基础设施。