Gemini может одновременно обрабатывать различные типы входных данных, включая текст, изображения и аудио.
В этом руководстве показано, как работать с медиафайлами с помощью Files API. Основные операции одинаковы для аудиофайлов, изображений, видео, документов и других поддерживаемых типов файлов.
Инструкции по настройке параметров файлов см. в разделе « Руководство по настройке параметров файлов» .
Загрузите файл
Для загрузки медиафайлов можно использовать Files API. Всегда используйте Files API, если общий размер запроса (включая файлы, текстовое сообщение, системные инструкции и т. д.) превышает 100 МБ. Для файлов PDF лимит составляет 50 МБ.
Следующий код загружает файл, а затем использует этот файл в вызове метода interactions.create .
Python
from google import genai
client = genai.Client()
myfile = client.files.upload(file="path/to/sample.mp3")
interaction = client.interactions.create(
model="gemini-3.8-flash",
input=[
{"type": "text", "text": "Describe this audio clip"},
{"type": "audio", "uri": myfile.uri, "mime_type": myfile.mime_type}
]
)
print(interaction.output_text)
JavaScript
import { GoogleGenAI } from "@google/genai";
const client = new GoogleGenAI({});
async function main() {
const myfile = await client.files.upload({
file: "path/to/sample.mp3",
config: { mime_type: "audio/mpeg" },
});
const interaction = await client.interactions.create({
model: "gemini-3.8-flash",
input: [
{ type: "text", text: "Describe this audio clip" },
{ type: "audio", uri: myfile.uri, mime_type: myfile.mimeType }
]
});
console.log(interaction.output_text);
}
await main();
Java
import com.google.genai.Client;
import com.google.genai.types.File;
import com.google.genai.types.UploadFileConfig;
Client client = new Client();
File file = client.files.upload(
new java.io.File("path/to/sample.txt"),
UploadFileConfig.builder().mimeType("text/plain").build()
);
System.out.println("Uploaded file URI: " + file.uri().orElse(""));
Идти
file, err := client.Files.UploadFromPath(ctx, "path/to/sample.mp3", nil)
if err != nil {
log.Fatal(err)
}
defer client.Files.Delete(ctx, file.Name)
interaction, err := client.Interactions.Create(ctx, "gemini-3.8-flash", &genai.InteractionRequest{
Input: []interface{}{
genai.NewPartFromFile(*file),
genai.NewPartFromText("Describe this audio clip"),
},
}, nil)
if err != nil {
log.Fatal(err)
}
// Print the model's text response
for _, step := range interaction.Steps {
if step.Type == "model_output" {
for _, part := range step.Content {
if part.Type == "text" {
fmt.Println(part.Text)
}
}
}
}
ОТДЫХ
AUDIO_PATH="path/to/sample.mp3"
MIME_TYPE=$(file -b --mime-type "${AUDIO_PATH}")
NUM_BYTES=$(wc -c < "${AUDIO_PATH}")
DISPLAY_NAME=AUDIO
tmp_header_file=upload-header.tmp
# Initial resumable request defining metadata.
# The upload url is in the response headers dump them to a file.
curl "${BASE_URL}/upload/v1beta/files" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-D "${tmp_header_file}