Skip to content

Commit 3a2c53d

Browse files
committed
feat: Add example for multimodal streaming response
1 parent f5b6c48 commit 3a2c53d

File tree

3 files changed

+106
-1
lines changed

3 files changed

+106
-1
lines changed

generative-ai/snippets/inference/nonStreamMultiModalityBasic.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ async function generateContent(
4242
{
4343
file_data: {
4444
file_uri: 'gs://generativeai-downloads/images/character.jpg',
45-
mime_type: 'video/mp4',
45+
mime_type: 'image/jpeg',
4646
},
4747
},
4848
],
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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_stream_multimodality_basic]
16+
const {VertexAI} = require('@google-cloud/vertexai');
17+
18+
/**
19+
* TODO(developer): Update these variables before running the sample.
20+
*/
21+
async function generateContent(
22+
projectId = 'PROJECT_ID',
23+
location = 'us-central1',
24+
model = 'gemini-1.5-pro-preview-0409'
25+
) {
26+
// Initialize Vertex AI
27+
const vertexAI = new VertexAI({project: projectId, location: location});
28+
const generativeModel = vertexAI.getGenerativeModel({model: model});
29+
30+
const request = {
31+
contents: [
32+
{
33+
role: 'user',
34+
parts: [
35+
{text: 'Are following video and image correlated?'},
36+
{
37+
file_data: {
38+
file_uri: 'gs://cloud-samples-data/video/animals.mp4',
39+
mime_type: 'video/mp4',
40+
},
41+
},
42+
{
43+
file_data: {
44+
file_uri: 'gs://generativeai-downloads/images/character.jpg',
45+
mime_type: 'image/jpeg',
46+
},
47+
},
48+
],
49+
},
50+
],
51+
};
52+
53+
const result = await generativeModel.generateContentStream(request);
54+
55+
for await (const item of result.stream) {
56+
console.log(item.candidates[0].content.parts[0].text);
57+
}
58+
}
59+
// [END generativeaionvertexai_stream_multimodality_basic]
60+
61+
generateContent(...process.argv.slice(2)).catch(err => {
62+
console.error(err.message);
63+
process.exitCode = 1;
64+
});
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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.0-pro';
25+
26+
describe('Generative AI Basic Text Inference Streaming', () => {
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 create a generative text model and infer text from a prompt, streaming the results', async () => {
36+
const output = execSync(
37+
`node ./inference/streamMultiModalityBasic.js ${projectId} ${location} ${model}`
38+
);
39+
assert(output.length > 0);
40+
});
41+
});

0 commit comments

Comments
 (0)