फ़ाइल इनपुट करने के तरीके

इस गाइड में, Gemini API को अनुरोध भेजते समय, इमेज, ऑडियो, वीडियो, और दस्तावेज़ जैसी मीडिया फ़ाइलें शामिल करने के अलग-अलग तरीकों के बारे में बताया गया है. नए तरीके, Gemini API के सभी एंडपॉइंट पर काम करते हैं. इनमें बैच, इंटरैक्शन, और Live API शामिल हैं. सही तरीका चुनना, आपकी फ़ाइल के साइज़, डेटा सेव करने की जगह, और फ़ाइल को कितनी बार इस्तेमाल करने की योजना पर निर्भर करता है.

किसी फ़ाइल को इनपुट के तौर पर शामिल करने का सबसे आसान तरीका है कि स्थानीय फ़ाइल को पढ़ा जाए और उसे किसी प्रॉम्प्ट में शामिल किया जाए. यहां दिए गए उदाहरण में, स्थानीय पीडीएफ़ फ़ाइल को पढ़ने का तरीका बताया गया है. इस तरीके के लिए, पीडीएफ़ का साइज़ 50 एमबी से ज़्यादा नहीं होना चाहिए. फ़ाइल इनपुट टाइप और सीमाओं की पूरी सूची देखने के लिए, इनपुट के तरीके की तुलना करने वाली टेबल देखें.

Python

from google import genai
import pathlib
import base64

client = genai.Client()

filepath = pathlib.Path('my_local_file.pdf')

prompt = "Summarize this document"
interaction = client.interactions.create(
    model="gemini-3.7-flash",
    input=[
        {"type": "text", "text": prompt},
        {"type": "document", "data": base64.b64encode(filepath.read_bytes()).decode('utf-8'), "mime_type": "application/pdf"}
    ]
)
print(interaction.output_text)

JavaScript

import { GoogleGenAI } from "@google/genai";
import * as fs from 'node:fs';

const client = new GoogleGenAI({});
const prompt = "Summarize this document";

async function main() {
    const filePath = 'my_local_file.pdf';

    const interaction = await client.interactions.create({
        model: "gemini-3.7-flash",
        input: [
            { type: "text", text: prompt },
            {
                type: "document",
                data: fs.readFileSync(filePath).toString("base64"),
                mime_type: "application/pdf"
            }
        ]
    });
    console.log(interaction.output_text);
}

main();

Java

import com.google.genai.Client;
import com.google.genai.gaos.models.interactions.Content;
import com.google.genai.gaos.models.interactions.CreateModelInteraction;
import com.google.genai.gaos.models.interactions.DocumentContent;
import com.google.genai.gaos.models.interactions.DocumentContentMimeType;
import com.google.genai.gaos.models.interactions.Interaction;
import com.google.genai.gaos.models.interactions.InteractionsInput;
import com.google.genai.gaos.models.interactions.Model;
import com.google.genai.gaos.models.interactions.TextContent;
import com.google.genai.gaos.models.operations.CreateInteractionRequestBody;
import java.util.Arrays;
import java.util.List;

Client client = new Client();

Content textContent = TextContent.builder().text("Summarize this document.").build();
Content docContent =
    DocumentContent.builder()
        .uri("gs://cloud-samples-data/generative-ai/pdf/sample.pdf")
        .mimeType(DocumentContentMimeType.APPLICATION_PDF)
        .build();

List<Content> contents = Arrays.asList(textContent, docContent);

CreateModelInteraction params =
    CreateModelInteraction.builder()
        .model(Model.of("gemini-3.7-flash"))
        .input(InteractionsInput.ofContent(contents))
        .build();

Interaction interaction =
    client.interactions.create(CreateInteractionRequestBody.of(params)).interaction().get();

System.out.println(interaction.outputText().orElse(""));

REST

# Encode the local file to base64
B64_CONTENT=$(base64 -w 0 my_local_file.pdf)

curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
  -H "x-goog-api-key: $GEMINI_API_KEY" \
  -H 'Content-Type: application/json' \
  -d '{
    "model": "gemini-3.7-flash",
    "input": [
      {"type": "text", "text": "Summarize this document"},
      {
        "type": "document",
        "data": "'${B64_CONTENT}'",
        "mime_type": "application/pdf"
      }
    ]
  }'

