Skip to content

Commit a6cf83f

Browse files
authored
Added a files_create_pdf example. (#263)
Add a Create PDF sample.
1 parent bc65f79 commit a6cf83f

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

samples/files.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,50 @@ async function filesCreateVideo() {
184184
// [END files_create_video]
185185
}
186186

187+
async function filesCreatePDF() {
188+
// [START files_create_pdf]
189+
// Make sure to include these imports:
190+
// import { GoogleGenerativeAI } from "@google/generative-ai";
191+
// import { GoogleAIFileManager } from "@google/generative-ai/server";
192+
193+
// Initialize GoogleGenerativeAI with your API_KEY.
194+
const genAI = new GoogleGenerativeAI(process.env.API_KEY);
195+
// Initialize GoogleAIFileManager with your API_KEY.
196+
const fileManager = new GoogleAIFileManager(process.env.API_KEY);
197+
198+
const model = genAI.getGenerativeModel({
199+
// Choose a Gemini model.
200+
model: "gemini-1.5-flash",
201+
});
202+
203+
// Upload the file and specify a display name.
204+
const uploadResponse = await fileManager.uploadFile(
205+
`${mediaPath}/gemini.pdf`,
206+
{
207+
mimeType: "application/pdf",
208+
displayName: "Gemini 1.5 PDF",
209+
}
210+
);
211+
212+
// View the response.
213+
console.log(
214+
`Uploaded file ${uploadResponse.file.displayName} as: ${uploadResponse.file.uri}`,
215+
);
216+
// Generate content using text and the URI reference for the uploaded file.
217+
const result = await model.generateContent([
218+
{
219+
fileData: {
220+
mimeType: uploadResponse.file.mimeType,
221+
fileUri: uploadResponse.file.uri,
222+
},
223+
},
224+
{ text: "Can you summarize this document as a bulleted list?" },
225+
]);
226+
// Output the generated text to the console
227+
console.log(result.response.text());
228+
// [END files_create_pdf]
229+
}
230+
187231
async function filesList() {
188232
// [START files_list]
189233
// Make sure to include these imports:
@@ -250,6 +294,7 @@ async function runAll() {
250294
await filesCreateAudio();
251295
await filesCreateText();
252296
await filesCreateVideo();
297+
await filesCreatePDF();
253298
await filesList();
254299
await filesGet();
255300
await filesDelete();

samples/media/gemini.pdf

6.89 MB
Binary file not shown.

0 commit comments

Comments
 (0)