-
Notifications
You must be signed in to change notification settings - Fork 2k
feat(genai): Sample/textgen part1 #4160
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
gericdong
merged 17 commits into
GoogleCloudPlatform:main
from
Guiners:sample/textgen-part1
Sep 11, 2025
Merged
Changes from 8 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
7f48533
adding samples and tests
7d7a3f4
adding samples, test, lints
9b7ee3c
adding samples, test, lints
9c1e9ad
Merge branch 'main' into sample/textgen-part1
Guiners b02de4c
Merge branch 'main' into sample/textgen-part1
Guiners 46a71cd
adding samples, test, lints
a8d084b
adding samples, test, lints
edf9755
Merge branch 'main' into sample/textgen-part1
Guiners d580470
Update genai/text-generation/textgen-async-with-txt.js
Guiners 2e9fb52
Update genai/text-generation/textgen-chat-stream-with-txt.js
Guiners eaa09e7
Merge branch 'main' into sample/textgen-part1
Guiners 16cf086
adding samples, test, lints
6b9003a
Merge branch 'main' into sample/textgen-part1
msampathkumar edd0903
adding samples, test, lints
5fb5089
Merge branch 'main' into sample/textgen-part1
gericdong 6729ca7
fixing functions names
6dd92f9
Merge branch 'main' into sample/textgen-part1
gericdong 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
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,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('../text-generation/textgen-async-with-txt.js'); | ||
|
|
||
| describe('textgen-async-with-txt', () => { | ||
| it('should generate text content from a text prompt and with system instructions', async function () { | ||
| this.timeout(100000); | ||
| 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,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('../text-generation/textgen-chat-stream-with-txt.js'); | ||
|
|
||
| describe('textgen-chat-stream-with-txt', () => { | ||
| it('should generate text content from a mute video', async function () { | ||
| this.timeout(100000); | ||
| const output = await sample.generateContent(projectId); | ||
| assert.isTrue(output); | ||
| }); | ||
| }); |
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,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('../text-generation/textgen-chat-with-txt.js'); | ||
|
|
||
| describe('textgen-chat-with-txt', () => { | ||
| it('should generate chat content from a text prompt', async function () { | ||
| this.timeout(100000); | ||
| 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,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('../text-generation/textgen-config-with-txt.js'); | ||
|
|
||
| describe('textgen-config-with-txt', () => { | ||
| it('should generate text content from a text prompt with config', async function () { | ||
| this.timeout(100000); | ||
| 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,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('../text-generation/textgen-with-mute-video.js'); | ||
|
|
||
| describe('textgen-with-mute-video', () => { | ||
| it('should generate text content from a mute video', async function () { | ||
| this.timeout(100000); | ||
| 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,54 @@ | ||
| // 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_textgen_async_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', | ||
| contents: | ||
| 'Compose a song about the adventures of a time-traveling nuggets.', | ||
| config: { | ||
| responseMimeType: 'text/plain', | ||
| }, | ||
| }); | ||
|
|
||
| console.log(response.text); | ||
|
|
||
| return response.text; | ||
| } | ||
| // Example response: | ||
| // (Verse 1) | ||
| // Sammy the nugget, a furry little friend | ||
| // Had a knack for adventure, beyond all comprehend | ||
| // [END googlegenaisdk_textgen_async_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,54 @@ | ||
| // 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_textgen_chat_stream_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 chatSession = ai.chats.create({ | ||
| model: 'gemini-2.5-flash', | ||
| }); | ||
|
|
||
| for await (const chunk of await chatSession.sendMessageStream({ | ||
| message: 'Why lava is red?', | ||
Guiners marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| })) { | ||
| console.log(chunk.text); | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
| // Example response: | ||
| // The | ||
| // sky appears blue due to a phenomenon called **Rayleigh scattering**. Here's | ||
| // a breakdown of why: | ||
| // ... | ||
| // [END googlegenaisdk_textgen_chat_stream_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,59 @@ | ||
| // 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_textgen_chat_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 chatSession = ai.chats.create({ | ||
| model: 'gemini-2.5-flash', | ||
| history: [ | ||
| { | ||
| role: 'user', | ||
| parts: [{text: 'Hello'}], | ||
| }, | ||
| { | ||
| role: 'model', | ||
| parts: [{text: 'Great to meet you. What would you like to know?'}], | ||
| }, | ||
| ], | ||
| }); | ||
|
|
||
| const response = await chatSession.sendMessage({message: 'Tell me a story.'}); | ||
| console.log(response.text); | ||
|
|
||
| return response.text; | ||
| } | ||
| // Example response: | ||
| // Okay, here's a story for you: | ||
| // ... | ||
| // [END googlegenaisdk_textgen_chat_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,64 @@ | ||
| // 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_textgen_config_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 config = { | ||
| temperature: 0, | ||
| candidateCount: 1, | ||
| responseMimeType: 'application/json', | ||
| topP: 0.95, | ||
| topK: 20, | ||
| seed: 5, | ||
| maxOutputTokens: 500, | ||
| stopSequences: ['STOP!'], | ||
| presencePenalty: 0.0, | ||
| frequencyPenalty: 0.0, | ||
| }; | ||
|
|
||
| const response = await ai.models.generateContent({ | ||
| model: 'gemini-2.5-flash', | ||
| contents: 'Why is the sky blue?', | ||
| config: config, | ||
| }); | ||
|
|
||
| console.log(response.text); | ||
|
|
||
| return response.text; | ||
| } | ||
| // Example response: | ||
| // { | ||
| // "explanation": "The sky appears blue due to a phenomenon called Rayleigh scattering. When ... | ||
| // } | ||
| // [END googlegenaisdk_textgen_config_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.