इनपुट के तरीके की तुलना

यहां दी गई टेबल में, फ़ाइल की सीमाओं और सबसे सही इस्तेमाल के उदाहरणों के साथ, हर इनपुट तरीके की तुलना की गई है. ध्यान दें कि फ़ाइल के टाइप और उसे प्रोसेस करने के लिए इस्तेमाल किए गए मॉडल या टोकनाइज़र के हिसाब से, फ़ाइल के साइज़ की सीमा अलग-अलग हो सकती है.

तरीका इन स्थितियों में बेहतर है अधिकतम फ़ाइल आकार स्थायी
इनलाइन डेटा क्विक टेस्टिंग, छोटी फ़ाइलें, रीयल-टाइम ऐप्लिकेशन. हर अनुरोध या पेलोड के लिए 100 एमबी
(पीडीएफ़ के लिए 50 एमबी)
कोई नहीं (हर अनुरोध के साथ भेजा जाता है)
File API से अपलोड करना बड़ी फ़ाइलें, वे फ़ाइलें जिनका इस्तेमाल कई बार किया जाता है. हर फ़ाइल के लिए 2 जीबी,
हर प्रोजेक्ट के लिए 20 जीबी तक
48 घंटे
File API से GCS यूआरआई रजिस्टर करना बड़ी फ़ाइलें जो पहले से ही Google Cloud Storage में मौजूद हैं, वे फ़ाइलें जिनका इस्तेमाल कई बार किया जाता है. हर फ़ाइल के लिए 2 जीबी, स्टोरेज की कोई सीमा नहीं कोई नहीं (हर अनुरोध के लिए फ़ेच किया जाता है). एक बार रजिस्टर करने पर, 30 दिनों तक ऐक्सेस मिल सकता है.
एक्सटर्नल यूआरएल पब्लिक डेटा या क्लाउड बकेट (AWS, Azure, GCS) में मौजूद डेटा को फिर से अपलोड किए बिना इस्तेमाल करना. हर अनुरोध/पेलोड के लिए 100 एमबी कोई नहीं (हर अनुरोध के लिए फ़ेच किया जाता है)

इनलाइन डेटा

छोटी फ़ाइलों (100 एमबी से कम या पीडीएफ़ के लिए 50 एमबी) के लिए, डेटा को सीधे अनुरोध के पेलोड में पास किया जा सकता है. क्विक टेस्ट या रीयल-टाइम, ट्रांज़िएंट डेटा को मैनेज करने वाले ऐप्लिकेशन के लिए यह सबसे आसान तरीका है. बेस64 एनकोड की गई स्ट्रिंग के तौर पर डेटा उपलब्ध कराया जा सकता है या स्थानीय फ़ाइलों को सीधे पढ़कर डेटा उपलब्ध कराया जा सकता है.

स्थानीय फ़ाइल से पढ़ने के उदाहरण के लिए, इस पेज की शुरुआत में दिया गया उदाहरण देखें.

यूआरएल से फ़ेच करना

किसी यूआरएल से फ़ाइल फ़ेच की जा सकती है, उसे बाइट में बदला जा सकता है, और इनपुट में शामिल किया जा सकता है.

Python

from google import genai
import httpx

client = genai.Client()

doc_url = "https://discovery.ucl.ac.uk/id/eprint/10089234/1/343019_3_art_0_py4t4l_convrt.pdf"
doc_data = httpx.get(doc_url).content

prompt = "Summarize this document"

interaction = client.interactions.create(
    model="gemini-3.7-flash",
    input=[
        {"type": "document", "data": base64.b64encode(doc_data).decode('utf-8'), "mime_type": "application/pdf"},
        {"type": "text", "text": prompt}
    ]
)
print(interaction.output_text)

JavaScript

import { GoogleGenAI } from "@google/genai";

const client = new GoogleGenAI({});
const docUrl = 'https://discovery.ucl.ac.uk/id/eprint/10089234/1/343019_3_art_0_py4t4l_convrt.pdf';
const prompt = "Summarize this document";

