diff --git a/genai/image-generation/imggen-mmflash-with-txt.js b/genai/image-generation/imggen-mmflash-with-txt.js new file mode 100644 index 0000000000..054a7f72d5 --- /dev/null +++ b/genai/image-generation/imggen-mmflash-with-txt.js @@ -0,0 +1,69 @@ +// 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], + }, + }); + + const generatedFileNames = []; + let imageIndex = 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_${imageIndex++}.png`; + console.debug(`Writing response image to file: ${fileName}.`); + try { + fs.writeFileSync(fileName, data); + generatedFileNames.push(fileName); + } catch (error) { + console.error(`Failed to write image file ${fileName}:`, error); + } + } + } + + return generatedFileNames; +} +// [END googlegenaisdk_imggen_mmflash_with_txt] + +module.exports = { + generateContent, +}; diff --git a/genai/test/imggen-mmflash-with-txt.test.js b/genai/test/imggen-mmflash-with-txt.test.js new file mode 100644 index 0000000000..09b9544093 --- /dev/null +++ b/genai/test/imggen-mmflash-with-txt.test.js @@ -0,0 +1,29 @@ +// 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 images from a text prompt', async () => { + const generatedFileNames = await sample.generateContent(projectId); + assert(Array.isArray(generatedFileNames)); + assert(generatedFileNames.length > 0); + }); +}); diff --git a/genai/test/tools-code-exec-with-txt.test.js b/genai/test/tools-code-exec-with-txt.test.js new file mode 100644 index 0000000000..cedd6f6b7c --- /dev/null +++ b/genai/test/tools-code-exec-with-txt.test.js @@ -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); + }); +}); diff --git a/genai/tools/tools-code-exec-with-txt.js b/genai/tools/tools-code-exec-with-txt.js new file mode 100644 index 0000000000..7590be8237 --- /dev/null +++ b/genai/tools/tools-code-exec-with-txt.js @@ -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, +};