跳至主要內容

容器檔案 API

管理 Code Interpreter 容器中的檔案。當 code interpreter 產生輸出(圖表、CSV、圖片等)時,檔案會自動建立。

提示

想了解如何使用 Code Interpreter 嗎?請參閱 Code Interpreter 指南。

功能支援
成本追蹤✅
記錄✅
支援的提供者openai, azure

端點​

端點方法描述
/v1/containers/{container_id}/filesPOST上傳檔案至容器
/v1/containers/{container_id}/filesGET列出容器中的檔案
/v1/containers/{container_id}/files/{file_id}GET取得檔案中繼資料
/v1/containers/{container_id}/files/{file_id}/contentGET下載檔案內容
/v1/containers/{container_id}/files/{file_id}DELETE刪除檔案

LiteLLM Python SDK​

上傳容器檔案​

直接上傳檔案到容器工作階段。當 /chat/completions 或 /responses 將檔案傳送到容器,但輸入檔案類型僅限於 PDF 時,這會很有用。這個端點可讓您處理其他檔案類型,例如 CSV、Excel、Python 指令碼等。

upload_container_file.py
from litellm import upload_container_file

# Upload a CSV file
file = upload_container_file(
container_id="cntr_123...",
file=("data.csv", open("data.csv", "rb").read(), "text/csv"),
custom_llm_provider="openai"
)

print(f"Uploaded: {file.id}")
print(f"Path: {file.path}")

非同步:

aupload_container_file.py
from litellm import aupload_container_file

file = await aupload_container_file(
container_id="cntr_123...",
file=("script.py", b"print('hello world')", "text/x-python"),
custom_llm_provider="openai"
)

支援的檔案格式:

  • CSV (.csv)
  • Excel (.xlsx)
  • Python 指令碼 (.py)
  • JSON (.json)
  • Markdown (.md)
  • 文字檔 (.txt)
  • 還有更多...

列出容器檔案​

list_container_files.py
from litellm import list_container_files

files = list_container_files(
container_id="cntr_123...",
custom_llm_provider="openai"
)

for file in files.data:
print(f" - {file.id}: {file.filename}")

非同步:

alist_container_files.py
from litellm import alist_container_files

files = await alist_container_files(
container_id="cntr_123...",
custom_llm_provider="openai"
)

取得容器檔案​

retrieve_container_file.py
from litellm import retrieve_container_file

file = retrieve_container_file(
container_id="cntr_123...",
file_id="cfile_456...",
custom_llm_provider="openai"
)

print(f"File: {file.filename}")
print(f"Size: {file.bytes} bytes")

下載檔案內容​

retrieve_container_file_content.py
from litellm import retrieve_container_file_content

content = retrieve_container_file_content(
container_id="cntr_123...",
file_id="cfile_456...",
custom_llm_provider="openai"
)

# content is raw bytes
with open("output.png", "wb") as f:
f.write(content)

刪除容器檔案​

delete_container_file.py
from litellm import delete_container_file

result = delete_container_file(
container_id="cntr_123...",
file_id="cfile_456...",
custom_llm_provider="openai"
)

print(f"Deleted: {result.deleted}")

LiteLLM AI Gateway(Proxy)​

上傳檔案​

upload_file.py
from openai import OpenAI

client = OpenAI(
api_key="sk-<your-litellm-api-key>",
base_url="http://localhost:4000"
)

file = client.containers.files.create(
container_id="cntr_123...",
file=open("data.csv", "rb")
)

print(f"Uploaded: {file.id}")
print(f"Path: {file.path}")

列出檔案​

list_files.py
from openai import OpenAI

client = OpenAI(
api_key="sk-<your-litellm-api-key>",
base_url="http://localhost:4000"
)

files = client.containers.files.list(
container_id="cntr_123..."
)

for file in files.data:
print(f" - {file.id}: {file.filename}")

取得檔案中繼資料​

retrieve_file.py
from openai import OpenAI

client = OpenAI(
api_key="sk-<your-litellm-api-key>",
base_url="http://localhost:4000"
)

file = client.containers.files.retrieve(
container_id="cntr_123...",
file_id="cfile_456..."
)

print(f"File: {file.filename}")
print(f"Size: {file.bytes} bytes")

下載檔案內容​

download_content.py
from openai import OpenAI

client = OpenAI(
api_key="sk-<your-litellm-api-key>",
base_url="http://localhost:4000"
)

content = client.containers.files.content(
container_id="cntr_123...",
file_id="cfile_456..."
)

with open("output.png", "wb") as f:
f.write(content.read())

刪除檔案​

delete_file.py
from openai import OpenAI

client = OpenAI(
api_key="sk-<your-litellm-api-key>",
base_url="http://localhost:4000"
)

result = client.containers.files.delete(
container_id="cntr_123...",
file_id="cfile_456..."
)

print(f"Deleted: {result.deleted}")

參數​

上傳檔案​

參數類型必填描述
container_idstringYes容器 ID
fileFileTypesYes要上傳的檔案。可以是 (filename, content, content_type) 的 tuple、類檔案物件或 bytes

列出檔案​

參數類型必填描述
container_idstringYes容器 ID
afterstringNo分頁游標
limitintegerNo要回傳的項目數(1-100,預設:20)
orderstringNo排序順序:asc 或 desc

取得/刪除檔案​

參數類型必填描述
container_idstringYes容器 ID
file_idstringYes檔案 ID

回應物件​

ContainerFileObject​

ContainerFileObject
{
"id": "cfile_456...",
"object": "container.file",
"container_id": "cntr_123...",
"bytes": 12345,
"created_at": 1234567890,
"filename": "chart.png",
"path": "/mnt/data/chart.png",
"source": "code_interpreter"
}

ContainerFileListResponse​

ContainerFileListResponse
{
"object": "list",
"data": [...],
"first_id": "cfile_456...",
"last_id": "cfile_789...",
"has_more": false
}

DeleteContainerFileResponse​

DeleteContainerFileResponse
{
"id": "cfile_456...",
"object": "container.file.deleted",
"deleted": true
}

支援的提供者​

提供者狀態
OpenAI✅ 支援
Azure OpenAI✅ 支援

對於 Azure,請在 SDK 中傳入 custom_llm_provider="azure",並搭配 api_base 和 api_key,或將 custom-llm-provider: azure 傳送至 proxy。於 proxy 上使用 Azure deployment 的 model 建立的 container 會回傳一個 ID,將其檔案請求路由至該 deployment