async function main() {
    const pdfResp = await fetch(docUrl)
      .then((response) => response.arrayBuffer());

    const interaction = await client.interactions.create({
        model: "gemini-3.7-flash",
        input: [
            { type: "text", text: prompt },
            {
                type: "document",
                data: Buffer.from(pdfResp).toString("base64"),
                mime_type: "application/pdf"
            }
        ]
    });
    console.log(interaction.output_text);
}

main();

Java

import com.google.genai.Client;
import com.google.genai.gaos.models.interactions.Content;
import com.google.genai.gaos.models.interactions.CreateModelInteraction;
import com.google.genai.gaos.models.interactions.DocumentContent;
import com.google.genai.gaos.models.interactions.DocumentContentMimeType;
import com.google.genai.gaos.models.interactions.Interaction;
import com.google.genai.gaos.models.interactions.InteractionsInput;
import com.google.genai.gaos.models.interactions.Model;
import com.google.genai.gaos.models.interactions.TextContent;
import com.google.genai.gaos.models.operations.CreateInteractionRequestBody;
import java.util.Arrays;
import java.util.List;

Client client = new Client();

Content textContent = TextContent.builder().text("Summarize this document.").build();
Content docContent =
    DocumentContent.builder()
        .uri("gs://cloud-samples-data/generative-ai/pdf/sample.pdf")
        .mimeType(DocumentContentMimeType.APPLICATION_PDF)
        .build();

List<Content> contents = Arrays.asList(textContent, docContent);

CreateModelInteraction params =
    CreateModelInteraction.builder()
        .model(Model.of("gemini-3.7-flash"))
        .input(InteractionsInput.ofContent(contents))
        .build();

Interaction interaction =
    client.interactions.create(CreateInteractionRequestBody.of(params)).interaction().get();

System.out.println(interaction.outputText().orElse(""));

REST

DOC_URL="https://discovery.ucl.ac.uk/id/eprint/10089234/1/343019_3_art_0_py4t4l_convrt.pdf"
PROMPT="Summarize this document"
DISPLAY_NAME="base64_pdf"

# Download the PDF
wget -O "${DISPLAY_NAME}.pdf" "${DOC_URL}"

# Check for FreeBSD base64 and set flags accordingly
if [[ "$(base64 --version 2>&1)" = *"FreeBSD"* ]]; then
  B64FLAGS="--input"
else
  B64FLAGS="-w0"
fi

# Base64 encode the PDF
ENCODED_PDF=$(base64 $B64FLAGS "${DISPLAY_NAME}.pdf")

# Create JSON payload file
cat <<EOF > payload.json
{
"model": "gemini-3.7-flash",
"input": [
{"type": "document", "data": "${ENCODED_PDF}", "mime_type": "application/pdf"},
{"type": "text", "text": "${PROMPT}"}
]
}
EOF

# Generate content using interactions
curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
    -H "x-goog-api-key: $GEMINI_API_KEY" \
    -H 'Content-Type: application/json' \
    -d @payload.json 2> /dev/null > response.json

cat response.json
echo

jq ".outputs[] | select(.type == \"text\") | .text" response.json

Gemini File API

File API, बड़ी फ़ाइलों (2 जीबी तक) या उन फ़ाइलों के लिए डिज़ाइन किया गया है जिनका इस्तेमाल कई अनुरोधों में किया जाना है.

फ़ाइल अपलोड करने का स्टैंडर्ड तरीका

Gemini API पर कोई स्थानीय फ़ाइल अपलोड करें. इस तरीके से अपलोड की गई फ़ाइलें, अस्थायी तौर पर (48 घंटे) सेव की जाती हैं. साथ ही, मॉडल की मदद से इन्हें आसानी से वापस पाने के लिए प्रोसेस किया जाता है.

Python

from google import genai

client = genai.Client()

doc_file = client.files.upload(file="path/to/your/sample.pdf")
prompt = "Summarize this document"

interaction = client.interactions.create(
    model="gemini-3.7-flash",
    input=[
        {"type": "text", "text": prompt},
        {"type": "document", "uri": doc_file.uri, "mime_type": doc_file.mime_type}
    ]
)
print(interaction.output_text)

JavaScript

import { GoogleGenAI } from "@google/genai";

const client = new GoogleGenAI({});
const prompt = "Summarize this document";

