Skip to content

Commit 7b75847

Browse files
committed
feat: Add basic multimodal example
1 parent 3b99e39 commit 7b75847

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// [START generativeaionvertexai_non_stream_multimodality_basic]
2+
const {VertexAI} = require('@google-cloud/vertexai');
3+
4+
/**
5+
* TODO(developer): Update these variables before running the sample.
6+
*/
7+
async function generateContent(
8+
projectId = 'PROJECT_ID',
9+
location = 'us-central1',
10+
model = 'gemini-1.5-pro-preview-0409'
11+
) {
12+
// Initialize Vertex AI
13+
const vertexAI = new VertexAI({project: projectId, location: location});
14+
const generativeModel = vertexAI.getGenerativeModel({model: model});
15+
16+
const request = {
17+
contents: [
18+
{
19+
role: 'user',
20+
parts: [
21+
{text: 'Are following video and image correlated?'},
22+
{
23+
file_data: {
24+
file_uri: 'gs://cloud-samples-data/video/animals.mp4',
25+
mime_type: 'video/mp4',
26+
},
27+
},
28+
{
29+
file_data: {
30+
file_uri: 'gs://generativeai-downloads/images/character.jpg',
31+
mime_type: 'video/mp4',
32+
},
33+
},
34+
],
35+
},
36+
],
37+
};
38+
39+
const result = await generativeModel.generateContent(request);
40+
41+
console.log(result.response.candidates[0].content.parts[0].text);
42+
}
43+
// [END generativeaionvertexai_non_stream_multimodality_basic]
44+
45+
generateContent(...process.argv.slice(2)).catch(err => {
46+
console.error(err.message);
47+
process.exitCode = 1;
48+
});
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
const location = process.env.LOCATION;
24+
const model = 'gemini-1.5-pro-preview-0409';
25+
26+
describe('Generative AI Multimodal prompt', () => {
27+
/**
28+
* TODO(developer): Uncomment these variables before running the sample.\
29+
* (Not necessary if passing values as arguments)
30+
*/
31+
// const projectId = 'YOUR_PROJECT_ID';
32+
// const location = 'YOUR_LOCATION';
33+
// const model = 'gemini-1.0-pro';
34+
35+
it('should generate text based on a prompt containing text, a video, and an image', async () => {
36+
const output = execSync(
37+
`node ./inference/nonStreamTextBasic.js ${projectId} ${location} ${model}`
38+
);
39+
40+
// Assert that the correct prompt was issued
41+
assert(output.length > 0);
42+
});
43+
});

0 commit comments

Comments
 (0)