Gemini मॉडल, PDF फ़ॉर्मैट में दस्तावेज़ों को प्रोसेस कर सकते हैं. इसके लिए, वे नेटिव विज़न का इस्तेमाल करके दस्तावेज़ के पूरे कॉन्टेक्स्ट को समझते हैं. यह टेक्स्ट निकालने की सामान्य सुविधा से ज़्यादा है. इससे Gemini ये काम कर सकता है:
- टेक्स्ट, इमेज, डायग्राम, चार्ट, और टेबल जैसे कॉन्टेंट का विश्लेषण करें और उसे समझें. यह सुविधा, 1, 000 पेजों तक के लंबे दस्तावेज़ों में भी काम करती है.
- जानकारी को स्ट्रक्चर्ड आउटपुट फ़ॉर्मैट में निकालें.
- दस्तावेज़ में मौजूद विज़ुअल और टेक्स्ट, दोनों एलिमेंट के आधार पर खास जानकारी पाएं और सवालों के जवाब पाएं.
- दस्तावेज़ के कॉन्टेंट को एचटीएमएल में ट्रांसक्राइब करना. साथ ही, लेआउट और फ़ॉर्मैटिंग को बनाए रखना, ताकि इसे डाउनस्ट्रीम ऐप्लिकेशन में इस्तेमाल किया जा सके.
इनलाइन PDF डेटा पास करना
अनुरोध में, generateContent
को इनलाइन PDF डेटा पास किया जा सकता है.
20 एमबी से कम के PDF पेलोड के लिए, आपके पास base64 में कोड किए गए दस्तावेज़ अपलोड करने या सीधे तौर पर लोकल स्टोरेज में मौजूद फ़ाइलें अपलोड करने का विकल्प होता है.
यहां दिए गए उदाहरण में, किसी यूआरएल से PDF फ़ेच करने और उसे प्रोसेस करने के लिए, बाइट में बदलने का तरीका बताया गया है:
Python
from google import genai
from google.genai import types
import httpx
client = genai.Client()
doc_url = "https://discovery.ucl.ac.uk/id/eprint/10089234/1/343019_3_art_0_py4t4l_convrt.pdf"
# Retrieve and encode the PDF byte
doc_data = httpx.get(doc_url).content
prompt = "Summarize this document"
response = client.models.generate_content(
model="gemini-2.5-flash",
contents=[
types.Part.from_bytes(
data=doc_data,
mime_type='application/pdf',
),
prompt])
print(response.text)
JavaScript
import { GoogleGenAI } from "@google/genai";
const ai = new GoogleGenAI({ apiKey: "GEMINI_API_KEY" });
async function main() {
const pdfResp = await fetch('https://discovery.ucl.ac.uk/id/eprint/10089234/1/343019_3_art_0_py4t4l_convrt.pdf')
.then((response) => response.arrayBuffer());
const contents = [
{ text: "Summarize this document" },
{
inlineData: {
mimeType: 'application/pdf',
data: Buffer.from(pdfResp).toString("base64")
}
}
];
const response = await ai.models.generateContent({
model: "gemini-2.5-flash",
contents: contents
});
console.log(response.text);
}
main();
शुरू करें
package main
import (
"context"
"fmt"
"io"
"net/http"
"os"
"google.golang.org/genai"
)
func main() {
ctx := context.Background()
client, _ := genai.NewClient(ctx, &genai.ClientConfig{
APIKey: os.Getenv("GEMINI_API_KEY"),
Backend: genai.BackendGeminiAPI,
})
pdfResp, _ := http.Get("https://discovery.ucl.ac.uk/id/eprint/10089234/1/343019_3_art_0_py4t4l_convrt.pdf")
var pdfBytes []byte
if pdfResp != nil && pdfResp.Body != nil {
pdfBytes, _ = io.ReadAll(pdfResp.Body)
pdfResp.Body.Close()
}
parts := []*genai.Part{
&genai.Part{
InlineData: &genai.Blob{
MIMEType: "application/pdf",
Data: pdfBytes,
},
},
genai.NewPartFromText("Summarize this document"),
}
contents := []*genai.Content{
genai.NewContentFromParts(parts, genai.RoleUser),
}
result, _ := client.Models.GenerateContent(
ctx,
"gemini-2.5-flash",
contents,
nil,
)
fmt.Println(result.Text())
}
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")
# Generate content using the base64 encoded PDF
curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent?key=$GOOGLE_API_KEY" \
-H 'Content-Type: application/json' \
-X POST \
-d '{
"contents": [{
"parts":[
{"inline_data": {"mime_type": "application/pdf", "data": "'"$ENCODED_PDF"'"}},
{"text": "'$PROMPT'"}
]
}]
}' 2> /dev/null > response.json
cat response.json
echo
jq ".candidates[].content.parts[].text" response.json
# Clean up the downloaded PDF
rm "${DISPLAY_NAME}.pdf"
प्रोसेस करने के लिए, किसी लोकल फ़ाइल से PDF भी पढ़ा जा सकता है:
Python
from google import genai
from google.genai import types
import pathlib
client = genai.Client()
# Retrieve and encode the PDF byte
filepath = pathlib.Path('file.pdf')
prompt = "Summarize this document"
response = client.models.generate_content(
model="gemini-2.5-flash",
contents=[
types.Part.from_bytes(
data=filepath.read_bytes(),
mime_type='application/pdf',
),
prompt])
print(response.text)
JavaScript
import { GoogleGenAI } from "@google/genai";
import * as fs from 'fs';
const ai = new GoogleGenAI({ apiKey: "GEMINI_API_KEY" });
async function main() {
const contents = [
{ text: "Summarize this document" },
{
inlineData: {
mimeType: 'application/pdf',
data: Buffer.from(fs.readFileSync("content/343019_3_art_0_py4t4l_convrt.pdf")).toString("base64")
}
}
];
const response = await ai.models.generateContent({
model: "gemini-2.5-flash",
contents: contents
});
console.log(response.text);
}
main();
शुरू करें
package main
import (
"context"
"fmt"
"os"
"google.golang.org/genai"
)
func main() {
ctx := context.Background()
client, _ := genai.NewClient(ctx, &genai.ClientConfig{
APIKey: os.Getenv("GEMINI_API_KEY"),
Backend: genai.BackendGeminiAPI,
})
pdfBytes, _ := os.ReadFile("path/to/your/file.pdf")
parts := []*genai.Part{
&genai.Part{
InlineData: &genai.Blob{
MIMEType: "application/pdf",
Data: pdfBytes,
},
},
genai.NewPartFromText("Summarize this document"),
}
contents := []*genai.Content{
genai.NewContentFromParts(parts, genai.RoleUser),
}
result, _ := client.Models.GenerateContent(
ctx,
"gemini-2.5-flash",
contents,
nil,
)
fmt.Println(result.Text())
}
File API का इस्तेमाल करके PDF अपलोड करना
बड़े दस्तावेज़ अपलोड करने के लिए, File API का इस्तेमाल किया जा सकता है. जब अनुरोध का कुल साइज़ (इसमें फ़ाइलें, टेक्स्ट प्रॉम्प्ट, सिस्टम के निर्देश वगैरह शामिल हैं) 20 एमबी से ज़्यादा हो, तो हमेशा File API का इस्तेमाल करें.
File API का इस्तेमाल करके फ़ाइल अपलोड करने के लिए, media.upload
को कॉल करें. नीचे दिया गया कोड, दस्तावेज़ फ़ाइल को अपलोड करता है. इसके बाद, models.generateContent
को कॉल करने के लिए फ़ाइल का इस्तेमाल करता है.
यूआरएल से बड़ी PDF फ़ाइलें
यूआरएल से बड़ी PDF फ़ाइलों को अपलोड करने और प्रोसेस करने के लिए, File API का इस्तेमाल करें:
Python
from google import genai
from google.genai import types
import io
import httpx
client = genai.Client()
long_context_pdf_path = "https://www.nasa.gov/wp-content/uploads/static/history/alsj/a17/A17_FlightPlan.pdf"
# Retrieve and upload the PDF using the File API
doc_io = io.BytesIO(httpx.get(long_context_pdf_path).content)
sample_doc = client.files.upload(
# You can pass a path or a file-like object here
file=doc_io,
config=dict(
mime_type='application/pdf')
)
prompt = "Summarize this document"
response = client.models.generate_content(
model="gemini-2.5-flash",
contents=[sample_doc, prompt])
print(response.text)
JavaScript
import { createPartFromUri, GoogleGenAI } from "@google/genai";
const ai = new GoogleGenAI({ apiKey: "GEMINI_API_KEY" });
async function main() {
const pdfBuffer = await fetch("https://www.nasa.gov/wp-content/uploads/static/history/alsj/a17/A17_FlightPlan.pdf")
.then((response) => response.arrayBuffer());
const fileBlob = new Blob([pdfBuffer], { type: 'application/pdf' });
const file = await ai.files.upload({
file: fileBlob,
config: {
displayName: 'A17_FlightPlan.pdf',
},
});
// Wait for the file to be processed.
let getFile = await ai.files.get({ name: file.name });
while (getFile.state === 'PROCESSING') {
getFile = await ai.files.get({ name: file.name });
console.log(`current file status: ${getFile.state}`);
console.log('File is still processing, retrying in 5 seconds');
await new Promise((resolve) => {
setTimeout(resolve, 5000);
});
}
if (file.state === 'FAILED') {
throw new Error('File processing failed.');
}
// Add the file to the contents.
const content = [
'Summarize this document',
];
if (file.uri && file.mimeType) {
const fileContent = createPartFromUri(file.uri, file.mimeType);
content.push(fileContent);
}
const response = await ai.models.generateContent({
model: 'gemini-2.5-flash',
contents: content,
});
console.log(response.text);
}
main();
शुरू करें
package main
import (
"context"
"fmt"
"io"
"net/http"
"os"
"google.golang.org/genai"
)
func main() {
ctx := context.Background()
client, _ := genai.NewClient(ctx, &genai.ClientConfig{
APIKey: os.Getenv("GEMINI_API_KEY"),
Backend: genai.BackendGeminiAPI,
})
pdfURL := "https://www.nasa.gov/wp-content/uploads/static/history/alsj/a17/A17_FlightPlan.pdf"
localPdfPath := "A17_FlightPlan_downloaded.pdf"
respHttp, _ := http.Get(pdfURL)
defer respHttp.Body.Close()
outFile, _ := os.Create(localPdfPath)
defer outFile.Close()
_, _ = io.Copy(outFile, respHttp.Body)
uploadConfig := &genai.UploadFileConfig{MIMEType: "application/pdf"}
uploadedFile, _ := client.Files.UploadFromPath(ctx, localPdfPath, uploadConfig)
promptParts := []*genai.Part{
genai.NewPartFromURI(uploadedFile.URI, uploadedFile.MIMEType),
genai.NewPartFromText("Summarize this document"),
}
contents := []*genai.Content{
genai.NewContentFromParts(promptParts, genai.RoleUser), // Specify role
}
result, _ := client.Models.GenerateContent(
ctx,
"gemini-2.5-flash",
contents,
nil,
)
fmt.Println(result.Text())
}
REST
PDF_PATH="https://www.nasa.gov/wp-content/uploads/static/history/alsj/a17/A17_FlightPlan.pdf"
DISPLAY_NAME="A17_FlightPlan"
PROMPT="Summarize this document"
# Download the PDF from the provided URL
wget -O "${DISPLAY_NAME}.pdf" "${PDF_PATH}"
MIME_TYPE=$(file -b --mime-type "${DISPLAY_NAME}.pdf")
NUM_BYTES=$(wc -c < "${DISPLAY_NAME}.pdf")
echo "MIME_TYPE: ${MIME_TYPE}"
echo "NUM_BYTES: ${NUM_BYTES}"
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?key=${GOOGLE_API_KEY}" \
-D upload-header.tmp \
-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 "@${DISPLAY_NAME}.pdf" 2> /dev/null > file_info.json
file_uri=$(jq ".file.uri" file_info.json)
echo "file_uri: ${file_uri}"
# Now generate content using that file
curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent?key=$GOOGLE_API_KEY" \
-H 'Content-Type: application/json' \
-X POST \
-d '{
"contents": [{
"parts":[
{"text": "'$PROMPT'"},
{"file_data":{"mime_type": "application/pdf", "file_uri": '$file_uri'}}]
}]
}' 2> /dev/null > response.json
cat response.json
echo
jq ".candidates[].content.parts[].text" response.json
# Clean up the downloaded PDF
rm "${DISPLAY_NAME}.pdf"
डिवाइस में सेव किए गए बड़े PDF
Python
from google import genai
from google.genai import types
import pathlib
import httpx
client = genai.Client()
# Retrieve and encode the PDF byte
file_path = pathlib.Path('large_file.pdf')
# Upload the PDF using the File API
sample_file = client.files.upload(
file=file_path,
)
prompt="Summarize this document"
response = client.models.generate_content(
model="gemini-2.5-flash",
contents=[sample_file, "Summarize this document"])
print(response.text)
JavaScript
import { createPartFromUri, GoogleGenAI } from "@google/genai";
const ai = new GoogleGenAI({ apiKey: "GEMINI_API_KEY" });
async function main() {
const file = await ai.files.upload({
file: 'path-to-localfile.pdf'
config: {
displayName: 'A17_FlightPlan.pdf',
},
});
// Wait for the file to be processed.
let getFile = await ai.files.get({ name: file.name });
while (getFile.state === 'PROCESSING') {
getFile = await ai.files.get({ name: file.name });
console.log(`current file status: ${getFile.state}`);
console.log('File is still processing, retrying in 5 seconds');
await new Promise((resolve) => {
setTimeout(resolve, 5000);
});
}
if (file.state === 'FAILED') {
throw new Error('File processing failed.');
}
// Add the file to the contents.
const content = [
'Summarize this document',
];
if (file.uri && file.mimeType) {
const fileContent = createPartFromUri(file.uri, file.mimeType);
content.push(fileContent);
}
const response = await ai.models.generateContent({
model: 'gemini-2.5-flash',
contents: content,
});
console.log(response.text);
}
main();
शुरू करें
package main
import (
"context"
"fmt"
"os"
"google.golang.org/genai"
)
func main() {
ctx := context.Background()
client, _ := genai.NewClient(ctx, &genai.ClientConfig{
APIKey: os.Getenv("GEMINI_API_KEY"),
Backend: genai.BackendGeminiAPI,
})
localPdfPath := "/path/to/file.pdf"
uploadConfig := &genai.UploadFileConfig{MIMEType: "application/pdf"}
uploadedFile, _ := client.Files.UploadFromPath(ctx, localPdfPath, uploadConfig)
promptParts := []*genai.Part{
genai.NewPartFromURI(uploadedFile.URI, uploadedFile.MIMEType),
genai.NewPartFromText("Give me a summary of this pdf file."),
}
contents := []*genai.Content{
genai.NewContentFromParts(promptParts, genai.RoleUser),
}
result, _ := client.Models.GenerateContent(
ctx,
"gemini-2.5-flash",
contents,
nil,
)
fmt.Println(result.Text())
}
REST
NUM_BYTES=$(wc -c < "${PDF_PATH}")
DISPLAY_NAME=TEXT
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?key=${GEMINI_API_KEY}" \
-D upload-header.tmp \
-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: application/pdf" \
-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 "@${PDF_PATH}" 2> /dev/null > file_info.json
file_uri=$(jq ".file.uri" file_info.json)
echo file_uri=$file_uri
# Now generate content using that file
curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent?key=$GOOGLE_API_KEY" \
-H 'Content-Type: application/json' \
-X POST \
-d '{
"contents": [{
"parts":[
{"text": "Can you add a few more lines to this poem?"},
{"file_data":{"mime_type": "application/pdf", "file_uri": '$file_uri'}}]
}]
}' 2> /dev/null > response.json
cat response.json
echo
jq ".candidates[].content.parts[].text" response.json
files.get
को कॉल करके, यह पुष्टि की जा सकती है कि अपलोड की गई फ़ाइल को एपीआई ने सही से सेव किया है या नहीं. साथ ही, उसका मेटाडेटा भी पाया जा सकता है. सिर्फ़ name
(और एक्सटेंशन के तौर पर, uri
) यूनीक होते हैं.
Python
from google import genai
import pathlib
client = genai.Client()
fpath = pathlib.Path('example.txt')
fpath.write_text('hello')
file = client.files.upload(file='example.txt')
file_info = client.files.get(name=file.name)
print(file_info.model_dump_json(indent=4))
REST
name=$(jq ".file.name" file_info.json)
# Get the file of interest to check state
curl https://generativelanguage.googleapis.com/v1beta/files/$name > file_info.json
# Print some information about the file you got
name=$(jq ".file.name" file_info.json)
echo name=$name
file_uri=$(jq ".file.uri" file_info.json)
echo file_uri=$file_uri
एक से ज़्यादा PDF फ़ाइलें पास करना
Gemini API, एक ही अनुरोध में कई PDF दस्तावेज़ों (ज़्यादा से ज़्यादा 1,000 पेजों तक) को प्रोसेस कर सकता है. हालांकि, ऐसा तब ही होगा, जब दस्तावेज़ों और टेक्स्ट प्रॉम्प्ट का कुल साइज़, मॉडल की कॉन्टेक्स्ट विंडो में रहे.
Python
from google import genai
import io
import httpx
client = genai.Client()
doc_url_1 = "https://arxiv.org/pdf/2312.11805"
doc_url_2 = "https://arxiv.org/pdf/2403.05530"
# Retrieve and upload both PDFs using the File API
doc_data_1 = io.BytesIO(httpx.get(doc_url_1).content)
doc_data_2 = io.BytesIO(httpx.get(doc_url_2).content)
sample_pdf_1 = client.files.upload(
file=doc_data_1,
config=dict(mime_type='application/pdf')
)
sample_pdf_2 = client.files.upload(
file=doc_data_2,
config=dict(mime_type='application/pdf')
)
prompt = "What is the difference between each of the main benchmarks between these two papers? Output these in a table."
response = client.models.generate_content(
model="gemini-2.5-flash",
contents=[sample_pdf_1, sample_pdf_2, prompt])
print(response.text)
JavaScript
import { createPartFromUri, GoogleGenAI } from "@google/genai";
const ai = new GoogleGenAI({ apiKey: "GEMINI_API_KEY" });
async function uploadRemotePDF(url, displayName) {
const pdfBuffer = await fetch(url)
.then((response) => response.arrayBuffer());
const fileBlob = new Blob([pdfBuffer], { type: 'application/pdf' });
const file = await ai.files.upload({
file: fileBlob,
config: {
displayName: displayName,
},
});
// Wait for the file to be processed.
let getFile = await ai.files.get({ name: file.name });
while (getFile.state === 'PROCESSING') {
getFile = await ai.files.get({ name: file.name });
console.log(`current file status: ${getFile.state}`);
console.log('File is still processing, retrying in 5 seconds');
await new Promise((resolve) => {
setTimeout(resolve, 5000);
});
}
if (file.state === 'FAILED') {
throw new Error('File processing failed.');
}
return file;
}
async function main() {
const content = [
'What is the difference between each of the main benchmarks between these two papers? Output these in a table.',
];
let file1 = await uploadRemotePDF("https://arxiv.org/pdf/2312.11805", "PDF 1")
if (file1.uri && file1.mimeType) {
const fileContent = createPartFromUri(file1.uri, file1.mimeType);
content.push(fileContent);
}
let file2 = await uploadRemotePDF("https://arxiv.org/pdf/2403.05530", "PDF 2")
if (file2.uri && file2.mimeType) {
const fileContent = createPartFromUri(file2.uri, file2.mimeType);
content.push(fileContent);
}
const response = await ai.models.generateContent({
model: 'gemini-2.5-flash',
contents: content,
});
console.log(response.text);
}
main();
शुरू करें
package main
import (
"context"
"fmt"
"io"
"net/http"
"os"
"google.golang.org/genai"
)
func main() {
ctx := context.Background()
client, _ := genai.NewClient(ctx, &genai.ClientConfig{
APIKey: os.Getenv("GEMINI_API_KEY"),
Backend: genai.BackendGeminiAPI,
})
docUrl1 := "https://arxiv.org/pdf/2312.11805"
docUrl2 := "https://arxiv.org/pdf/2403.05530"
localPath1 := "doc1_downloaded.pdf"
localPath2 := "doc2_downloaded.pdf"
respHttp1, _ := http.Get(docUrl1)
defer respHttp1.Body.Close()
outFile1, _ := os.Create(localPath1)
_, _ = io.Copy(outFile1, respHttp1.Body)
outFile1.Close()
respHttp2, _ := http.Get(docUrl2)
defer respHttp2.Body.Close()
outFile2, _ := os.Create(localPath2)
_, _ = io.Copy(outFile2, respHttp2.Body)
outFile2.Close()
uploadConfig1 := &genai.UploadFileConfig{MIMEType: "application/pdf"}
uploadedFile1, _ := client.Files.UploadFromPath(ctx, localPath1, uploadConfig1)
uploadConfig2 := &genai.UploadFileConfig{MIMEType: "application/pdf"}
uploadedFile2, _ := client.Files.UploadFromPath(ctx, localPath2, uploadConfig2)
promptParts := []*genai.Part{
genai.NewPartFromURI(uploadedFile1.URI, uploadedFile1.MIMEType),
genai.NewPartFromURI(uploadedFile2.URI, uploadedFile2.MIMEType),
genai.NewPartFromText("What is the difference between each of the " +
"main benchmarks between these two papers? " +
"Output these in a table."),
}
contents := []*genai.Content{
genai.NewContentFromParts(promptParts, genai.RoleUser),
}
modelName := "gemini-2.5-flash"
result, _ := client.Models.GenerateContent(
ctx,
modelName,
contents,
nil,
)
fmt.Println(result.Text())
}
REST
DOC_URL_1="https://arxiv.org/pdf/2312.11805"
DOC_URL_2="https://arxiv.org/pdf/2403.05530"
DISPLAY_NAME_1="Gemini_paper"
DISPLAY_NAME_2="Gemini_1.5_paper"
PROMPT="What is the difference between each of the main benchmarks between these two papers? Output these in a table."
# Function to download and upload a PDF
upload_pdf() {
local doc_url="$1"
local display_name="$2"
# Download the PDF
wget -O "${display_name}.pdf" "${doc_url}"
local MIME_TYPE=$(file -b --mime-type "${display_name}.pdf")
local NUM_BYTES=$(wc -c < "${display_name}.pdf")
echo "MIME_TYPE: ${MIME_TYPE}"
echo "NUM_BYTES: ${NUM_BYTES}"
local tmp_header_file=upload-header.tmp
# Initial resumable request
curl "${BASE_URL}/upload/v1beta/files?key=${GOOGLE_API_KEY}" \
-D "${tmp_header_file}" \
-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
local upload_url=$(grep -i "x-goog-upload-url: " "${tmp_header_file}" | cut -d" " -f2 | tr -d "\r")
rm "${tmp_header_file}"
# Upload the PDF
curl "${upload_url}" \
-H "Content-Length: ${NUM_BYTES}" \
-H "X-Goog-Upload-Offset: 0" \
-H "X-Goog-Upload-Command: upload, finalize" \
--data-binary "@${display_name}.pdf" 2> /dev/null > "file_info_${display_name}.json"
local file_uri=$(jq ".file.uri" "file_info_${display_name}.json")
echo "file_uri for ${display_name}: ${file_uri}"
# Clean up the downloaded PDF
rm "${display_name}.pdf"
echo "${file_uri}"
}
# Upload the first PDF
file_uri_1=$(upload_pdf "${DOC_URL_1}" "${DISPLAY_NAME_1}")
# Upload the second PDF
file_uri_2=$(upload_pdf "${DOC_URL_2}" "${DISPLAY_NAME_2}")
# Now generate content using both files
curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent?key=$GOOGLE_API_KEY" \
-H 'Content-Type: application/json' \
-X POST \
-d '{
"contents": [{
"parts":[
{"file_data": {"mime_type": "application/pdf", "file_uri": '$file_uri_1'}},
{"file_data": {"mime_type": "application/pdf", "file_uri": '$file_uri_2'}},
{"text": "'$PROMPT'"}
]
}]
}' 2> /dev/null > response.json
cat response.json
echo
jq ".candidates[].content.parts[].text" response.json
तकनीकी जानकारी
Gemini में ज़्यादा से ज़्यादा 1,000 दस्तावेज़ पेज हो सकते हैं. दस्तावेज़ का हर पेज 258 टोकन के बराबर होता है.
मॉडल की कंटेक्स्ट विंडो के अलावा, किसी दस्तावेज़ में पिक्सल की संख्या की कोई खास सीमा नहीं होती. हालांकि, बड़े पेजों को उनके मूल आसपेक्ट रेशियो को बनाए रखते हुए, 3072x3072 पिक्सल के ज़्यादा से ज़्यादा रिज़ॉल्यूशन तक स्केल किया जाता है. वहीं, छोटे पेजों को 768x768 पिक्सल तक स्केल किया जाता है. कम साइज़ वाले पेजों के लिए, बैंडविड्थ के अलावा कोई और लागत कम नहीं होती. इसके अलावा, ज़्यादा रिज़ॉल्यूशन वाले पेजों की परफ़ॉर्मेंस में भी कोई सुधार नहीं होता.
दस्तावेज़ के टाइप
तकनीकी तौर पर, दस्तावेज़ को समझने के लिए, TXT, मार्कडाउन, एचटीएमएल, एक्सएमएल वगैरह जैसे अन्य एमआईएमई टाइप इस्तेमाल किए जा सकते हैं. हालांकि, दस्तावेज़ विज़न सिर्फ़ PDF को समझता है. अन्य फ़ाइलों को सिर्फ़ टेक्स्ट के तौर पर निकाला जाएगा. साथ ही, मॉडल उन फ़ाइलों को रेंडर करने के दौरान दिखने वाली चीज़ों का विश्लेषण नहीं कर पाएगा. चार्ट, डायग्राम, एचटीएमएल टैग, मार्कडाउन फ़ॉर्मैटिंग वगैरह जैसी फ़ाइल टाइप की कोई भी जानकारी मिट जाएगी.
सबसे सही तरीके
सर्वोत्तम परिणामों के लिएः
- अपलोड करने से पहले, पेजों को सही ओरिएंटेशन में घुमाएं.
- धुंधले पेजों का इस्तेमाल न करें.
- अगर एक पेज का इस्तेमाल किया जा रहा है, तो टेक्स्ट प्रॉम्प्ट को पेज के बाद रखें.
आगे क्या करना है
ज़्यादा जानने के लिए, ये संसाधन देखें:
- फ़ाइल के लिए प्रॉम्प्ट करने की रणनीतियां: Gemini API, टेक्स्ट, इमेज, ऑडियो, और वीडियो डेटा के साथ प्रॉम्प्ट करने की सुविधा देता है. इसे मल्टीमॉडल प्रॉम्प्ट भी कहा जाता है.
- सिस्टम के निर्देश: सिस्टम के निर्देशों की मदद से, अपनी खास ज़रूरतों और इस्तेमाल के उदाहरणों के आधार पर, मॉडल के व्यवहार को कंट्रोल किया जा सकता है.