Skip to content

Commit

Permalink
Added a files_create_pdf example. (#263)
Browse files Browse the repository at this point in the history
Add a Create PDF sample.
  • Loading branch information
pcoet authored Sep 30, 2024
1 parent bc65f79 commit a6cf83f
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions samples/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -250,6 +294,7 @@ async function runAll() {
await filesCreateAudio();
await filesCreateText();
await filesCreateVideo();
await filesCreatePDF();
await filesList();
await filesGet();
await filesDelete();
Expand Down
Binary file added samples/media/gemini.pdf
Binary file not shown.

0 comments on commit a6cf83f

Please sign in to comment.