From ba6e10f1dcc869f6bf7d5fa2185b9fa165e25364 Mon Sep 17 00:00:00 2001 From: Eric Dong Date: Mon, 12 May 2025 20:24:04 -0400 Subject: [PATCH 01/10] feat(genai): Add GenAI SDK samples (1) --- genai/package.json | 27 +++++++++++++++ genai/test/textgen-with-txt.test.js | 28 ++++++++++++++++ genai/text-generation/textgen-with-txt.js | 41 +++++++++++++++++++++++ 3 files changed, 96 insertions(+) create mode 100644 genai/package.json create mode 100644 genai/test/textgen-with-txt.test.js create mode 100644 genai/text-generation/textgen-with-txt.js diff --git a/genai/package.json b/genai/package.json new file mode 100644 index 0000000000..1786d292d9 --- /dev/null +++ b/genai/package.json @@ -0,0 +1,27 @@ +{ + "name": "nodejs-genai-samples", + "private": true, + "license": "Apache-2.0", + "author": "Google LLC", + "engines": { + "node": ">=16.0.0" + }, + "files": [ + "*.js" + ], + "scripts": { + "test": "c8 mocha -p -j 2 --timeout 2400000 test/*.test.js test/**/*.test.js" + }, + "dependencies": { + "@google/genai": "^0.13.0", + "axios": "^1.6.2", + "supertest": "^7.0.0" + }, + "devDependencies": { + "c8": "^10.0.0", + "chai": "^4.5.0", + "mocha": "^10.0.0", + "sinon": "^18.0.0", + "uuid": "^10.0.0" + } +} diff --git a/genai/test/textgen-with-txt.test.js b/genai/test/textgen-with-txt.test.js new file mode 100644 index 0000000000..a6f772a3a4 --- /dev/null +++ b/genai/test/textgen-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('../text-generation/textgen-with-txt.js'); + +describe('textgen-with-txt', async () => { + it('should generate text content from a text prompt', async () => { + const output = await sample.generateContent(projectId); + assert(output.length > 0); + }); +}); diff --git a/genai/text-generation/textgen-with-txt.js b/genai/text-generation/textgen-with-txt.js new file mode 100644 index 0000000000..7f9e45a7f1 --- /dev/null +++ b/genai/text-generation/textgen-with-txt.js @@ -0,0 +1,41 @@ +// 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_with_txt] +const {GoogleGenAI} = require('@google/genai'); + +async function generateContent( + projectId = process.env.GOOGLE_CLOUD_PROJECT, + location = process.env.GOOGLE_CLOUD_LOCATION || 'global' +) { + const ai = new GoogleGenAI({ + vertexai: true, + project: projectId, + location: location, + }); + + const response = await ai.models.generateContent({ + model: 'gemini-2.0-flash', + contents: 'How does AI work?', + }); + + return response.text; +} +// [END googlegenaisdk_textgen_with_txt] + +module.exports = { + generateContent, +}; From 58f7a434527a2b1d2fd27c4c995ed79773576b2d Mon Sep 17 00:00:00 2001 From: Eric Dong Date: Tue, 13 May 2025 08:11:00 -0400 Subject: [PATCH 02/10] Improve readability and asserts --- genai/test/textgen-with-txt.test.js | 4 +++- genai/text-generation/textgen-with-txt.js | 7 +++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/genai/test/textgen-with-txt.test.js b/genai/test/textgen-with-txt.test.js index a6f772a3a4..596e9c62a4 100644 --- a/genai/test/textgen-with-txt.test.js +++ b/genai/test/textgen-with-txt.test.js @@ -23,6 +23,8 @@ const sample = require('../text-generation/textgen-with-txt.js'); describe('textgen-with-txt', async () => { it('should generate text content from a text prompt', async () => { const output = await sample.generateContent(projectId); - assert(output.length > 0); + assert( + output.length > 0 && output.includes('AI') && output.includes('models') + ); }); }); diff --git a/genai/text-generation/textgen-with-txt.js b/genai/text-generation/textgen-with-txt.js index 7f9e45a7f1..ed9dcb1798 100644 --- a/genai/text-generation/textgen-with-txt.js +++ b/genai/text-generation/textgen-with-txt.js @@ -17,9 +17,12 @@ // [START googlegenaisdk_textgen_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 = process.env.GOOGLE_CLOUD_PROJECT, - location = process.env.GOOGLE_CLOUD_LOCATION || 'global' + projectId = GOOGLE_CLOUD_PROJECT, + location = GOOGLE_CLOUD_LOCATION ) { const ai = new GoogleGenAI({ vertexai: true, From cff6de19afdb3cd3c94bf862b1983ac4e856bb95 Mon Sep 17 00:00:00 2001 From: Eric Dong Date: Tue, 13 May 2025 08:32:44 -0400 Subject: [PATCH 03/10] Add streaming example --- genai/test/textgen-with-txt-stream.test.js | 28 +++++++++++ .../textgen-with-txt-stream.js | 49 +++++++++++++++++++ genai/text-generation/textgen-with-txt.js | 2 + 3 files changed, 79 insertions(+) create mode 100644 genai/test/textgen-with-txt-stream.test.js create mode 100644 genai/text-generation/textgen-with-txt-stream.js diff --git a/genai/test/textgen-with-txt-stream.test.js b/genai/test/textgen-with-txt-stream.test.js new file mode 100644 index 0000000000..6e818a89cb --- /dev/null +++ b/genai/test/textgen-with-txt-stream.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('../text-generation/textgen-with-txt-stream.js'); + +describe('textgen-with-txt-stream', async () => { + it('should generate streaming text content from a text prompt', async () => { + const output = await sample.generateContent(projectId); + assert(output.length > 0 && output.includes('sky')); + }); +}); diff --git a/genai/text-generation/textgen-with-txt-stream.js b/genai/text-generation/textgen-with-txt-stream.js new file mode 100644 index 0000000000..061151a5fa --- /dev/null +++ b/genai/text-generation/textgen-with-txt-stream.js @@ -0,0 +1,49 @@ +// 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_with_txt_stream] +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.generateContentStream({ + model: 'gemini-2.0-flash', + contents: 'Why is the sky blue?', + }); + + let response_text = ''; + for await (const chunk of response) { + response_text += chunk.text; + console.log(chunk.text); + } + return response_text; +} +// [END googlegenaisdk_textgen_with_txt_stream] + +module.exports = { + generateContent, +}; diff --git a/genai/text-generation/textgen-with-txt.js b/genai/text-generation/textgen-with-txt.js index ed9dcb1798..ea44a4009a 100644 --- a/genai/text-generation/textgen-with-txt.js +++ b/genai/text-generation/textgen-with-txt.js @@ -35,6 +35,8 @@ async function generateContent( contents: 'How does AI work?', }); + console.log(response.text); + return response.text; } // [END googlegenaisdk_textgen_with_txt] From c6711992ea41a9dce74df7e8209970f97ef2216c Mon Sep 17 00:00:00 2001 From: Eric Dong Date: Tue, 13 May 2025 09:30:32 -0400 Subject: [PATCH 04/10] Add image example --- genai/test/textgen-with-txt-img.test.js | 28 ++++++++++ genai/text-generation/textgen-with-txt-img.js | 53 +++++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 genai/test/textgen-with-txt-img.test.js create mode 100644 genai/text-generation/textgen-with-txt-img.js diff --git a/genai/test/textgen-with-txt-img.test.js b/genai/test/textgen-with-txt-img.test.js new file mode 100644 index 0000000000..fc4e4deb1b --- /dev/null +++ b/genai/test/textgen-with-txt-img.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('../text-generation/textgen-with-txt-img.js'); + +describe('textgen-with-txt-img', async () => { + it('should generate text content from a text prompt and an image', async () => { + const output = await sample.generateContent(projectId); + assert(output.length > 0 && output.includes('image')); + }); +}); diff --git a/genai/text-generation/textgen-with-txt-img.js b/genai/text-generation/textgen-with-txt-img.js new file mode 100644 index 0000000000..ec4b45200a --- /dev/null +++ b/genai/text-generation/textgen-with-txt-img.js @@ -0,0 +1,53 @@ +// 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_with_txt_img] +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 image = { + fileData: { + fileUri: 'gs://cloud-samples-data/generative-ai/image/scones.jpg', + mimeType: 'image/jpeg', + }, + }; + + const response = await ai.models.generateContent({ + model: 'gemini-2.0-flash', + contents: [image, 'What is shown in this image?'], + }); + + console.log(response.text); + + return response.text; +} +// [END googlegenaisdk_textgen_with_txt_img] + +module.exports = { + generateContent, +}; From 6552dda3beededdfaeca63f24806ec3c759b9eae Mon Sep 17 00:00:00 2001 From: Eric Dong Date: Tue, 13 May 2025 09:48:10 -0400 Subject: [PATCH 05/10] Add video example --- genai/test/textgen-with-video.test.js | 28 ++++++++++ genai/text-generation/textgen-with-video.js | 59 +++++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 genai/test/textgen-with-video.test.js create mode 100644 genai/text-generation/textgen-with-video.js diff --git a/genai/test/textgen-with-video.test.js b/genai/test/textgen-with-video.test.js new file mode 100644 index 0000000000..0f967cdfe0 --- /dev/null +++ b/genai/test/textgen-with-video.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('../text-generation/textgen-with-video.js'); + +describe('textgen-with-video', async () => { + it('should generate text content from a text prompt and a video', async () => { + const output = await sample.generateContent(projectId); + assert(output.length > 0 && output.includes('video')); + }); +}); diff --git a/genai/text-generation/textgen-with-video.js b/genai/text-generation/textgen-with-video.js new file mode 100644 index 0000000000..5ae4182522 --- /dev/null +++ b/genai/text-generation/textgen-with-video.js @@ -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_with_video] +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 prompt = ` + Analyze the provided video file, including its audio. + Summarize the main points of the video concisely. + Create a chapter breakdown with timestamps for key sections or topics discussed. + `; + + const video = { + fileData: { + fileUri: 'gs://cloud-samples-data/generative-ai/video/pixel8.mp4', + mimeType: 'video/mp4', + }, + }; + + const response = await ai.models.generateContent({ + model: 'gemini-2.0-flash', + contents: [video, prompt], + }); + + console.log(response.text); + + return response.text; +} +// [END googlegenaisdk_textgen_with_video] + +module.exports = { + generateContent, +}; From c6cbd3b67bbde5283d7dca059fd0f50a8222a91d Mon Sep 17 00:00:00 2001 From: Eric Dong Date: Tue, 13 May 2025 09:57:12 -0400 Subject: [PATCH 06/10] Improve relibiity in tests --- genai/test/textgen-with-txt.test.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/genai/test/textgen-with-txt.test.js b/genai/test/textgen-with-txt.test.js index 596e9c62a4..d3d2473774 100644 --- a/genai/test/textgen-with-txt.test.js +++ b/genai/test/textgen-with-txt.test.js @@ -23,8 +23,6 @@ const sample = require('../text-generation/textgen-with-txt.js'); describe('textgen-with-txt', async () => { it('should generate text content from a text prompt', async () => { const output = await sample.generateContent(projectId); - assert( - output.length > 0 && output.includes('AI') && output.includes('models') - ); + assert(output.length > 0 && output.includes('AI')); }); }); From 510347853165b6e3f78ffb3cc69090f373bbbe67 Mon Sep 17 00:00:00 2001 From: Eric Dong Date: Tue, 13 May 2025 10:28:41 -0400 Subject: [PATCH 07/10] Add multi image example --- genai/test/textgen-with-multi-img.test.js | 28 ++++++++ .../text-generation/textgen-with-multi-img.js | 64 +++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 genai/test/textgen-with-multi-img.test.js create mode 100644 genai/text-generation/textgen-with-multi-img.js diff --git a/genai/test/textgen-with-multi-img.test.js b/genai/test/textgen-with-multi-img.test.js new file mode 100644 index 0000000000..64f80a6442 --- /dev/null +++ b/genai/test/textgen-with-multi-img.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('../text-generation/textgen-with-multi-img.js'); + +describe('textgen-with-multi-img', async () => { + it('should generate text content from a text prompt and multiple images', async () => { + const output = await sample.generateContent(projectId); + assert(output.length > 0 && output.includes('blueberry')); + }); +}); diff --git a/genai/text-generation/textgen-with-multi-img.js b/genai/text-generation/textgen-with-multi-img.js new file mode 100644 index 0000000000..13edbe4bea --- /dev/null +++ b/genai/text-generation/textgen-with-multi-img.js @@ -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_with_multi_img] +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 image1 = { + fileData: { + fileUri: 'gs://cloud-samples-data/generative-ai/image/scones.jpg', + mimeType: 'image/jpeg', + }, + }; + + const image2 = { + fileData: { + fileUri: 'gs://cloud-samples-data/generative-ai/image/fruit.png', + mimeType: 'image/png', + }, + }; + + const response = await ai.models.generateContent({ + model: 'gemini-2.0-flash', + contents: [ + image1, + image2, + 'Generate a list of all the objects contained in both images.', + ], + }); + + console.log(response.text); + + return response.text; +} +// [END googlegenaisdk_textgen_with_multi_img] + +module.exports = { + generateContent, +}; From f34e1e12a0c957ca6fa0f2346c075be9412d32f3 Mon Sep 17 00:00:00 2001 From: Eric Dong Date: Tue, 13 May 2025 10:46:02 -0400 Subject: [PATCH 08/10] Add system instructions example --- genai/test/textgen-sys-instr-with-txt.test.js | 28 +++++++++ .../textgen-sys-instr-with-txt.js | 57 +++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 genai/test/textgen-sys-instr-with-txt.test.js create mode 100644 genai/text-generation/textgen-sys-instr-with-txt.js diff --git a/genai/test/textgen-sys-instr-with-txt.test.js b/genai/test/textgen-sys-instr-with-txt.test.js new file mode 100644 index 0000000000..0162870b75 --- /dev/null +++ b/genai/test/textgen-sys-instr-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('../text-generation/textgen-sys-instr-with-txt.js'); + +describe('textgen-sys-instr-with-txt', async () => { + it('should generate text content from a text prompt and with system instructions', async () => { + const output = await sample.generateContent(projectId); + assert(output.length > 0 && output.includes('bagels')); + }); +}); diff --git a/genai/text-generation/textgen-sys-instr-with-txt.js b/genai/text-generation/textgen-sys-instr-with-txt.js new file mode 100644 index 0000000000..c08810d71d --- /dev/null +++ b/genai/text-generation/textgen-sys-instr-with-txt.js @@ -0,0 +1,57 @@ +// 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_sys_instr_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 prompt = ` + User input: I like bagels. + Answer: + `; + + const response = await ai.models.generateContent({ + model: 'gemini-2.0-flash', + contents: prompt, + config: { + systemInstruction: [ + 'You are a language translator.', + 'Your mission is to translate text in English to French.', + ], + }, + }); + + console.log(response.text); + + return response.text; +} +// [END googlegenaisdk_textgen_sys_instr_with_txt] + +module.exports = { + generateContent, +}; From 3e856b3e7bb65e26bac9d38bb18c55ce1207eeea Mon Sep 17 00:00:00 2001 From: Eric Dong Date: Tue, 13 May 2025 11:35:23 -0400 Subject: [PATCH 09/10] Add function calling example --- genai/test/tools-func-desc-with-txt.test.js | 28 +++++++ genai/tools/tools-func-desc-with-txt.js | 91 +++++++++++++++++++++ 2 files changed, 119 insertions(+) create mode 100644 genai/test/tools-func-desc-with-txt.test.js create mode 100644 genai/tools/tools-func-desc-with-txt.js diff --git a/genai/test/tools-func-desc-with-txt.test.js b/genai/test/tools-func-desc-with-txt.test.js new file mode 100644 index 0000000000..d627a52aaf --- /dev/null +++ b/genai/test/tools-func-desc-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-func-desc-with-txt.js'); + +describe('tools-func-desc-with-txt', async () => { + it('should generate a function call', async () => { + const output = await sample.generateContent(projectId); + assert(output.length > 0); + }); +}); diff --git a/genai/tools/tools-func-desc-with-txt.js b/genai/tools/tools-func-desc-with-txt.js new file mode 100644 index 0000000000..6790de561a --- /dev/null +++ b/genai/tools/tools-func-desc-with-txt.js @@ -0,0 +1,91 @@ +// 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_func_desc_with_txt] +const {GoogleGenAI, Type} = 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 get_album_sales = { + name: 'get_album_sales', + description: 'Gets the number of albums sold', + parameters: { + type: Type.OBJECT, + properties: { + albums: { + type: Type.ARRAY, + description: 'List of albums', + items: { + description: 'Album and its sales', + type: Type.OBJECT, + properties: { + album_name: { + type: Type.STRING, + description: 'Name of the music album', + }, + copies_sold: { + type: Type.INTEGER, + description: 'Number of copies sold', + }, + }, + }, + }, + }, + }, + }; + + const sales_tool = { + functionDeclarations: [get_album_sales], + }; + + const prompt = ` + At Stellar Sounds, a music label, 2024 was a rollercoaster. "Echoes of the Night", a debut synth-pop album, + surprisingly sold 350,000 copies, while veteran rock band "Crimson Tide's" latest, "Reckless Hearts", + lagged at 120,000. Their up-and-coming indie artist, "Luna Bloom's" EP, "Whispers of Dawn", + secured 75,000 sales. The biggest disappointment was the highly-anticipated rap album "Street Symphony" + only reaching 100,000 units. Overall, Stellar Sounds moved over 645,000 units this year, revealing unexpected + trends in music consumption. + `; + + const response = await ai.models.generateContent({ + model: 'gemini-2.0-flash', + contents: prompt, + config: { + tools: [sales_tool], + temperature: 0, + }, + }); + + console.log(response.functionCalls); + + return response.functionCalls; +} +// [END googlegenaisdk_tools_func_desc_with_txt] + +module.exports = { + generateContent, +}; From 7407ca607549d7344be4ffcb83e155ccb4fecaab Mon Sep 17 00:00:00 2001 From: Eric Dong Date: Tue, 13 May 2025 11:56:00 -0400 Subject: [PATCH 10/10] Add count tokens example --- genai/count-tokens/counttoken-with-txt.js | 46 +++++++++++++++++++++++ genai/test/counttoken-with-txt.test.js | 28 ++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 genai/count-tokens/counttoken-with-txt.js create mode 100644 genai/test/counttoken-with-txt.test.js diff --git a/genai/count-tokens/counttoken-with-txt.js b/genai/count-tokens/counttoken-with-txt.js new file mode 100644 index 0000000000..eb82c348bf --- /dev/null +++ b/genai/count-tokens/counttoken-with-txt.js @@ -0,0 +1,46 @@ +// 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_counttoken_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 countTokens( + projectId = GOOGLE_CLOUD_PROJECT, + location = GOOGLE_CLOUD_LOCATION +) { + const ai = new GoogleGenAI({ + vertexai: true, + project: projectId, + location: location, + }); + + const response = await ai.models.countTokens({ + model: 'gemini-2.0-flash', + contents: 'What is the highest mountain in Africa?', + }); + + console.log(response); + + return response.totalTokens; +} +// [END googlegenaisdk_counttoken_with_txt] + +module.exports = { + countTokens, +}; diff --git a/genai/test/counttoken-with-txt.test.js b/genai/test/counttoken-with-txt.test.js new file mode 100644 index 0000000000..85d8f2153b --- /dev/null +++ b/genai/test/counttoken-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('../count-tokens/counttoken-with-txt.js'); + +describe('counttoken-with-txt', async () => { + it('should return the total token count for a text prompt', async () => { + const output = await sample.countTokens(projectId); + assert(output > 0); + }); +});