Skip to content

Commit 2e46a66

Browse files
authored
feat(genai): Add image generation and code execution examples (#4096)
* feat(genai): Add image generation and code execution examples * Improve test
1 parent 8a6e111 commit 2e46a66

File tree

4 files changed

+178
-0
lines changed

4 files changed

+178
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Copyright 2025 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+
// [START googlegenaisdk_imggen_mmflash_with_txt]
18+
const fs = require('fs');
19+
const {GoogleGenAI, Modality} = require('@google/genai');
20+
21+
const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT;
22+
const GOOGLE_CLOUD_LOCATION =
23+
process.env.GOOGLE_CLOUD_LOCATION || 'us-central1';
24+
25+
async function generateContent(
26+
projectId = GOOGLE_CLOUD_PROJECT,
27+
location = GOOGLE_CLOUD_LOCATION
28+
) {
29+
const ai = new GoogleGenAI({
30+
vertexai: true,
31+
project: projectId,
32+
location: location,
33+
});
34+
35+
const response = await ai.models.generateContentStream({
36+
model: 'gemini-2.0-flash-exp',
37+
contents:
38+
'Generate an image of the Eiffel tower with fireworks in the background.',
39+
config: {
40+
responseModalities: [Modality.TEXT, Modality.IMAGE],
41+
},
42+
});
43+
44+
const generatedFileNames = [];
45+
let imageIndex = 0;
46+
for await (const chunk of response) {
47+
const text = chunk.text;
48+
const data = chunk.data;
49+
if (text) {
50+
console.debug(text);
51+
} else if (data) {
52+
const fileName = `generate_content_streaming_image_${imageIndex++}.png`;
53+
console.debug(`Writing response image to file: ${fileName}.`);
54+
try {
55+
fs.writeFileSync(fileName, data);
56+
generatedFileNames.push(fileName);
57+
} catch (error) {
58+
console.error(`Failed to write image file ${fileName}:`, error);
59+
}
60+
}
61+
}
62+
63+
return generatedFileNames;
64+
}
65+
// [END googlegenaisdk_imggen_mmflash_with_txt]
66+
67+
module.exports = {
68+
generateContent,
69+
};
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2025 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+
20+
const projectId = process.env.CAIP_PROJECT_ID;
21+
const sample = require('../image-generation/imggen-mmflash-with-txt.js');
22+
23+
describe('imggen-mmflash-with-txt', async () => {
24+
it('should generate images from a text prompt', async () => {
25+
const generatedFileNames = await sample.generateContent(projectId);
26+
assert(Array.isArray(generatedFileNames));
27+
assert(generatedFileNames.length > 0);
28+
});
29+
});
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright 2025 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+
20+
const projectId = process.env.CAIP_PROJECT_ID;
21+
const sample = require('../tools/tools-code-exec-with-txt.js');
22+
23+
describe('tools-code-exec-with-txt', async () => {
24+
it('should generate code and execution result', async () => {
25+
const output = await sample.generateContent(projectId);
26+
assert(output.length > 0);
27+
});
28+
});
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Copyright 2025 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+
// [START googlegenaisdk_tools_code_exec_with_txt]
18+
const {GoogleGenAI} = require('@google/genai');
19+
20+
const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT;
21+
const GOOGLE_CLOUD_LOCATION = process.env.GOOGLE_CLOUD_LOCATION || 'global';
22+
23+
async function generateContent(
24+
projectId = GOOGLE_CLOUD_PROJECT,
25+
location = GOOGLE_CLOUD_LOCATION
26+
) {
27+
const ai = new GoogleGenAI({
28+
vertexai: true,
29+
project: projectId,
30+
location: location,
31+
});
32+
33+
const response = await ai.models.generateContent({
34+
model: 'gemini-2.5-flash-preview-05-20',
35+
contents:
36+
'What is the sum of the first 50 prime numbers? Generate and run code for the calculation, and make sure you get all 50.',
37+
config: {
38+
tools: [{codeExecution: {}}],
39+
temperature: 0,
40+
},
41+
});
42+
43+
console.debug(response.executableCode);
44+
console.debug(response.codeExecutionResult);
45+
46+
return response.codeExecutionResult;
47+
}
48+
// [END googlegenaisdk_tools_code_exec_with_txt]
49+
50+
module.exports = {
51+
generateContent,
52+
};

0 commit comments

Comments
 (0)