async function main() {
  const filePath = "path/to/your/sample.pdf";

  const myfile = await client.files.upload({
    file: filePath,
    config: { mime_type: "application/pdf" },
  });

  const interaction = await client.interactions.create({
    model: "gemini-3.7-flash",
    input: [
        { type: "text", text: prompt },
        { type: "document", uri: myfile.uri, mime_type: myfile.mimeType }
    ]
  });
  console.log(interaction.output_text);
}

await main();

Java

import com.google.genai.Client;
import com.google.genai.gaos.models.interactions.Content;
import com.google.genai.gaos.models.interactions.CreateModelInteraction;
import com.google.genai.gaos.models.interactions.DocumentContent;
import com.google.genai.gaos.models.interactions.DocumentContentMimeType;
import com.google.genai.gaos.models.interactions.Interaction;
import com.google.genai.gaos.models.interactions.InteractionsInput;
import com.google.genai.gaos.models.interactions.Model;
import com.google.genai.gaos.models.interactions.TextContent;
import com.google.genai.gaos.models.operations.CreateInteractionRequestBody;
import java.util.Arrays;
import java.util.List;

Client client = new Client();

Content textContent = TextContent.builder().text("Summarize this document.").build();
Content docContent =
    DocumentContent.builder()
        .uri("gs://cloud-samples-data/generative-ai/pdf/sample.pdf")
        .mimeType(DocumentContentMimeType.APPLICATION_PDF)
        .build();

List<Content> contents = Arrays.asList(textContent, docContent);

CreateModelInteraction params =
    CreateModelInteraction.builder()
        .model(Model.of("gemini-3.7-flash"))
        .input(InteractionsInput.ofContent(contents))
        .build();

Interaction interaction =
    client.interactions.create(CreateInteractionRequestBody.of(params)).interaction().get();

System.out.println(interaction.outputText().orElse(""));

REST

FILE_PATH="path/to/sample.pdf"
MIME_TYPE=$(file -b --mime-type "${FILE_PATH}")
NUM_BYTES=$(wc -c < "${FILE_PATH}")
DISPLAY_NAME=DOCUMENT

tmp_header_file=upload-header.tmp

# Initial resumable request defining metadata.
curl "https://generativelanguage.googleapis.com/upload/v1beta/files" \
  -D "${tmp_header_file}" \
  -H "x-goog-api-key: $GEMINI_API_KEY" \
  -H "X-Goog-Upload-Protocol: resumable" \
  -H "X-Goog-Upload-Command: start" \
  -H "X-Goog-Upload-Header-Content-Length: ${NUM_BYTES}" \
  -H "X-Goog-Upload-Header-Content-Type: ${MIME_TYPE}" \
  -H "Content-Type: application/json" \
  -d "{'file': {'display_name': '${DISPLAY_NAME}'}}" 2> /dev/null

upload_url=$(grep -i "x-goog-upload-url: " "${tmp_header_file}" | cut -d" " -f2 | tr -d "\r")
rm "${tmp_header_file}"

# Upload the actual bytes.
curl "${upload_url}" \
  -H "Content-Length: ${NUM_BYTES}" \
  -H "X-Goog-Upload-Offset: 0" \
  -H "X-Goog-Upload-Command: upload, finalize" \
  --data-binary "@${FILE_PATH}" 2> /dev/null > file_info.json

file_uri=$(jq ".file.uri" file_info.json)

# Now use in an interaction
curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
    -H "x-goog-api-key: $GEMINI_API_KEY" \
    -H 'Content-Type: application/json' \
    -d '{
      "model": "gemini-3.7-flash",
      "input": [
        {"type": "text", "text": "Summarize this document"},
        {"type": "document", "uri": '$file_uri', "mime_type": "'${MIME_TYPE}'"}
      ]
    }'

Google Cloud Storage में मौजूद फ़ाइलें रजिस्टर करना

