Skip to content

Commit 26802e4

Browse files
committed
feat(generative-ai): migrate text and multimodal inference samples to @google/genai
1 parent bc5bfdd commit 26802e4

8 files changed

Lines changed: 106 additions & 75 deletions

generative-ai/snippets/inference/nonStreamMultiModalityBasic.js

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,51 +13,52 @@
1313
// limitations under the License.
1414

1515
// [START generativeaionvertexai_non_stream_multimodality_basic]
16-
const {VertexAI} = require('@google-cloud/vertexai');
17-
16+
const {GoogleGenAI} = require('@google/genai');
1817
/**
1918
* TODO(developer): Update these variables before running the sample.
2019
*/
21-
const PROJECT_ID = process.env.CAIP_PROJECT_ID;
22-
const LOCATION = 'us-central1';
23-
const MODEL = 'gemini-2.0-flash-001';
24-
25-
async function generateContent() {
26-
// Initialize Vertex AI
27-
const vertexAI = new VertexAI({project: PROJECT_ID, location: LOCATION});
28-
const generativeModel = vertexAI.getGenerativeModel({model: MODEL});
20+
async function generateContent(
21+
projectId,
22+
location = 'us-central1',
23+
model = 'gemini-2.0-flash-001'
24+
) {
25+
// Initialize client
26+
const client = new GoogleGenAI({
27+
vertexai: true,
28+
project: projectId,
29+
location: location,
30+
});
2931

30-
const request = {
32+
const result = await client.models.generateContent({
33+
model: model,
3134
contents: [
3235
{
3336
role: 'user',
3437
parts: [
3538
{
36-
file_data: {
37-
file_uri: 'gs://cloud-samples-data/video/animals.mp4',
38-
mime_type: 'video/mp4',
39+
fileData: {
40+
fileUri: 'gs://cloud-samples-data/video/animals.mp4',
41+
mimeType: 'video/mp4',
3942
},
4043
},
4144
{
42-
file_data: {
43-
file_uri:
45+
fileData: {
46+
fileUri:
4447
'gs://cloud-samples-data/generative-ai/image/character.jpg',
45-
mime_type: 'image/jpeg',
48+
mimeType: 'image/jpeg',
4649
},
4750
},
4851
{text: 'Are this video and image correlated?'},
4952
],
5053
},
5154
],
52-
};
53-
54-
const result = await generativeModel.generateContent(request);
55+
});
5556

56-
console.log(result.response.candidates[0].content.parts[0].text);
57+
console.log(result.text);
5758
}
5859
// [END generativeaionvertexai_non_stream_multimodality_basic]
5960

60-
generateContent().catch(err => {
61+
generateContent(...process.argv.slice(2)).catch(err => {
6162
console.error(err.message);
6263
process.exitCode = 1;
6364
});

generative-ai/snippets/inference/nonStreamTextBasic.js

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,25 @@
1313
// limitations under the License.
1414

1515
// [START generativeaionvertexai_non_stream_text_basic]
16-
const {VertexAI} = require('@google-cloud/vertexai');
17-
16+
const {GoogleGenAI} = require('@google/genai');
1817
/**
1918
* TODO(developer): Update these variables before running the sample.
2019
*/
21-
const PROJECT_ID = process.env.CAIP_PROJECT_ID;
22-
const LOCATION = process.env.LOCATION;
23-
const MODEL = 'gemini-2.0-flash-001';
24-
25-
async function generateContent() {
26-
// Initialize Vertex with your Cloud project and location
27-
const vertexAI = new VertexAI({project: PROJECT_ID, location: LOCATION});
2820

29-
// Instantiate the model
30-
const generativeModel = vertexAI.getGenerativeModel({
31-
model: MODEL,
21+
async function generateContent(
22+
projectId,
23+
location = 'us-central1',
24+
model = 'gemini-2.0-flash-001'
25+
) {
26+
// Initialize client with your Cloud project and location
27+
const client = new GoogleGenAI({
28+
vertexai: true,
29+
project: projectId,
30+
location: location,
3231
});
3332

3433
const request = {
34+
model: model,
3535
contents: [
3636
{
3737
role: 'user',
@@ -46,13 +46,13 @@ async function generateContent() {
4646

4747
console.log(JSON.stringify(request));
4848

49-
const result = await generativeModel.generateContent(request);
49+
const response = await client.models.generateContent(request);
5050

51-
console.log(result.response.candidates[0].content.parts[0].text);
51+
console.log(response.text);
5252
}
5353
// [END generativeaionvertexai_non_stream_text_basic]
5454

55-
generateContent().catch(err => {
55+
generateContent(...process.argv.slice(2)).catch(err => {
5656
console.error(err.message);
5757
process.exitCode = 1;
5858
});

generative-ai/snippets/inference/streamMultiModalityBasic.js

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,36 +13,41 @@
1313
// limitations under the License.
1414

1515
// [START generativeaionvertexai_stream_multimodality_basic]
16-
const {VertexAI} = require('@google-cloud/vertexai');
16+
const {GoogleGenAI} = require('@google/genai');
1717

1818
/**
1919
* TODO(developer): Update these variables before running the sample.
2020
*/
21-
const PROJECT_ID = process.env.CAIP_PROJECT_ID;
22-
const LOCATION = process.env.LOCATION;
23-
const MODEL = 'gemini-2.0-flash-001';
2421

25-
async function generateContent() {
26-
// Initialize Vertex AI
27-
const vertexAI = new VertexAI({project: PROJECT_ID, location: LOCATION});
28-
const generativeModel = vertexAI.getGenerativeModel({model: MODEL});
22+
async function generateContent(
23+
projectId,
24+
location = 'us-central1',
25+
model = 'gemini-2.0-flash-001'
26+
) {
27+
// Initialize client
28+
const client = new GoogleGenAI({
29+
vertexai: true,
30+
project: projectId,
31+
location: location,
32+
});
2933

3034
const request = {
35+
model: model,
3136
contents: [
3237
{
3338
role: 'user',
3439
parts: [
3540
{
36-
file_data: {
37-
file_uri: 'gs://cloud-samples-data/video/animals.mp4',
38-
mime_type: 'video/mp4',
41+
fileData: {
42+
fileUri: 'gs://cloud-samples-data/video/animals.mp4',
43+
mimeType: 'video/mp4',
3944
},
4045
},
4146
{
42-
file_data: {
43-
file_uri:
47+
fileData: {
48+
fileUri:
4449
'gs://cloud-samples-data/generative-ai/image/character.jpg',
45-
mime_type: 'image/jpeg',
50+
mimeType: 'image/jpeg',
4651
},
4752
},
4853
{text: 'Are this video and image correlated?'},
@@ -51,15 +56,15 @@ async function generateContent() {
5156
],
5257
};
5358

54-
const result = await generativeModel.generateContentStream(request);
59+
const responseStream = await client.models.generateContentStream(request);
5560

56-
for await (const item of result.stream) {
57-
console.log(item.candidates[0].content.parts[0].text);
61+
for await (const chunk of responseStream) {
62+
console.log(chunk.text);
5863
}
5964
}
6065
// [END generativeaionvertexai_stream_multimodality_basic]
6166

62-
generateContent().catch(err => {
67+
generateContent(...process.argv.slice(2)).catch(err => {
6368
console.error(err.message);
6469
process.exitCode = 1;
6570
});

generative-ai/snippets/inference/streamTextBasic.js

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,26 @@
1313
// limitations under the License.
1414

1515
// [START generativeaionvertexai_stream_text_basic]
16-
const {VertexAI} = require('@google-cloud/vertexai');
16+
const {GoogleGenAI} = require('@google/genai');
1717

1818
/**
1919
* TODO(developer): Update these variables before running the sample.
2020
*/
21-
const PROJECT_ID = process.env.CAIP_PROJECT_ID;
22-
const LOCATION = process.env.LOCATION;
23-
const MODEL = 'gemini-2.0-flash-001';
2421

25-
async function generateContent() {
26-
// Initialize Vertex with your Cloud project and location
27-
const vertexAI = new VertexAI({project: PROJECT_ID, location: LOCATION});
28-
29-
// Instantiate the model
30-
const generativeModel = vertexAI.getGenerativeModel({
31-
model: MODEL,
22+
async function generateContent(
23+
projectId,
24+
location = 'us-central1',
25+
model = 'gemini-2.0-flash-001'
26+
) {
27+
// Initialize client with your Cloud project and location
28+
const client = new GoogleGenAI({
29+
vertexai: true,
30+
project: projectId,
31+
location: location,
3232
});
3333

3434
const request = {
35+
model: model,
3536
contents: [
3637
{
3738
role: 'user',
@@ -43,17 +44,17 @@ async function generateContent() {
4344
},
4445
],
4546
};
46-
4747
console.log(JSON.stringify(request));
4848

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);
49+
const responseStream = await client.models.generateContentStream(request);
50+
51+
for await (const chunk of responseStream) {
52+
console.log(chunk.text);
5253
}
5354
}
5455
// [END generativeaionvertexai_stream_text_basic]
5556

56-
generateContent().catch(err => {
57+
generateContent(...process.argv.slice(2)).catch(err => {
5758
console.error(err.message);
5859
process.exitCode = 1;
5960
});

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,15 @@ const {describe, it} = require('mocha');
1919
const cp = require('child_process');
2020
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
2121

22+
const projectId = process.env.GOOGLE_SAMPLES_PROJECT;
23+
const location = process.env.LOCATION;
24+
const model = 'gemini-2.0-flash-001';
25+
2226
describe('Generative AI Multimodal Text Inference', () => {
2327
it('should generate text based on a prompt containing text, a video, and an image', async () => {
24-
const output = execSync('node ./inference/nonStreamMultiModalityBasic.js');
28+
const output = execSync(
29+
`node ./inference/nonStreamMultiModalityBasic.js ${projectId} ${location} ${model}`
30+
);
2531
assert(output.length > 0);
2632
});
2733
});

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,15 @@ const {describe, it} = require('mocha');
1919
const cp = require('child_process');
2020
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
2121

22+
const projectId = process.env.GOOGLE_SAMPLES_PROJECT;
23+
const location = process.env.LOCATION;
24+
const model = 'gemini-2.0-flash-001';
25+
2226
describe('Generative AI Basic Text Inference', () => {
2327
it('should create a generative text model and infer text from a prompt', async () => {
24-
const output = execSync('node ./inference/nonStreamTextBasic.js');
28+
const output = execSync(
29+
`node ./inference/nonStreamTextBasic.js ${projectId} ${location} ${model}`
30+
);
2531

2632
// Assert that the correct prompt was issued
2733
assert(output.match(/Write a story about a magic backpack/));

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,15 @@ const {describe, it} = require('mocha');
1919
const cp = require('child_process');
2020
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
2121

22+
const projectId = process.env.GOOGLE_SAMPLES_PROJECT;
23+
const location = process.env.LOCATION;
24+
const model = 'gemini-2.0-flash-001';
25+
2226
describe('Generative AI Basic Multimodal Text Inference Streaming', () => {
2327
it('should create a generative text model and infer text from a prompt, streaming the results', async () => {
24-
const output = execSync('node ./inference/streamMultiModalityBasic.js');
28+
const output = execSync(
29+
`node ./inference/streamMultiModalityBasic.js ${projectId} ${location} ${model}`
30+
);
2531
assert(output.length > 0);
2632
});
2733
});

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,15 @@ const {describe, it} = require('mocha');
1919
const cp = require('child_process');
2020
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
2121

22+
const projectId = process.env.GOOGLE_SAMPLES_PROJECT;
23+
const location = process.env.LOCATION;
24+
const model = 'gemini-2.0-flash-001';
25+
2226
describe('Generative AI Basic Text Inference Streaming', () => {
2327
it('should create a generative text model and infer text from a prompt, streaming the results', async () => {
24-
const output = execSync('node ./inference/streamTextBasic.js');
28+
const output = execSync(
29+
`node ./inference/streamTextBasic.js ${projectId} ${location} ${model}`
30+
);
2531
assert(output.length > 0);
2632
});
2733
});

0 commit comments

Comments
 (0)