Skip to content

Commit f5b6c48

Browse files
committed
feat: Add streaming inference example
1 parent a569ee4 commit f5b6c48

File tree

4 files changed

+102
-4
lines changed

4 files changed

+102
-4
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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_text_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.0-pro'
25+
) {
26+
// Initialize Vertex with your Cloud project and location
27+
const vertexAI = new VertexAI({project: projectId, location: location});
28+
29+
// Instantiate the model
30+
const generativeModel = vertexAI.getGenerativeModel({
31+
model: model,
32+
});
33+
34+
const request = {
35+
contents: [
36+
{
37+
role: 'user',
38+
parts: [
39+
{
40+
text: 'Write a story about a magic backpack.',
41+
},
42+
],
43+
},
44+
],
45+
};
46+
47+
console.log(JSON.stringify(request));
48+
49+
const result = await generativeModel.generateContentStream(request);
50+
for await (const item of result.stream) {
51+
console.log(item.candidates[0].content.parts[0].text);
52+
}
53+
}
54+
// [END generativeaionvertexai_stream_text_basic]
55+
56+
generateContent(...process.argv.slice(2)).catch(err => {
57+
console.error(err.message);
58+
process.exitCode = 1;
59+
});

generative-ai/snippets/test/inference/nonStreamMultiModalityBasic.test.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,8 @@ describe('Generative AI Multimodal prompt', () => {
3434

3535
it('should generate text based on a prompt containing text, a video, and an image', async () => {
3636
const output = execSync(
37-
`node ./inference/nonStreamTextBasic.js ${projectId} ${location} ${model}`
37+
`node ./inference/nonStreamMultiModalityBasic.js ${projectId} ${location} ${model}`
3838
);
39-
40-
// Assert that the correct prompt was issued
4139
assert(output.length > 0);
4240
});
4341
});

generative-ai/snippets/test/inference/nonStreamTextBasic.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ describe('Generative AI Basic Text Inference', () => {
3232
// const location = 'YOUR_LOCATION';
3333
// const model = 'gemini-1.0-pro';
3434

35-
it('should create a generateive text model and infer text from a prompt', async () => {
35+
it('should create a generative text model and infer text from a prompt', async () => {
3636
const output = execSync(
3737
`node ./inference/nonStreamTextBasic.js ${projectId} ${location} ${model}`
3838
);
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/streamTextBasic.js ${projectId} ${location} ${model}`
38+
);
39+
assert(output.length > 0);
40+
});
41+
});

0 commit comments

Comments
 (0)