Skip to content

Commit efc8d2f

Browse files
angelcaamaliennae
andauthored
refactor(gnerative-ai): migrate batch 2 generative-ai samples to @google/genai (GoogleCloudPlatform#4331)
* refactor: migrate batch 2 generative-ai samples to @google/genai * test: add LOCATION fallback and update model to gemini-2.5-flash in translate test --------- Co-authored-by: Jennifer Davis <sigje@google.com>
1 parent 2f827d4 commit efc8d2f

18 files changed

Lines changed: 232 additions & 203 deletions

generative-ai/snippets/gemini-all-modalities.js

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

1515
// [START generativeaionvertexai_gemini_all_modalities]
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-
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-2.0-flash-001',
21+
async function analyze_all_modalities(
22+
projectId = 'PROJECT_ID',
23+
model = 'gemini-2.5-flash'
24+
) {
25+
const client = new GoogleGenAI({
26+
vertexai: true,
27+
project: projectId,
28+
location: 'us-central1',
2629
});
2730

2831
const videoFilePart = {
29-
file_data: {
30-
file_uri:
32+
fileData: {
33+
fileUri:
3134
'gs://cloud-samples-data/generative-ai/video/behind_the_scenes_pixel.mp4',
32-
mime_type: 'video/mp4',
35+
mimeType: 'video/mp4',
3336
},
3437
};
3538
const imageFilePart = {
36-
file_data: {
37-
file_uri:
39+
fileData: {
40+
fileUri:
3841
'gs://cloud-samples-data/generative-ai/image/a-man-and-a-dog.png',
39-
mime_type: 'image/png',
42+
mimeType: 'image/png',
4043
},
4144
};
4245

@@ -52,13 +55,12 @@ async function analyze_all_modalities(projectId = 'PROJECT_ID') {
5255
- What is the context of the moment and what does the narrator say about it?`,
5356
};
5457

55-
const request = {
56-
contents: [{role: 'user', parts: [videoFilePart, imageFilePart, textPart]}],
57-
};
58+
const response = await client.models.generateContent({
59+
model: model,
60+
contents: [videoFilePart, imageFilePart, textPart],
61+
});
5862

59-
const resp = await generativeModel.generateContent(request);
60-
const contentResponse = await resp.response;
61-
console.log(JSON.stringify(contentResponse));
63+
console.log(response.text);
6264
}
6365
// [END generativeaionvertexai_gemini_all_modalities]
6466

generative-ai/snippets/gemini-audio-summarization.js

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

1515
// [START generativeaionvertexai_gemini_audio_summarization]
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-
async function summarize_audio(projectId = 'PROJECT_ID') {
22-
const vertexAI = new VertexAI({project: projectId, location: 'us-central1'});
23-
24-
const generativeModel = vertexAI.getGenerativeModel({
25-
model: 'gemini-2.0-flash-001',
20+
async function summarize_audio(
21+
projectId = 'PROJECT_ID',
22+
model = 'gemini-2.5-flash'
23+
) {
24+
const client = new GoogleGenAI({
25+
vertexai: true,
26+
project: projectId,
27+
location: 'us-central1',
2628
});
2729

2830
const filePart = {
29-
file_data: {
30-
file_uri: 'gs://cloud-samples-data/generative-ai/audio/pixel.mp3',
31-
mime_type: 'audio/mpeg',
31+
fileData: {
32+
fileUri: 'gs://cloud-samples-data/generative-ai/audio/pixel.mp3',
33+
mimeType: 'audio/mpeg',
3234
},
3335
};
3436
const textPart = {
@@ -38,13 +40,12 @@ async function summarize_audio(projectId = 'PROJECT_ID') {
3840
Do not make up any information that is not part of the audio and do not be verbose.`,
3941
};
4042

41-
const request = {
42-
contents: [{role: 'user', parts: [filePart, textPart]}],
43-
};
43+
const response = await client.models.generateContent({
44+
model: model,
45+
contents: [filePart, textPart],
46+
});
4447

45-
const resp = await generativeModel.generateContent(request);
46-
const contentResponse = await resp.response;
47-
console.log(JSON.stringify(contentResponse));
48+
console.log(response.text);
4849
}
4950
// [END generativeaionvertexai_gemini_audio_summarization]
5051

generative-ai/snippets/gemini-audio-transcription.js

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

1515
// [START generativeaionvertexai_gemini_audio_transcription]
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-
async function transcript_audio(projectId = 'PROJECT_ID') {
22-
const vertexAI = new VertexAI({project: projectId, location: 'us-central1'});
23-
24-
const generativeModel = vertexAI.getGenerativeModel({
25-
model: 'gemini-2.0-flash-001',
21+
async function transcript_audio(
22+
projectId = 'PROJECT_ID',
23+
model = 'gemini-2.5-flash'
24+
) {
25+
const client = new GoogleGenAI({
26+
vertexai: true,
27+
project: projectId,
28+
location: 'us-central1',
2629
});
2730

2831
const filePart = {
29-
file_data: {
30-
file_uri: 'gs://cloud-samples-data/generative-ai/audio/pixel.mp3',
31-
mime_type: 'audio/mpeg',
32+
fileData: {
33+
fileUri: 'gs://cloud-samples-data/generative-ai/audio/pixel.mp3',
34+
mimeType: 'audio/mpeg',
3235
},
3336
};
3437
const textPart = {
@@ -37,13 +40,12 @@ async function transcript_audio(projectId = 'PROJECT_ID') {
3740
Use speaker A, speaker B, etc. to identify speakers.`,
3841
};
3942

40-
const request = {
41-
contents: [{role: 'user', parts: [filePart, textPart]}],
42-
};
43+
const response = await client.models.generateContent({
44+
model: model,
45+
contents: [filePart, textPart],
46+
});
4347

44-
const resp = await generativeModel.generateContent(request);
45-
const contentResponse = await resp.response;
46-
console.log(JSON.stringify(contentResponse));
48+
console.log(response.text);
4749
}
4850
// [END generativeaionvertexai_gemini_audio_transcription]
4951

generative-ai/snippets/gemini-pdf.js

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

1515
// [START generativeaionvertexai_gemini_pdf]
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-
async function analyze_pdf(projectId = 'PROJECT_ID') {
22-
const vertexAI = new VertexAI({project: projectId, location: 'us-central1'});
23-
24-
const generativeModel = vertexAI.getGenerativeModel({
25-
model: 'gemini-2.0-flash-001',
21+
async function analyze_pdf(
22+
projectId = 'PROJECT_ID',
23+
model = 'gemini-2.5-flash'
24+
) {
25+
const client = new GoogleGenAI({
26+
vertexai: true,
27+
project: projectId,
28+
location: 'us-central1',
2629
});
2730

2831
const filePart = {
@@ -31,19 +34,19 @@ async function analyze_pdf(projectId = 'PROJECT_ID') {
3134
mimeType: 'application/pdf',
3235
},
3336
};
37+
3438
const textPart = {
3539
text: `
3640
You are a very professional document summarization specialist.
3741
Please summarize the given document.`,
3842
};
3943

40-
const request = {
41-
contents: [{role: 'user', parts: [filePart, textPart]}],
42-
};
44+
const response = await client.models.generateContent({
45+
model: model,
46+
contents: [filePart, textPart],
47+
});
4348

44-
const resp = await generativeModel.generateContent(request);
45-
const contentResponse = await resp.response;
46-
console.log(JSON.stringify(contentResponse));
49+
console.log(response.text);
4750
}
4851
// [END generativeaionvertexai_gemini_pdf]
4952

generative-ai/snippets/gemini-system-instruction.js

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

1515
// [START generativeaionvertexai_gemini_system_instruction]
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-
async function set_system_instruction(projectId = 'PROJECT_ID') {
22-
const vertexAI = new VertexAI({project: projectId, location: 'us-central1'});
23-
24-
const generativeModel = vertexAI.getGenerativeModel({
25-
model: 'gemini-2.0-flash-001',
26-
systemInstruction: {
27-
parts: [
28-
{text: 'You are a helpful language translator.'},
29-
{text: 'Your mission is to translate text in English to French.'},
30-
],
31-
},
21+
async function set_system_instruction(
22+
projectId = 'PROJECT_ID',
23+
model = 'gemini-2.5-flash'
24+
) {
25+
const client = new GoogleGenAI({
26+
vertexai: true,
27+
project: projectId,
28+
location: 'us-central1',
3229
});
3330

3431
const textPart = {
@@ -37,13 +34,20 @@ async function set_system_instruction(projectId = 'PROJECT_ID') {
3734
Answer:`,
3835
};
3936

40-
const request = {
41-
contents: [{role: 'user', parts: [textPart]}],
42-
};
37+
const response = await client.models.generateContent({
38+
model: model,
39+
contents: [textPart],
40+
config: {
41+
systemInstruction: {
42+
parts: [
43+
{text: 'You are a helpful language translator.'},
44+
{text: 'Your mission is to translate text in English to French.'},
45+
],
46+
},
47+
},
48+
});
4349

44-
const resp = await generativeModel.generateContent(request);
45-
const contentResponse = await resp.response;
46-
console.log(JSON.stringify(contentResponse));
50+
console.log(response.text);
4751
}
4852
// [END generativeaionvertexai_gemini_system_instruction]
4953

generative-ai/snippets/gemini-text-input.js

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

1515
// [START generativeaionvertexai_gemini_generate_from_text_input]
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-
async function generate_from_text_input(projectId = 'PROJECT_ID') {
22-
const vertexAI = new VertexAI({project: projectId, location: 'us-central1'});
23-
24-
const generativeModel = vertexAI.getGenerativeModel({
25-
model: 'gemini-2.0-flash-001',
20+
async function generate_from_text_input(
21+
projectId = 'PROJECT_ID',
22+
model = 'gemini-2.5-flash'
23+
) {
24+
const client = new GoogleGenAI({
25+
vertexai: true,
26+
project: projectId,
27+
location: 'us-central1',
2628
});
2729

2830
const prompt =
2931
"What's a good name for a flower shop that specializes in selling bouquets of dried flowers?";
3032

31-
const resp = await generativeModel.generateContent(prompt);
32-
const contentResponse = await resp.response;
33-
console.log(JSON.stringify(contentResponse));
33+
const response = await client.models.generateContent({
34+
model: model,
35+
contents: prompt,
36+
});
37+
38+
console.log(response.text);
3439
}
3540
// [END generativeaionvertexai_gemini_generate_from_text_input]
3641

0 commit comments

Comments
 (0)