diff --git a/samples/files.js b/samples/files.js index 71a899e7..22ed271c 100644 --- a/samples/files.js +++ b/samples/files.js @@ -184,6 +184,50 @@ async function filesCreateVideo() { // [END files_create_video] } +async function filesCreatePDF() { + // [START files_create_pdf] + // Make sure to include these imports: + // import { GoogleGenerativeAI } from "@google/generative-ai"; + // import { GoogleAIFileManager } from "@google/generative-ai/server"; + + // Initialize GoogleGenerativeAI with your API_KEY. + const genAI = new GoogleGenerativeAI(process.env.API_KEY); + // Initialize GoogleAIFileManager with your API_KEY. + const fileManager = new GoogleAIFileManager(process.env.API_KEY); + + const model = genAI.getGenerativeModel({ + // Choose a Gemini model. + model: "gemini-1.5-flash", + }); + + // Upload the file and specify a display name. + const uploadResponse = await fileManager.uploadFile( + `${mediaPath}/gemini.pdf`, + { + mimeType: "application/pdf", + displayName: "Gemini 1.5 PDF", + } + ); + + // View the response. + console.log( + `Uploaded file ${uploadResponse.file.displayName} as: ${uploadResponse.file.uri}`, + ); + // Generate content using text and the URI reference for the uploaded file. + const result = await model.generateContent([ + { + fileData: { + mimeType: uploadResponse.file.mimeType, + fileUri: uploadResponse.file.uri, + }, + }, + { text: "Can you summarize this document as a bulleted list?" }, + ]); + // Output the generated text to the console + console.log(result.response.text()); + // [END files_create_pdf] +} + async function filesList() { // [START files_list] // Make sure to include these imports: @@ -250,6 +294,7 @@ async function runAll() { await filesCreateAudio(); await filesCreateText(); await filesCreateVideo(); + await filesCreatePDF(); await filesList(); await filesGet(); await filesDelete(); diff --git a/samples/media/gemini.pdf b/samples/media/gemini.pdf new file mode 100644 index 00000000..fe8caa8d Binary files /dev/null and b/samples/media/gemini.pdf differ