|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2025 Google LLC |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | +import {GoogleGenAI} from '@google/genai'; |
| 7 | + |
| 8 | +const GEMINI_API_KEY = process.env.GEMINI_API_KEY; |
| 9 | +const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT; |
| 10 | +const GOOGLE_CLOUD_LOCATION = process.env.GOOGLE_CLOUD_LOCATION; |
| 11 | +const GOOGLE_GENAI_USE_VERTEXAI = process.env.GOOGLE_GENAI_USE_VERTEXAI; |
| 12 | + |
| 13 | +async function delay(ms: number): Promise<void> { |
| 14 | + return new Promise((resolve) => setTimeout(resolve, ms)); |
| 15 | +} |
| 16 | + |
| 17 | +async function generateContentFromMLDev() { |
| 18 | + const ai = new GoogleGenAI({vertexai: false, apiKey: GEMINI_API_KEY}); |
| 19 | + var operation = await ai.models.generateVideos({ |
| 20 | + model: 'veo-2.0-generate-001', |
| 21 | + prompt: 'Man with a dog', |
| 22 | + config: { |
| 23 | + numberOfVideos: 1, |
| 24 | + }, |
| 25 | + }); |
| 26 | + |
| 27 | + while (!operation.done) { |
| 28 | + console.log('Waiting for completion'); |
| 29 | + await delay(1000); |
| 30 | + operation = await ai.operations.get({operation: operation}); |
| 31 | + } |
| 32 | + |
| 33 | + const response = operation.result; |
| 34 | + const fileName = response?.generatedVideos?.[0].video?.uri; |
| 35 | + console.log(fileName); |
| 36 | +} |
| 37 | + |
| 38 | +async function generateContentFromVertexAI() { |
| 39 | + const ai = new GoogleGenAI({ |
| 40 | + vertexai: true, |
| 41 | + project: GOOGLE_CLOUD_PROJECT, |
| 42 | + location: GOOGLE_CLOUD_LOCATION, |
| 43 | + }); |
| 44 | + var operation = await ai.models.generateVideos({ |
| 45 | + model: 'veo-2.0-generate-001', |
| 46 | + prompt: 'Man with a dog', |
| 47 | + }); |
| 48 | + |
| 49 | + while (!operation.done) { |
| 50 | + console.log('Waiting for completion'); |
| 51 | + await delay(1000); |
| 52 | + operation = await ai.operations.get({operation: operation}); |
| 53 | + } |
| 54 | + |
| 55 | + const response = operation.result; |
| 56 | + const fileName = response?.generatedVideos?.[0].video?.uri; |
| 57 | + console.log(fileName); |
| 58 | +} |
| 59 | + |
| 60 | +async function main() { |
| 61 | + if (GOOGLE_GENAI_USE_VERTEXAI) { |
| 62 | + await generateContentFromVertexAI().catch((e) => |
| 63 | + console.error('got error', e), |
| 64 | + ); |
| 65 | + } else { |
| 66 | + await generateContentFromMLDev().catch((e) => |
| 67 | + console.error('got error', e), |
| 68 | + ); |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +main(); |
0 commit comments