Skip to content

Commit

Permalink
Add canonical File API samples (#204)
Browse files Browse the repository at this point in the history
  • Loading branch information
hsubox76 authored Jul 10, 2024
1 parent e6bb974 commit 812037d
Show file tree
Hide file tree
Showing 7 changed files with 29,050 additions and 6 deletions.
4 changes: 2 additions & 2 deletions samples/node/controlled_generation.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ async function jsonNoSchema() {
// [END json_no_schema]
}

async function run() {
async function runAll() {
// Comment out or delete any sample cases you don't want to run.
await jsonControlledGeneration();
await jsonNoSchema();
}

run();
runAll();
4 changes: 2 additions & 2 deletions samples/node/count_tokens.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ async function tokensTools() {
// [END tokens_tools]
}

async function run() {
async function runAll() {
// Comment out or delete any sample cases you don't want to run.
await tokensTextOnly();
await tokensChat();
Expand All @@ -312,4 +312,4 @@ async function run() {
await tokensTools();
}

run();
runAll();
240 changes: 240 additions & 0 deletions samples/node/files.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,240 @@
/**
* @license
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { GoogleGenerativeAI } from "@google/generative-ai";
import { GoogleAIFileManager, FileState } from "@google/generative-ai/server";
import { dirname } from "path";
import { fileURLToPath } from "url";

const __dirname = dirname(fileURLToPath(import.meta.url));
const mediaPath = __dirname + "/media";

async function filesCreateImage() {
// [START files_create_image]
const fileManager = new GoogleAIFileManager(process.env.API_KEY);

const uploadResult = await fileManager.uploadFile(
`${mediaPath}/jetpack.jpg`,
{
mimeType: "image/jpeg",
displayName: "Jetpack drawing",
},
);
// View the response.
console.log(
`Uploaded file ${uploadResult.file.displayName} as: ${uploadResult.file.uri}`,
);

const genAI = new GoogleGenerativeAI(process.env.API_KEY);
const model = genAI.getGenerativeModel({ model: "gemini-1.5-flash" });
const result = await model.generateContent([
"Tell me about this image.",
{
fileData: {
fileUri: uploadResult.file.uri,
mimeType: uploadResult.file.mimeType,
},
},
]);
console.log(result.response.text());
// [END files_create_image]
}

async function filesCreateAudio() {
// [START files_create_audio]
const fileManager = new GoogleAIFileManager(process.env.API_KEY);

const uploadResult = await fileManager.uploadFile(
`${mediaPath}/samplesmall.mp3`,
{
mimeType: "audio/mp3",
displayName: "Audio sample",
},
);

let file = await fileManager.getFile(uploadResult.file.name);
while (file.state === FileState.PROCESSING) {
process.stdout.write(".");
// Sleep for 10 seconds
await new Promise((resolve) => setTimeout(resolve, 10_000));
// Fetch the file from the API again
file = await fileManager.getFile(uploadResult.file.name);
}

if (file.state === FileState.FAILED) {
throw new Error("Audio processing failed.");
}

// View the response.
console.log(
`Uploaded file ${uploadResult.file.displayName} as: ${uploadResult.file.uri}`,
);

const genAI = new GoogleGenerativeAI(process.env.API_KEY);
const model = genAI.getGenerativeModel({ model: "gemini-1.5-flash" });
const result = await model.generateContent([
"Tell me about this audio clip.",
{
fileData: {
fileUri: uploadResult.file.uri,
mimeType: uploadResult.file.mimeType,
},
},
]);
console.log(result.response.text());
// [END files_create_audio]
}

async function filesCreateText() {
// [START files_create_text]
const fileManager = new GoogleAIFileManager(process.env.API_KEY);

const uploadResult = await fileManager.uploadFile(`${mediaPath}/a11.txt`, {
mimeType: "text/plain",
displayName: "Apollo 11",
});
// View the response.
console.log(
`Uploaded file ${uploadResult.file.displayName} as: ${uploadResult.file.uri}`,
);

const genAI = new GoogleGenerativeAI(process.env.API_KEY);
const model = genAI.getGenerativeModel({ model: "gemini-1.5-flash" });
const result = await model.generateContent([
"Transcribe the first few sentences of this document.",
{
fileData: {
fileUri: uploadResult.file.uri,
mimeType: uploadResult.file.mimeType,
},
},
]);
console.log(result.response.text());
// [END files_create_text]
}

async function filesCreateVideo() {
// [START files_create_video]
const fileManager = new GoogleAIFileManager(process.env.API_KEY);

const uploadResult = await fileManager.uploadFile(
`${mediaPath}/Big_Buck_Bunny.mp4`,
{
mimeType: "video/mp4",
displayName: "Big Buck Bunny",
},
);

let file = await fileManager.getFile(uploadResult.file.name);
while (file.state === FileState.PROCESSING) {
process.stdout.write(".");
// Sleep for 10 seconds
await new Promise((resolve) => setTimeout(resolve, 10_000));
// Fetch the file from the API again
file = await fileManager.getFile(uploadResult.file.name);
}

if (file.state === FileState.FAILED) {
throw new Error("Video processing failed.");
}

// View the response.
console.log(
`Uploaded file ${uploadResult.file.displayName} as: ${uploadResult.file.uri}`,
);

const genAI = new GoogleGenerativeAI(process.env.API_KEY);
const model = genAI.getGenerativeModel({ model: "gemini-1.5-flash" });
const result = await model.generateContent([
"Tell me about this video.",
{
fileData: {
fileUri: uploadResult.file.uri,
mimeType: uploadResult.file.mimeType,
},
},
]);
console.log(result.response.text());
// [END files_create_video]
}

async function filesList() {
// [START files_list]
const fileManager = new GoogleAIFileManager(process.env.API_KEY);

const listFilesResponse = await fileManager.listFiles();

// View the response.
for (const file of listFilesResponse.files) {
console.log(`name: ${file.name} | display name: ${file.displayName}`);
}
// [END files_list]
}

async function filesGet() {
// [START files_get]
const fileManager = new GoogleAIFileManager(process.env.API_KEY);

const uploadResponse = await fileManager.uploadFile(
`${mediaPath}/jetpack.jpg`,
{
mimeType: "image/jpeg",
displayName: "Jetpack drawing",
},
);

// Get the previously uploaded file's metadata.
const getResponse = await fileManager.getFile(uploadResponse.file.name);

// View the response.
console.log(
`Retrieved file ${getResponse.displayName} as ${getResponse.uri}`,
);
// [END files_get]
}

async function filesDelete() {
// [START files_delete]
const fileManager = new GoogleAIFileManager(process.env.API_KEY);

const uploadResult = await fileManager.uploadFile(
`${mediaPath}/jetpack.jpg`,
{
mimeType: "image/jpeg",
displayName: "Jetpack drawing",
},
);

// Delete the file.
await fileManager.deleteFile(uploadResult.file.name);

console.log(`Deleted ${uploadResult.file.displayName}`);
// [END files_delete]
}

async function runAll() {
// Comment out or delete any sample cases you don't want to run.
await filesCreateImage();
await filesCreateAudio();
await filesCreateText();
await filesCreateVideo();
await filesList();
await filesGet();
await filesDelete();
}

runAll();
Loading

0 comments on commit 812037d

Please sign in to comment.