अगर आपका डेटा पहले से ही Google Cloud Storage में मौजूद है, तो उसे डाउनलोड करके फिर से अपलोड करने की ज़रूरत नहीं है. उसे सीधे File API से रजिस्टर किया जा सकता है.

  1. हर बकेट के लिए, सर्विस एजेंट को ऐक्सेस दें

    1. अपने Google Cloud प्रोजेक्ट में, Gemini API चालू करें.

    2. सर्विस एजेंट बनाएं:

      gcloud beta services identity create --service=generativelanguage.googleapis.com --project=<your_project>

    3. अपने स्टोरेज बकेट को पढ़ने के लिए, Gemini API के सर्विस एजेंट को अनुमतियां दें.

      उपयोगकर्ता को उन स्टोरेज बकेट के लिए, इस सर्विस एजेंट को Storage Object Viewer IAM की भूमिका असाइन करनी होगी जिनका इस्तेमाल करना है.

    डिफ़ॉल्ट रूप से, यह ऐक्सेस खत्म नहीं होता. हालांकि, इसे किसी भी समय बदला जा सकता है. अनुमतियां देने के लिए, Google Cloud Storage IAM SDK के कमांड भी इस्तेमाल किए जा सकते हैं.

  2. अपनी सेवा की पुष्टि करें

    ज़रूरी शर्तें

    • एपीआई चालू करना
    • सही अनुमतियों के साथ कोई सेवा खाता या एजेंट बनाएं.

    सबसे पहले, आपको उस सेवा के तौर पर पुष्टि करनी होगी जिसके पास स्टोरेज ऑब्जेक्ट व्यूअर की अनुमतियां हैं. यह इस बात पर निर्भर करता है कि आपका फ़ाइल मैनेजमेंट कोड किस एनवायरमेंट में चलेगा.

    Google Cloud के बाहर

    अगर आपका कोड Google Cloud के बाहर चल रहा है, जैसे कि आपके डेस्कटॉप पर, तो Google Cloud Console से खाते के क्रेडेंशियल डाउनलोड करने के लिए, यह तरीका अपनाएं:

    1. सर्विस एजेंट कंसोल पर जाएं
    2. काम का सेवा खाता चुनें
    3. कुंजियां टैब चुनें और कुंजी जोड़ें, नई कुंजी बनाएं को चुनें
    4. JSON कुंजी टाइप चुनें. साथ ही, ध्यान दें कि आपके कंप्यूटर पर फ़ाइल कहां डाउनलोड हुई है.

    ज़्यादा जानकारी के लिए, सेवा खाते की कुंजी के मैनेजमेंट के बारे में Google Cloud का आधिकारिक दस्तावेज़ देखें.

    इसके बाद, पुष्टि करने के लिए इन कमांड का इस्तेमाल करें. इन कमांड से यह माना जाता है कि आपका सेवा खाता, मौजूदा डायरेक्ट्री में service-account.json नाम से मौजूद है.

    Python

    from google.oauth2.service_account import Credentials
    
    GCS_READ_SCOPES = [
      'https://www.googleapis.com/auth/devstorage.read_only',
      'https://www.googleapis.com/auth/cloud-platform'
    ]
    
    SERVICE_ACCOUNT_FILE = 'service-account.json'
    
    credentials = Credentials.from_service_account_file(
        SERVICE_ACCOUNT_FILE,
        scopes=GCS_READ_SCOPES
    )
    

    Javascript

    const { GoogleAuth } = require('google-auth-library');
    
    const GCS_READ_SCOPES = [
      'https://www.googleapis.com/auth/devstorage.read_only',
      'https://www.googleapis.com/auth/cloud-platform'
    ];
    
    const SERVICE_ACCOUNT_FILE = 'service-account.json';
    
    const auth = new GoogleAuth({
      keyFile: SERVICE_ACCOUNT_FILE,
      scopes: GCS_READ_SCOPES
    });
    

    CLI

    gcloud auth application-default login \
      --client-id-file=service-account.json \
      --scopes='https://www.googleapis.com/auth/cloud-platform,https://www.googleapis.com/auth/devstorage.read_only'
    

    Google Cloud पर

    अगर आप सीधे Google Cloud में काम कर रहे हैं, जैसे कि Cloud Run फ़ंक्शन या Compute Engine इंस्टेंस का इस्तेमाल करके, तो आपके पास इंप्लिसिट क्रेडेंशियल होंगे. हालांकि, सही स्कोप देने के लिए, आपको फिर से पुष्टि करनी होगी.

    Python

    इस कोड से यह माना जाता है कि सेवा ऐसे एनवायरमेंट में चल रही है जहां ऐप्लिकेशन डिफ़ॉल्ट क्रेडेंशियल अपने-आप हासिल किए जा सकते हैं. जैसे, Cloud Run या Compute Engine.

    import google.auth
    
    GCS_READ_SCOPES = [
      'https://www.googleapis.com/auth/devstorage.read_only',
      'https://www.googleapis.com/auth/cloud-platform'
    ]
    
    credentials, project = google.auth.default(scopes=GCS_READ_SCOPES)
    

    JavaScript

    इस कोड से यह माना जाता है कि सेवा ऐसे एनवायरमेंट में चल रही है जहां ऐप्लिकेशन डिफ़ॉल्ट क्रेडेंशियल अपने-आप हासिल किए जा सकते हैं. जैसे, Cloud Run या Compute Engine.

    const { GoogleAuth } = require('google-auth-library');
    
    const auth = new GoogleAuth({
      scopes: [
        'https://www.googleapis.com/auth/devstorage.read_only',
        'https://www.googleapis.com/auth/cloud-platform'
      ]
    });
    

