-
Notifications
You must be signed in to change notification settings - Fork 2k
feat(genai): Add image generation and code execution examples #4096
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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, | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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')); | ||
gericdong marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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, | ||
| }; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.