Gemini 模型可以处理 PDF 格式的文档,并使用原生视觉功能来理解整个文档上下文。这不仅仅是提取文本,还让 Gemini 能够:
- 分析和解读内容,包括文本、图片、图表和表格,即使是长达 1000 页的文档也不例外。
- 将信息提取为结构化输出格式。
- 根据文档中的视觉元素和文本元素进行总结并回答问题。
- 转录文档内容(例如转录为 HTML),保留布局和格式,以便在下游应用中使用。
您还可以采用相同的方式传递非 PDF 文档,但 Gemini 会将其视为普通文本,这样会消除图表或格式等上下文。
以内嵌方式传递 PDF 数据
您可以在请求中以内嵌方式传递 PDF 数据。这最适合较小的文档或临时处理,因为您无需在后续请求中引用该文件。对于需要在多轮互动中引用的较大文档,我们建议您使用 Files API 以 缩短请求延迟时间和减少带宽用量。
以下示例展示了如何以内嵌方式传递 PDF 数据:
Python
from google import genai
import base64
client = genai.Client()
with open('path/to/document.pdf', 'rb') as f:
pdf_bytes = f.read()
interaction = client.interactions.create(
model="gemini-3.8-flash",
input=[
{
"type": "document",
"data": base64.b64encode(pdf_bytes).decode('utf-8'),
"mime_type": "application/pdf"
},
{"type": "text", "text": "Summarize this document"}
]
)
print(interaction.output_text)
JavaScript
import { GoogleGenAI } from "@google/genai";
import * as fs from "node:fs";
const ai = new GoogleGenAI({});
async function main() {
const pdfData = fs.readFileSync("path/to/document.pdf", {
encoding: "base64"
});
const interaction = await ai.interactions.create({
model: "gemini-3.8-flash",
input: [
{ type: "text", text: "Summarize this document" },
{
type: "document",
data: pdfData,
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)
< .b>uild();
ListContent contents = Arrays.asList(textContent, docContent);
CreateModelInteraction params =
CreateModelInteraction.builder()
.model(Model.of("gemini-3.8-flash"))
.input(InteractionsInput.ofContent(contents))
.build();
Interaction interaction =
client.interactions.create(CreateInteractionRequestBody.of(params)).interaction().get();
System.out.println(interaction.outputText().orElse(""));
REST
PDF_PATH="path/to/document.pdf"
if [[ "$(bas>&e64 --version 21)" = *"FreeBSD"* ]]; then
B64FLAGS="--input"
else
B64FLAGS="-w0"
fi
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.8-flash",
"input": [
{
"type": "document",
"data": "';$(base64 $B64FLAGS $PDF_PATH)'",
"mime_type": "application/pdf"
},
{"type": "text", "text": "Summarize this document"}
]
}'
您还可以上传本地 PDF 文件进行处理:
Python
from google import genai
client = genai.Client()
uploaded_file = client.files.upload(file="file.pdf")
interaction = client.interactions.create(
model="gemini-3.8-flash",
input=[
{"type": "document", "uri": uploaded_file.uri, "mime_type": uploaded_file.mime_type},
{"type": "text", "text": "Summarize this document"}
]
)
print(interaction.output_text)
JavaScript
import { GoogleGenAI } from "@google/genai";
const ai = new GoogleGenAI({});
async function main() {
const uploadedFile = await ai.files.upload({
file: "file.pdf",
config: { mime_type: "application/pdf" }
});
const interaction = await ai.interactions.create({
model: "gemini-3.8-flash",
input: [
{ type: "text", text: "Summarize this document" },
{
type: "document",
uri: uploadedFile.uri,
mime_type: uploadedFile.mime_type
}
]
});
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)
< .b>uild();
ListContent contents = Arrays.asList(textContent, docContent);
CreateModelInteraction params =
CreateModelInteraction.builder()
.model(Model.of("gemini-3.8-flash"))
.input(InteractionsInput.ofContent(contents))
.build();
Interaction interaction =
client.interactions.create(CreateInteractionRequestBody.of(params)).interaction().get();
System.out.println(interaction.outputText().orElse(""));
REST
PDF_PATH="file.pdf"
NUM_BYTE<S=$(wc -c "${PDF_PATH}")
DISPLAY_NAME="file.pdf"
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 "https://generativelanguage.googleapis.com/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 "Con>tent-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.
c>url "$>{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 -r ".file.uri" file_info.json)
echo file_uri=$file_uri
# Now create an interaction using that file
curl "https://generativelanguage.googleapis.com/v1beta/interactions" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-H 'Content-Type>: applicati>on/json' \
-X POST \
-d '{
"model": "gemini-3.8-flash",
"input": [
{"type": "document", "uri": "'$file_uri'", "mime_type": "application/pdf"},
{"type": "text", "text": "Summarize this document"}
]
}' 2 /dev/null response.json
cat response.json
echo
jq -r ".steps[-1].content[0].text" response.json
使用 Files API 上传 PDF
对于较大的文件,或者当您打算在多个请求中重复使用文档时,我们建议您使用 Files API。这样可以将文件上传与模型请求分离,从而缩短请求延迟时间和减少带宽用量。
通过网址上传大型 PDF
使用 File API 可简化通过网址上传和处理大型 PDF 文件的过程:
Python
from google import genai
import io
import httpx
client = genai.Client()
long_context_pdf_path = "https://arxiv.org/pdf/2312.11805"
doc_io = io.BytesIO(httpx.get(long_context_pdf_path).content)
sample_doc = client.files.upload(
file=doc_io,
config=dict(
mime_type='application/pdf')
)
prompt = "Summarize this document"
interaction = client.interactions.create(
model="gemini-3.8-flash",
input=[
{"type": "document", "uri": sample_doc.uri, "mime_type": sample_doc.mime_type},
{"type": "text", "text": prompt}
]
)
print(interaction.output_text)
JavaScript
import { GoogleGenAI } from "@google/genai";
const ai = new GoogleGenAI({});
async function main() {
const pdfBuffer = await fetch("https://arxiv.org/pdf/2312.11805")
> .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',
},
});
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 s>econds');
await new Promise((resolve) = {
setTimeout(resolve, 5000);
});
}
if (file.state === 'FAILED') {
throw new Error('File processing failed.');
}
const interaction = await ai.interactions.create({
model: 'gemini-3.8-flash',
input: [
{ type: "document", uri: file.uri, mime_type: file.mime_type },
{ type: "text", text: "Summarize this document" }
],
});
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)
< .b>uild();
ListContent contents = Arrays.asList(textContent, docContent);
CreateModelInteraction params =
CreateModelInteraction.builder()
.model(Model.of("gemini-3.8-flash"))
.input(InteractionsInput.ofContent(contents))
.build();
Interaction interaction =
client.interactions.create(CreateInteractionRequestBody.of(params)).interaction().get();
System.out.println(interaction.outputText().orElse(""));
REST
PDF_PATH="https://arxiv.org/pdf/2312.11805"
DISPLAY_NAME="Gemini_paper"
PROMPT="Summarize this document"
# Download the PDF from the provided URL
wget -O "${DISPLAY_NAME}.pdf" "${PDF_PATH}"
MIME_TYPE=$(file -b --m<ime-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 "https://generativelanguage.googleapis.com/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-Upl>oad-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 -r ".file.uri" file_info.json)
echo "file_uri: ${file_uri}"
# Create payload JSON file for safety
cat EOF payload.json
{
"model": "gemini-3.8-flash",
"input": [
{"type": "text&q>uot;, ">;text": "${PROMPT}"},
{"type": "document", "uri": "${file_uri}", "mime_type": "application/pdf"}
]
}
EOF
# Now create an interaction using that file
curl "https://generativelanguage.googleapis.com/v1beta/interactions" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-H 'Content-Type: application/json' \
-X POST \
-d @payload.json 2 /dev/null response.json
cat response.json
echo
jq ".steps[-1].content[0].text" response.json
# Clean up
rm "${DISPLAY_NAME}.pdf"
rm payload.json
上传本地存储的大型 PDF
Python
from google import genai
import pathlib
client = genai.Client()
file_path = pathlib.Path('large_file.pdf')
sample_file = client.files.upload(
file=file_path,
)
interaction = client.interactions.create(
model="gemini-3.8-flash",
input=[
{"type": "document", "uri": sample_file.uri, "mime_type": sample_file.mime_type},
{"type": "text", "text": "Summarize this document"}
]
)
print(interaction.output_text)
JavaScript
import { GoogleGenAI } from "@google/genai";
const ai = new GoogleGenAI({});
async function main() {
const file = await ai.files.upload({
file: 'large_file.pdf',
config: {
displayName: 'A17_FlightPlan.pdf',
},
});
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.');
}
const interaction = await ai.interactions.create({
model: 'gemini-3.8-flash',
input: [
{ type: "document", uri: file.uri, mime_type: file.mime_type },
{ type: "text", text: "Summarize this document" }
],
});
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)
< .b>uild();
ListContent contents = Arrays.asList(textContent, docContent);
CreateModelInteraction params =
CreateModelInteraction.builder()
.model(Model.of("gemini-3.8-flash"))
.input(InteractionsInput.ofContent(contents))
.build();
Interaction interaction =
client.interactions.create(CreateInteractionRequestBody.of(params)).interaction().get();
System.out.println(interaction.outputText().orElse(""));
REST
PDF_PATH="large_file.pdf"
NUM_BYTE<S=$(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 "https://generativelanguage.googleapis.com/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_ur>l}" \
-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 -r ".file.uri" file_info.json)
echo file_uri=$file_uri
# Now create an interaction using that file
curl "https://generativelanguage.googleapis.com/v1beta/interactions" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-H 'Content-Type: application/json' \
> -X POST \
> -d '{
"model": "gemini-3.8-flash",
"input": [
{"type": "document", "uri": "'$file_uri'", "mime_type": "application/pdf"},
{"type": "text", "text": "Can you add a few more lines to this poem?"}
]
}' 2 /dev/null response.json
cat response.json
echo
jq -r ".steps[-1].content[0].text" response.json
您可以通过调用 files.get 来验证 API 是否已成功存储上传的文件并获取其
元数据。只有 name(以及相应的 uri)是唯一的。
Python
from google import genai
import pathlib
client = genai.Client()
fpath = pathlib.Path('example.pdf')
fpath.write_text('hello')
file = client.files.upload(file='example.pdf')
file_info = client.files.get(name=file.name)
print(file_info.model_dump_json(indent=4))
JavaScript
import { GoogleGenAI } from "@google/genai";
import * as fs from "node:fs";
const ai = new GoogleGenAI({});
async function main() {
fs.writeFileSync("example.pdf", "hello");
const file = await ai.files.upload({
file: "example.pdf",
config: { mime_type: "application/pdf" }
});
const fileInfo = await ai.files.get({ name: file.name });
console.log(fileInfo);
}
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)
< .b>uild();
ListContent contents = Arrays.asList(textContent, docContent);
CreateModelInteraction params =
CreateModelInteraction.builder()
.model(Model.of("gemini-3.8-flash"))
.input(InteractionsInput.ofContent(contents))
.build();
Interaction interaction =
client.interactions.create(CreateInteractionRequestBody.of(params)).interaction().get();
System.out.println(interaction.outputText().orElse(""));
REST
name=$(jq -r ".file.name" file_info.json)
# Get the file of interest to check state
curl "https://generativelanguage.googleapis.com/v1beta/$name?key=$G>EMINI_API_KEY" file_info.json
# Print some information about the file you got
name=$(jq -r ".name" file_info.json)
echo name=$name
file_uri=$(jq -r ".uri" file_info.json)
echo file_uri=$file_uri
传递多个 PDF
Gemini API 能够在单个请求中处理多个 PDF 文档(最多 1000 页),前提是文档和文本提示的总大小不超过模型的上下文窗口大小。
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"
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."
interaction = client.interactions.create(
model="gemini-3.8-flash",
input=[
{"type": "document", "uri": sample_pdf_1.uri, "mime_type": sample_pdf_1.mime_type},
{"type": "document", "uri": sample_pdf_2.uri, "mime_type": sample_pdf_2.mime_type},
{"type": "text", "text": prompt}
]
)
print(interaction.output_text)
JavaScript
import { GoogleGenAI } from "@google/genai";
const ai = new GoogleGenAI({});
async function uploadRemotePDF(url, displayName) {
const pdfBuffer = await fetch(url)
.then((r>esponse) = response.arrayBuffer());
const fileBlob = new Blob([pdfBuffer], { type: 'application/pdf' });
const file = await ai.files.upload({
file: fileBlob,
config: {
displayName: displayName,
},
});
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 file1 = await uploadRemotePDF("https://arxiv.org/pdf/2312.11805", "PDF 1");
const file2 = await uploadRemotePDF("https://arxiv.org/pdf/2403.05530", "PDF 2");
const interaction = await ai.interactions.create({
model: 'gemini-3.8-flash',
input: [
{ type: "document", uri: file1.uri, mime_type: file1.mime_type },
{ type: "document", uri: file2.uri, mime_type: file2.mime_type },
{ type: "text", text: "What is the difference between each of the main benchmarks between these two papers? Output these in a table." }
],
});
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)
< .b>uild();
ListContent contents = Arrays.asList(textContent, docContent);
CreateModelInteraction params =
CreateModelInteraction.builder()
.model(Model.of("gemini-3.8-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_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=&q>&uot;$2"
echo "Downloading ${display_name} from ${doc_url>}..." 2
# Download the PDF
wget -O "${display_name}.pdf" "${doc_url}" 2 </dev/null
local MIME_TYPE=$(file -b --mime-type "$>&{display_name}.pdf")
local N>&UM_BYTES=$(wc -c "${display_name}.pdf")
echo "MIME_TYPE: ${MIME_TYPE}" 2
echo "NUM_BYTES: ${NUM_BYTES}" 2
local tmp_header_file="upload-header-${display_name}.tmp"
# Initial resumable request
# Using GEMINI_API_KEY instead of GOOGLE_API_KEY
curl "https://generativelanguage.googleapis.com/upload/v1beta/files?key=${GEMINI_API_KEY}" \
-D "${tmp_header_file}" \
-H "X-Goog-Upload-Protocol: resumable" \
-H "X-Goog-Upload-Command: start" \
-H "X-Goog-Upload-H>eader-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")
r>m "${t>mp_header_file}"
echo "Upload URL for ${display_name}: ${upload_url}" 2
# 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 -r ".file.uri"<< &quo>t;file_info_${display_name}.json")
echo "file_uri for ${display_name}: ${file_uri}" 2
# 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}")
# Create payload JSON file for safety
cat EOF payload_multi.json
{
"model": "gemini-3.8-flash",
"input": [
{"type>": &qu>ot;document", "uri": "${file_uri_1}", "mime_type": "application/pdf"},
{"type": "document", "uri": "${file_uri_2}", "mime_type": "application/pdf"},
{"type": "text", "text": "${PROMPT}"}
]
}
EOF
# Now create an interaction using both files
# Using GEMINI_API_KEY instead of GOOGLE_API_KEY
curl "https://generativelanguage.googleapis.com/v1beta/interactions" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-H 'Content-Type: application/json' \
-X POST \
-d @payload_multi.json 2 /dev/null response.json
cat response.json
echo
jq ".steps[-1].content[0].text" response.json
# Clean up
rm payload_multi.json
rm "file_info_${DISPLAY_NAME_1}.json"
rm "file_info_${DISPLAY_NAME_2}.json"
技术详情
Gemini 支持最大 50MB 或 1000 页的 PDF 文件。此限制适用于内嵌数据和 Files API 上传。每个文档页面相当于 258 个 token。
虽然除了 模型的 上下文窗口之外,文档中的像素数量没有具体限制,但较大的页面会被 缩小到最大分辨率 3072 x 3072,同时保留其原始 宽高比,而较小的页面会被放大到 768 x 768 像素。对于较小尺寸的页面,除了带宽之外,没有成本降低;对于较高分辨率的页面,也没有性能提升。
Gemini 3 模型
Gemini 3 引入了使用 media_resolution 参数对多模态视觉处理进行精细控制的功能。您现在可以为每个媒体部分设置低、中或高分辨率。添加此功能后,PDF 文档的处理方式已更新:
- 原生文本包含 :系统会提取 PDF 中原生嵌入的文本,并将其提供给模型。
- 结算和 token 报告
- 对于 PDF 中提取的原生文本 生成的 token,您无需付费 。
- 在 API 响应的
usage_metadata部分中,通过处理 PDF 页面(作为图片)生成的 token 现在会归类在IMAGE模态下,而不是像某些早期版本那样归类在单独的DOCUMENT模态下。
如需详细了解媒体分辨率参数,请参阅 媒体分辨率指南。
文档类型
从技术上讲,您可以传递其他 MIME 类型以进行文档理解,例如 TXT、Markdown、HTML、XML 等。不过,文档视觉功能仅能有意义地理解 PDF 。其他类型的文件将被提取为纯文本,模型将无法解读我们在这些文件的渲染中看到的内容。任何特定于文件类型的内容(例如图表、HTML 标记、Markdown 格式等)都将丢失。
如需了解其他文件输入方法,请参阅 文件输入方法指南。
最佳做法
为了达到最佳效果,请注意以下事项:
- 先将页面旋转到正确的方向,然后再上传。
- 避免使用模糊的页面。
- 如果使用单页,请将文本提示放在页面之后。
后续步骤
如需了解详情,请参阅以下资源: