Skip to content

Commit

Permalink
Add files samples
Browse files Browse the repository at this point in the history
  • Loading branch information
hsubox76 committed Jul 9, 2024
1 parent 0e1a1f0 commit 0b157e5
Show file tree
Hide file tree
Showing 4 changed files with 28,901 additions and 35 deletions.
14 changes: 7 additions & 7 deletions samples/node/count_tokens.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,14 +302,14 @@ async function tokensTools() {

async function runAll() {
// Comment out or delete any sample cases you don't want to run.
// await tokensTextOnly();
// await tokensChat();
// await tokensMultimodalImageInline();
// await tokensMultimodalImageFileApi();
// await tokensMultimodalVideoAudioFileApi();
await tokensTextOnly();
await tokensChat();
await tokensMultimodalImageInline();
await tokensMultimodalImageFileApi();
await tokensMultimodalVideoAudioFileApi();
await tokensCachedContent();
// await tokensSystemInstruction();
// await tokensTools();
await tokensSystemInstruction();
await tokensTools();
}

runAll();
188 changes: 160 additions & 28 deletions samples/node/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,42 +15,174 @@
* 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 genAI = new GoogleGenerativeAI(process.env.API_KEY);
const model = genAI.getGenerativeModel({
model: "gemini-1.5-flash",
safetySettings: [
{
category: HarmCategory.HARM_CATEGORY_HARASSMENT,
threshold: HarmBlockThreshold.BLOCK_LOW_AND_ABOVE,
},
],
});

const unsafePrompt =
"I support Martians Soccer Club and I think " +
"Jupiterians Football Club sucks! Write an ironic phrase telling " +
"them how I feel about them.";

const result = await model.generateContent(unsafePrompt);

try {
result.response.text();
} catch (e) {
console.error(e);
console.log(result.response.candidates[0].safetyRatings);
}
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}`,
);
// [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}`,
);
// [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}`,
);
// [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}`,
);
// [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 safetySettings();
await filesCreateImage();
await filesCreateAudio();
await filesCreateText();
await filesCreateVideo();
await filesList();
await filesGet();
await filesDelete();
}

runAll();
Loading

0 comments on commit 0b157e5

Please sign in to comment.