Java

java import com.google.genai.Client; import com.google.genai.gaos.models.interactions.Content; import com.google.genai.gaos.models.interactions.CreateModelInteraction; import com.google.genai.gaos.models.interactions.DocumentContent; import com.google.genai.gaos.models.interactions.DocumentContentMimeType; import com.google.genai.gaos.models.interactions.Interaction; import com.google.genai.gaos.models.interactions.InteractionsInput; import com.google.genai.gaos.models.interactions.Model; import com.google.genai.gaos.models.interactions.TextContent; import com.google.genai.gaos.models.operations.CreateInteractionRequestBody; import java.util.Arrays; import java.util.List; Client client = new Client(); Content textContent = TextContent.builder().text("Summarize this document.").build(); Content docContent = DocumentContent.builder() .uri("gs://cloud-samples-data/generative-ai/pdf/sample.pdf") .mimeType(DocumentContentMimeType.APPLICATION_PDF) .build(); List<Content> contents = Arrays.asList(textContent, docContent); CreateModelInteraction params = CreateModelInteraction.builder() .model(Model.of("gemini-3.7-flash")) .input(InteractionsInput.ofContent(contents)) .build(); Interaction interaction = client.interactions.create(CreateInteractionRequestBody.of(params)).interaction().get(); System.out.println(interaction.outputText().orElse(""));

CLI

यह एक इंटरैक्टिव कमांड है. Compute Engine जैसी सेवाओं के लिए, कॉन्फ़िगरेशन लेवल पर चल रही सेवा में स्कोप जोड़े जा सकते हैं. उदाहरण के लिए, उपयोगकर्ता मैनेज करने वाली सेवा दस्तावेज़ देखें.

gcloud auth application-default login \
--scopes="https://www.googleapis.com/auth/cloud-platform,https://www.googleapis.com/auth/devstorage.read_only"
  1. फ़ाइल रजिस्टर करना (Files API) फ़ाइलें रजिस्टर करने के लिए, Files API का इस्तेमाल करें. इससे Files API का पाथ जनरेट होता है. इसका इस्तेमाल सीधे Gemini API में किया जा सकता है.

    Python

    from google import genai
    
    client = genai.Client(credentials=credentials)
    
    registered_gcs_files = client.files.register_files(
        uris=["gs://my_bucket/some_object.pdf", "gs://bucket2/object2.txt"]
    )
    prompt = "Summarize this file."
    
    for f in registered_gcs_files.files:
      print(f.name)
      interaction = client.interactions.create(
        model="gemini-3.7-flash",
        input=[
          {"type": "text", "text": prompt},
          {"type": "document", "uri": f.uri, "mime_type": f.mime_type}
        ],
      )
      print(interaction.output_text)
    

    JavaScript

    import { GoogleGenAI } from "@google/genai";
    
    const ai = new GoogleGenAI({ auth: auth });
    
    async function main() {
        const registeredGcsFiles = await ai.files.registerFiles({
            uris: ["gs://my_bucket/some_object.pdf", "gs://bucket2/object2.txt"]
        });
    
        const prompt = "Summarize this file.";
    
        for (const file of registeredGcsFiles.files) {
            console.log(file.name);
            const interaction = await ai.interactions.create({
                model: "gemini-3.7-flash",
                input: [
                    { type: "text", text: prompt },
                    { type: "document", uri: file.uri, mime_type: file.mimeType }
                ]
            });
    
            console.log(interaction.output_text);
        }
    }
    
    main();
    

