跳到主要内容

[测试版]Google AI Studio (Gemini) 文件 API

使用此功能将文件上传至 Google AI Studio (Gemini)。

对于将大型媒体文件传递给 Gemini 的 /generateContent 端点非常有用。

操作支持
创建
删除
检索
列出

用法

import base64
import requests
from litellm import completion, create_file
import os


### UPLOAD FILE ###

# Fetch the audio file and convert it to a base64 encoded string
url = "https://cdn.openai.com/API/docs/audio/alloy.wav"
response = requests.get(url)
response.raise_for_status()
wav_data = response.content
encoded_string = base64.b64encode(wav_data).decode('utf-8')


file = create_file(
file=wav_data,
purpose="user_data",
extra_body={"custom_llm_provider": "gemini"},
api_key=os.getenv("GEMINI_API_KEY"),
)

print(f"file: {file}")

assert file is not None


### GENERATE CONTENT ###
completion = completion(
model="gemini-2.0-flash",
messages=[
{
"role": "user",
"content": [
{
"type": "text",
"text": "What is in this recording?"
},
{
"type": "file",
"file": {
"file_id": file.id,
"filename": "my-test-name",
"format": "audio/wav"
}
}
]
},
]
)

print(completion.choices[0].message)