|
| 1 | +// Copyright 2024 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +// [START generativeaionvertexai_gemini_all_modalities] |
| 16 | +const {VertexAI} = require('@google-cloud/vertexai'); |
| 17 | + |
| 18 | +/** |
| 19 | + * TODO(developer): Update these variables before running the sample. |
| 20 | + */ |
| 21 | +async function analyze_all_modalities(projectId = 'PROJECT_ID') { |
| 22 | + const vertexAI = new VertexAI({project: projectId, location: 'us-central1'}); |
| 23 | + |
| 24 | + const generativeModel = vertexAI.getGenerativeModel({ |
| 25 | + model: 'gemini-1.5-pro-preview-0409', |
| 26 | + }); |
| 27 | + |
| 28 | + const videoFilePart = { |
| 29 | + file_data: { |
| 30 | + file_uri: |
| 31 | + 'gs://cloud-samples-data/generative-ai/video/behind_the_scenes_pixel.mp4', |
| 32 | + mime_type: 'video/mp4', |
| 33 | + }, |
| 34 | + }; |
| 35 | + const imageFilePart = { |
| 36 | + file_data: { |
| 37 | + file_uri: |
| 38 | + 'gs://cloud-samples-data/generative-ai/image/a-man-and-a-dog.png', |
| 39 | + mime_type: 'image/png', |
| 40 | + }, |
| 41 | + }; |
| 42 | + |
| 43 | + const textPart = { |
| 44 | + text: ` |
| 45 | + Watch each frame in the video carefully and answer the questions. |
| 46 | + Only base your answers strictly on what information is available in the video attached. |
| 47 | + Do not make up any information that is not part of the video and do not be too |
| 48 | + verbose, be to the point. |
| 49 | +
|
| 50 | + Questions: |
| 51 | + - When is the moment in the image happening in the video? Provide a timestamp. |
| 52 | + - What is the context of the moment and what does the narrator say about it?`, |
| 53 | + }; |
| 54 | + |
| 55 | + const request = { |
| 56 | + contents: [{role: 'user', parts: [videoFilePart, imageFilePart, textPart]}], |
| 57 | + }; |
| 58 | + |
| 59 | + const resp = await generativeModel.generateContent(request); |
| 60 | + const contentResponse = await resp.response; |
| 61 | + console.log(JSON.stringify(contentResponse)); |
| 62 | +} |
| 63 | +// [END generativeaionvertexai_gemini_all_modalities] |
| 64 | + |
| 65 | +analyze_all_modalities(...process.argv.slice(2)).catch(err => { |
| 66 | + console.error(err.message); |
| 67 | + process.exitCode = 1; |
| 68 | +}); |
0 commit comments