Java

import com.google.genai.Client;
import com.google.genai.gaos.models.interactions.Content;
import com.google.genai.gaos.models.interactions.CreateModelInteraction;
import com.google.genai.gaos.models.interactions.DocumentContent;
import com.google.genai.gaos.models.interactions.DocumentContentMimeType;
import com.google.genai.gaos.models.interactions.Interaction;
import com.google.genai.gaos.models.interactions.InteractionsInput;
import com.google.genai.gaos.models.interactions.Model;
import com.google.genai.gaos.models.interactions.TextContent;
import com.google.genai.gaos.models.operations.CreateInteractionRequestBody;
import java.util.Arrays;
import java.util.List;

Client client = new Client();

Content textContent = TextContent.builder().text("Summarize this document.").build();
Content docContent =
    DocumentContent.builder()
        .uri("gs://cloud-samples-data/generative-ai/pdf/sample.pdf")
        .mimeType(DocumentContentMimeType.APPLICATION_PDF)
        .build();

List<Content> contents = Arrays.asList(textContent, docContent);

CreateModelInteraction params =
    CreateModelInteraction.builder()
        .model(Model.of("gemini-3.7-flash"))
        .input(InteractionsInput.ofContent(contents))
        .build();

Interaction interaction =
    client.interactions.create(CreateInteractionRequestBody.of(params)).interaction().get();

System.out.println(interaction.outputText().orElse(""));

CLI

access_token=$(gcloud auth application-default print-access-token)
project_id=$(gcloud config get-value project)
curl -X POST https://generativelanguage.googleapis.com/v1beta/files:register \
    -H 'Content-Type: application/json' \
    -H "Authorization: Bearer ${access_token}" \
    -H "x-goog-user-project: ${project_id}" \
    -d '{"uris": ["gs://bucket/object1", "gs://bucket/object2"]}'

एक्सटर्नल एचटीटीपी / साइन किए गए यूआरएल

अनुरोध में, सार्वजनिक तौर पर ऐक्सेस किए जा सकने वाले एचटीटीपीएस यूआरएल या पहले से साइन किए गए यूआरएल सीधे पास किए जा सकते हैं. प्रोसेसिंग के दौरान, Gemini API सुरक्षित तरीके से कॉन्टेंट फ़ेच करेगा. यह 100 एमबी तक की उन फ़ाइलों के लिए सही है जिन्हें आपको फिर से अपलोड नहीं करना है.

Python

from google import genai

uri = "https://ontheline.trincoll.edu/images/bookdown/sample-local-pdf.pdf"
prompt = "Summarize this file"

client = genai.Client()

interaction = client.interactions.create(
    model="gemini-3.7-flash",
    input=[
        {"type": "document", "uri": uri, "mime_type": "application/pdf"},
        {"type": "text", "text": prompt}
    ]
)
print(interaction.output_text)

Javascript

import { GoogleGenAI } from '@google/genai';

const client = new GoogleGenAI({});

const uri = "https://ontheline.trincoll.edu/images/bookdown/sample-local-pdf.pdf";

async function main() {
  const interaction = await client.interactions.create({
    model: 'gemini-3.7-flash',
    input: [
      { type: "document", uri: uri, mime_type: "application/pdf" },
      { type: "text", text: "summarize this file" }
    ]
  });

  console.log(interaction.output_text);
}

main();

REST

curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
      -H 'x-goog-api-key: $GEMINI_API_KEY' \
      -H 'Content-Type: application/json' \
      -d '{
          "model": "gemini-3.7-flash",
          "input": [
            {"type": "text", "text": "Summarize this pdf"},
            {
              "type": "document",
              "uri": "https://ontheline.trincoll.edu/images/bookdown/sample-local-pdf.pdf",
              "mime_type": "application/pdf"
            }
          ]
        }'

सुलभता

पक्का करें कि आपके दिए गए यूआरएल, ऐसे पेजों पर न ले जाएं जिनके लिए लॉगिन करने की ज़रूरत हो या जिन पर पेवॉल लागू किया गया हो. प्राइवेट डेटाबेस के लिए, पक्का करें कि आपने सही ऐक्सेस अनुमतियों और समयसीमा के साथ साइन किया गया यूआरएल बनाया हो.

