Skip to content

Commit fd87aa9

Browse files
gericdongpattishin
andauthored
feat(generative-ai): Add all modalities sample for Gemini 1.5 (#3664)
Co-authored-by: Patti Shin <[email protected]>
1 parent 6383448 commit fd87aa9

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
'use strict';
16+
17+
const {assert} = require('chai');
18+
const {describe, it} = require('mocha');
19+
const cp = require('child_process');
20+
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
21+
22+
const projectId = process.env.CAIP_PROJECT_ID;
23+
24+
describe('Process all modalities', async () => {
25+
it('should process all modalities', async () => {
26+
const output = execSync(`node ./gemini-all-modalities.js ${projectId}`);
27+
28+
assert(output.length > 0);
29+
});
30+
});

0 commit comments

Comments
 (0)