Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a files_create_pdf example. #263

Merged
merged 2 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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?" },
pcoet marked this conversation as resolved.
Show resolved Hide resolved
]);
// 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.
Loading