/batches
涵盖 批处理、文件
特性 | 支持 | 备注 |
---|---|---|
支持的提供商 | OpenAI, Azure, Vertex | - |
✨ 成本跟踪 | ✅ | 仅限 LiteLLM 企业版 |
日志记录 | ✅ | 适用于所有日志记录集成 |
快速入门
创建用于批处理完成的文件
创建批处理请求
列出批处理
检索特定的批处理和文件内容
- LiteLLM 代理服务器
- SDK
$ export OPENAI_API_KEY="sk-..."
$ litellm
# RUNNING on http://0.0.0.0:4000
创建用于批处理完成的文件
curl http://localhost:4000/v1/files \
-H "Authorization: Bearer sk-1234" \
-F purpose="batch" \
-F file="@mydata.jsonl"
创建批处理请求
curl http://localhost:4000/v1/batches \
-H "Authorization: Bearer sk-1234" \
-H "Content-Type: application/json" \
-d '{
"input_file_id": "file-abc123",
"endpoint": "/v1/chat/completions",
"completion_window": "24h"
}'
检索特定的批处理
curl http://localhost:4000/v1/batches/batch_abc123 \
-H "Authorization: Bearer sk-1234" \
-H "Content-Type: application/json" \
列出批处理
curl http://localhost:4000/v1/batches \
-H "Authorization: Bearer sk-1234" \
-H "Content-Type: application/json" \
创建用于批处理完成的文件
from litellm
import os
os.environ["OPENAI_API_KEY"] = "sk-.."
file_name = "openai_batch_completions.jsonl"
_current_dir = os.path.dirname(os.path.abspath(__file__))
file_path = os.path.join(_current_dir, file_name)
file_obj = await litellm.acreate_file(
file=open(file_path, "rb"),
purpose="batch",
custom_llm_provider="openai",
)
print("Response from creating file=", file_obj)
创建批处理请求
from litellm
import os
create_batch_response = await litellm.acreate_batch(
completion_window="24h",
endpoint="/v1/chat/completions",
input_file_id=batch_input_file_id,
custom_llm_provider="openai",
metadata={"key1": "value1", "key2": "value2"},
)
print("response from litellm.create_batch=", create_batch_response)
检索特定的批处理和文件内容
retrieved_batch = await litellm.aretrieve_batch(
batch_id=create_batch_response.id, custom_llm_provider="openai"
)
print("retrieved batch=", retrieved_batch)
# just assert that we retrieved a non None batch
assert retrieved_batch.id == create_batch_response.id
# try to get file content for our original file
file_content = await litellm.afile_content(
file_id=batch_input_file_id, custom_llm_provider="openai"
)
print("file content = ", file_content)
列出批处理
list_batches_response = litellm.list_batches(custom_llm_provider="openai", limit=2)
print("list_batches_response=", list_batches_response)
支持的提供商:
Azure OpenAI
OpenAI
Vertex AI
批处理 API 的成本跟踪工作原理
LiteLLM 通过记录两个关键事件来跟踪批处理成本
事件类型 | 描述 | 记录时间 |
---|---|---|
acreate_batch | 初始批处理创建 | 提交批处理请求时 |
batch_success | 最终使用量和成本 | 批处理完成时 |
成本计算
- LiteLLM 轮询批处理状态直到完成
- 完成时,它会聚合输出文件中所有响应的使用量和成本
- 总计
token
和response_cost
反映了跨所有批处理响应的组合指标