Skip to content

Commit 2e89613

Browse files
committed
feat(genai): Add image generation and code execution examples
1 parent 8a6e111 commit 2e89613

File tree

4 files changed

+171
-0
lines changed

4 files changed

+171
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
let i = 0;
45+
for await (const chunk of response) {
46+
const text = chunk.text;
47+
const data = chunk.data;
48+
if (text) {
49+
console.debug(text);
50+
} else if (data) {
51+
const fileName = `generate_content_streaming_image_${i++}.png`;
52+
console.debug(`Writing response image to file: ${fileName}.`);
53+
fs.writeFileSync(fileName, data);
54+
}
55+
}
56+
57+
return 'generate_content_streaming_image';
58+
}
59+
// [END googlegenaisdk_imggen_mmflash_with_txt]
60+
61+
module.exports = {
62+
generateContent,
63+
};
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('../image-generation/imggen-mmflash-with-txt.js');
22+
23+
describe('imggen-mmflash-with-txt', async () => {
24+
it('should generate image from a text prompt', async () => {
25+
const output = await sample.generateContent(projectId);
26+
assert(output.length > 0 && output.includes('image'));
27+
});
28+
});
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)