सुरक्षा जांच

सिस्टम, यूआरएल पर कॉन्टेंट मॉडरेशन की जांच करता है, ताकि यह पक्का किया जा सके कि वे सुरक्षा और नीति के स्टैंडर्ड के मुताबिक हैं. अगर यूआरएल इस जांच में पास नहीं होता है, तो आपको url_retrieval_status की वैल्यू URL_RETRIEVAL_STATUS_UNSAFE मिलेगी.

इस तरह के कॉन्टेंट प्रोसेस किए जा सकते हैं

इस्तेमाल किए जा सकने वाले फ़ाइल टाइप और सीमाओं की यह सूची, शुरुआती दिशा-निर्देश के तौर पर दी गई है. यह पूरी सूची नहीं है. इस्तेमाल किए जा सकने वाले टाइप का सेट बदल सकता है. साथ ही, इस्तेमाल किए जा रहे मॉडल और टोकनाइज़र के वर्शन के हिसाब से, यह अलग-अलग हो सकता है. इस्तेमाल न किए जा सकने वाले टाइप की वजह से गड़बड़ी होगी. इसके अलावा, इन फ़ाइल टाइप के लिए कॉन्टेंट वापस पाने की सुविधा, सिर्फ़ सार्वजनिक तौर पर ऐक्सेस किए जा सकने वाले यूआरएल के लिए उपलब्ध है.

टेक्स्ट फ़ाइल के टाइप

  • text/html
  • text/css
  • text/plain
  • text/xml
  • text/csv
  • text/rtf
  • text/javascript

ऐप्लिकेशन फ़ाइल के टाइप

  • application/json
  • application/pdf

इमेज फ़ाइल के टाइप

  • image/bmp
  • image/jpeg
  • image/png
  • image/webp

वीडियो फ़ाइल के टाइप

  • video/mp4
  • video/mpeg
  • video/quicktime
  • video/avi
  • video/x-flv
  • video/mpg
  • video/webm
  • video/wmv
  • video/3gpp

सबसे सही तरीके

  • सही तरीका चुनें: छोटी, ट्रांज़िएंट फ़ाइलों के लिए, इनलाइन डेटा का इस्तेमाल करें. बड़ी या अक्सर इस्तेमाल की जाने वाली फ़ाइलों के लिए, File API का इस्तेमाल करें. ऑनलाइन होस्ट किए गए डेटा के लिए, एक्सटर्नल यूआरएल का इस्तेमाल करें.
  • एमआईएमई टाइप की जानकारी दें: सही प्रोसेसिंग के लिए, फ़ाइल डेटा का सही एमआईएमई टाइप हमेशा दें.
  • गड़बड़ियों को मैनेज करें: अपने कोड में गड़बड़ी को मैनेज करने की सुविधा लागू करें, ताकि नेटवर्क की गड़बड़ियों, फ़ाइल ऐक्सेस की समस्याओं या एपीआई की गड़बड़ियों जैसी संभावित समस्याओं को मैनेज किया जा सके.

सीमाएं

  • फ़ाइल के साइज़ की सीमाएं, तरीके (तुलना करने वाली टेबल देखें) और फ़ाइल के टाइप के हिसाब से अलग-अलग होती हैं.
  • इनलाइन डेटा से, अनुरोध के पेलोड का साइज़ बढ़ जाता है.
  • File API से अपलोड की गई फ़ाइलें अस्थायी होती हैं और 48 घंटे बाद खत्म हो जाती हैं.
  • एक्सटर्नल यूआरएल से फ़ेच करने की सीमा, हर पेलोड के लिए 100 एमबी है. साथ ही, यह कुछ खास तरह के कॉन्टेंट के लिए ही काम करता है.

आगे क्या करना है

  • Google AI Studio का इस्तेमाल करके, मल्टीमॉडल प्रॉम्प्ट लिखने की कोशिश करें.
  • अपने प्रॉम्प्ट में फ़ाइलें शामिल करने के बारे में जानकारी पाने के लिए, Vision, ऑडियो, और दस्तावेज़ प्रोसेसिंग की गाइड देखें.