跳到主要内容

[BETA]LiteLLM 管理文件

在不同提供商之间重复使用相同文件。

信息

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

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

功能描述备注
代理
SDK需要使用 postgres DB 存储文件 ID
适用于所有提供商

LiteLLM 管理文件的限制

  • 仅适用于 /chat/completions/batch 请求。

点击此处了解多模型、批量请求支持。

1. 设置 config.yaml

model_list:
- model_name: "gemini-2.0-flash"
litellm_params:
model: vertex_ai/gemini-2.0-flash
vertex_project: my-project-id
vertex_location: us-central1
- model_name: "gpt-4o-mini-openai"
litellm_params:
model: gpt-4o-mini
api_key: os.environ/OPENAI_API_KEY

2. 启动代理

litellm --config /path/to/config.yaml

3. 测试!

指定 target_model_names 以在不同提供商之间使用相同的文件 ID。这是通过 config.yaml(或 UI 上的 'public_model_names')设置的模型名称列表。

target_model_names="gpt-4o-mini-openai, gemini-2.0-flash" # 👈 Specify model_names

检查 /v1/models 以查看密钥可用的模型名称列表。

存储 PDF 文件

from openai import OpenAI

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


# Download and save the PDF locally
url = (
"https://storage.googleapis.com/cloud-samples-data/generative-ai/pdf/2403.05530.pdf"
)
response = requests.get(url)
response.raise_for_status()

# Save the PDF locally
with open("2403.05530.pdf", "wb") as f:
f.write(response.content)

file = client.files.create(
file=open("2403.05530.pdf", "rb"),
purpose="user_data", # can be any openai 'purpose' value
extra_body={"target_model_names": "gpt-4o-mini-openai, gemini-2.0-flash"}, # 👈 Specify model_names
)

print(f"file id={file.id}")

在不同提供商之间使用相同文件 ID

completion = client.chat.completions.create(
model="gpt-4o-mini-openai",
messages=[
{
"role": "user",
"content": [
{"type": "text", "text": "What is in this recording?"},
{
"type": "file",
"file": {
"file_id": file.id,
},
},
],
},
]
)

print(completion.choices[0].message)

完整示例

import base64
import requests
from openai import OpenAI

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


# Download and save the PDF locally
url = (
"https://storage.googleapis.com/cloud-samples-data/generative-ai/pdf/2403.05530.pdf"
)
response = requests.get(url)
response.raise_for_status()

# Save the PDF locally
with open("2403.05530.pdf", "wb") as f:
f.write(response.content)

# Read the local PDF file
file = client.files.create(
file=open("2403.05530.pdf", "rb"),
purpose="user_data", # can be any openai 'purpose' value
extra_body={"target_model_names": "gpt-4o-mini-openai, vertex_ai/gemini-2.0-flash"},
)

print(f"file.id: {file.id}") # 👈 Unified file id

## GEMINI CALL ###
completion = client.chat.completions.create(
model="gemini-2.0-flash",
messages=[
{
"role": "user",
"content": [
{"type": "text", "text": "What is in this recording?"},
{
"type": "file",
"file": {
"file_id": file.id,
},
},
],
},
]
)

print(completion.choices[0].message)


### OPENAI CALL ###
completion = client.chat.completions.create(
model="gpt-4o-mini-openai",
messages=[
{
"role": "user",
"content": [
{"type": "text", "text": "What is in this recording?"},
{
"type": "file",
"file": {
"file_id": file.id,
},
},
],
},
],
)

print(completion.choices[0].message)

支持的端点

创建文件 - /files

from openai import OpenAI

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

# Download and save the PDF locally
url = (
"https://storage.googleapis.com/cloud-samples-data/generative-ai/pdf/2403.05530.pdf"
)
response = requests.get(url)
response.raise_for_status()

# Save the PDF locally
with open("2403.05530.pdf", "wb") as f:
f.write(response.content)

# Read the local PDF file
file = client.files.create(
file=open("2403.05530.pdf", "rb"),
purpose="user_data", # can be any openai 'purpose' value
extra_body={"target_model_names": "gpt-4o-mini-openai, vertex_ai/gemini-2.0-flash"},
)

检索文件 - /files/{file_id}

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

file = client.files.retrieve(file_id=file.id)

删除文件 - /files/{file_id}/delete

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

file = client.files.delete(file_id=file.id)

常见问题

1. LiteLLM 是否存储文件?

不,LiteLLM 不存储文件。它只在 postgres DB 中存储文件 ID。

2. LiteLLM 如何知道给定文件 ID 使用哪个文件?

LiteLLM 在 postgres DB 中存储了 litellm 文件 ID 到模型特定文件 ID 的映射。当请求到来时,LiteLLM 查找模型特定文件 ID 并在发送给提供商的请求中使用它。

3. 文件删除如何工作?

当文件被删除时,LiteLLM 会从 postgres DB 中删除映射,并删除每个提供商上的文件。

架构