Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions genai/image-generation/imggen-mmflash-with-txt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

'use strict';

// [START googlegenaisdk_imggen_mmflash_with_txt]
const fs = require('fs');
const {GoogleGenAI, Modality} = require('@google/genai');

const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT;
const GOOGLE_CLOUD_LOCATION =
process.env.GOOGLE_CLOUD_LOCATION || 'us-central1';

async function generateContent(
projectId = GOOGLE_CLOUD_PROJECT,
location = GOOGLE_CLOUD_LOCATION
) {
const ai = new GoogleGenAI({
vertexai: true,
project: projectId,
location: location,
});

const response = await ai.models.generateContentStream({
model: 'gemini-2.0-flash-exp',
contents:
'Generate an image of the Eiffel tower with fireworks in the background.',
config: {
responseModalities: [Modality.TEXT, Modality.IMAGE],
},
});

let i = 0;
for await (const chunk of response) {
const text = chunk.text;
const data = chunk.data;
if (text) {
console.debug(text);
} else if (data) {
const fileName = `generate_content_streaming_image_${i++}.png`;
console.debug(`Writing response image to file: ${fileName}.`);
fs.writeFileSync(fileName, data);
}
}

return 'generate_content_streaming_image';
}
// [END googlegenaisdk_imggen_mmflash_with_txt]

module.exports = {
generateContent,
};
28 changes: 28 additions & 0 deletions genai/test/imggen-mmflash-with-txt.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

'use strict';

const {assert} = require('chai');
const {describe, it} = require('mocha');

const projectId = process.env.CAIP_PROJECT_ID;
const sample = require('../image-generation/imggen-mmflash-with-txt.js');

describe('imggen-mmflash-with-txt', async () => {
it('should generate image from a text prompt', async () => {
const output = await sample.generateContent(projectId);
assert(output.length > 0 && output.includes('image'));
});
});
28 changes: 28 additions & 0 deletions genai/test/tools-code-exec-with-txt.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

'use strict';

const {assert} = require('chai');
const {describe, it} = require('mocha');

const projectId = process.env.CAIP_PROJECT_ID;
const sample = require('../tools/tools-code-exec-with-txt.js');

describe('tools-code-exec-with-txt', async () => {
it('should generate code and execution result', async () => {
const output = await sample.generateContent(projectId);
assert(output.length > 0);
});
});
52 changes: 52 additions & 0 deletions genai/tools/tools-code-exec-with-txt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

'use strict';

// [START googlegenaisdk_tools_code_exec_with_txt]
const {GoogleGenAI} = require('@google/genai');

const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT;
const GOOGLE_CLOUD_LOCATION = process.env.GOOGLE_CLOUD_LOCATION || 'global';

async function generateContent(
projectId = GOOGLE_CLOUD_PROJECT,
location = GOOGLE_CLOUD_LOCATION
) {
const ai = new GoogleGenAI({
vertexai: true,
project: projectId,
location: location,
});

const response = await ai.models.generateContent({
model: 'gemini-2.5-flash-preview-05-20',
contents:
'What is the sum of the first 50 prime numbers? Generate and run code for the calculation, and make sure you get all 50.',
config: {
tools: [{codeExecution: {}}],
temperature: 0,
},
});

console.debug(response.executableCode);
console.debug(response.codeExecutionResult);

return response.codeExecutionResult;
}
// [END googlegenaisdk_tools_code_exec_with_txt]

module.exports = {
generateContent,
};
Loading