diff --git a/genai/test/textgen-async-with-txt.test.js b/genai/test/textgen-async-with-txt.test.js new file mode 100644 index 0000000000..540ff3e682 --- /dev/null +++ b/genai/test/textgen-async-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('../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.generateText(projectId); + assert(output.length > 0); + }); +}); diff --git a/genai/test/textgen-chat-stream-with-txt.test.js b/genai/test/textgen-chat-stream-with-txt.test.js new file mode 100644 index 0000000000..a81fdb4031 --- /dev/null +++ b/genai/test/textgen-chat-stream-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('../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.generateText(projectId); + assert.isTrue(output); + }); +}); diff --git a/genai/test/textgen-chat-with-txt.test.js b/genai/test/textgen-chat-with-txt.test.js new file mode 100644 index 0000000000..184013536c --- /dev/null +++ b/genai/test/textgen-chat-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('../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.generateText(projectId); + assert(output.length > 0); + }); +}); diff --git a/genai/test/textgen-code-with-pdf.test.js b/genai/test/textgen-code-with-pdf.test.js index d4a11130a1..22ec5b77fe 100644 --- a/genai/test/textgen-code-with-pdf.test.js +++ b/genai/test/textgen-code-with-pdf.test.js @@ -23,7 +23,7 @@ const sample = require('../text-generation/textgen-code-with-pdf.js'); describe('textgen-code-with-pdf', () => { it('should generate text content from a pdf', async function () { this.timeout(100000); - const output = await sample.generateContent(projectId); + const output = await sample.generateText(projectId); assert(output.length > 0); }); }); diff --git a/genai/test/textgen-config-with-txt.test.js b/genai/test/textgen-config-with-txt.test.js new file mode 100644 index 0000000000..b070952515 --- /dev/null +++ b/genai/test/textgen-config-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('../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); + }); +}); diff --git a/genai/test/textgen-with-mute-video.test.js b/genai/test/textgen-with-mute-video.test.js new file mode 100644 index 0000000000..65b5013126 --- /dev/null +++ b/genai/test/textgen-with-mute-video.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('../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.generateText(projectId); + assert(output.length > 0); + }); +}); diff --git a/genai/text-generation/textgen-async-with-txt.js b/genai/text-generation/textgen-async-with-txt.js new file mode 100644 index 0000000000..c47f59b4fa --- /dev/null +++ b/genai/text-generation/textgen-async-with-txt.js @@ -0,0 +1,56 @@ +// 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 generateText( + projectId = GOOGLE_CLOUD_PROJECT, + location = GOOGLE_CLOUD_LOCATION +) { + const client = new GoogleGenAI({ + vertexai: true, + project: projectId, + location: location, + }); + + const response = await client.models.generateContent({ + model: 'gemini-2.5-flash', + contents: + 'Compose a song about the adventures of a time-traveling squirrel.', + config: { + responseMimeType: 'text/plain', + }, + }); + + console.log(response.text); + + // Example response: + // (Verse 1) + // Sammy the nugget, a furry little friend + // Had a knack for adventure, beyond all comprehend + + return response.text; +} + +// [END googlegenaisdk_textgen_async_with_txt] + +module.exports = { + generateText, +}; diff --git a/genai/text-generation/textgen-chat-stream-with-txt.js b/genai/text-generation/textgen-chat-stream-with-txt.js new file mode 100644 index 0000000000..9276368582 --- /dev/null +++ b/genai/text-generation/textgen-chat-stream-with-txt.js @@ -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 generateText( + projectId = GOOGLE_CLOUD_PROJECT, + location = GOOGLE_CLOUD_LOCATION +) { + const client = new GoogleGenAI({ + vertexai: true, + project: projectId, + location: location, + }); + + const chatSession = client.chats.create({ + model: 'gemini-2.5-flash', + }); + + for await (const chunk of await chatSession.sendMessageStream({ + message: 'Why is the sky blue?', + })) { + console.log(chunk.text); + } + // Example response: + // The + // sky appears blue due to a phenomenon called **Rayleigh scattering**. Here's + // a breakdown of why: + // ... + return true; +} + +// [END googlegenaisdk_textgen_chat_stream_with_txt] + +module.exports = { + generateText, +}; diff --git a/genai/text-generation/textgen-chat-with-txt.js b/genai/text-generation/textgen-chat-with-txt.js new file mode 100644 index 0000000000..ae81aa9aa5 --- /dev/null +++ b/genai/text-generation/textgen-chat-with-txt.js @@ -0,0 +1,61 @@ +// 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 generateText( + projectId = GOOGLE_CLOUD_PROJECT, + location = GOOGLE_CLOUD_LOCATION +) { + const client = new GoogleGenAI({ + vertexai: true, + project: projectId, + location: location, + }); + + const chatSession = client.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); + + // Example response: + // Okay, here's a story for you: + // ... + + return response.text; +} + +// [END googlegenaisdk_textgen_chat_with_txt] + +module.exports = { + generateText, +}; diff --git a/genai/text-generation/textgen-code-with-pdf.js b/genai/text-generation/textgen-code-with-pdf.js index 762f4bff19..7211d177bc 100644 --- a/genai/text-generation/textgen-code-with-pdf.js +++ b/genai/text-generation/textgen-code-with-pdf.js @@ -20,7 +20,7 @@ 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( +async function generateText( projectId = GOOGLE_CLOUD_PROJECT, location = GOOGLE_CLOUD_LOCATION ) { @@ -74,5 +74,5 @@ async function generateContent( // [END googlegenaisdk_textgen_code_with_pdf] module.exports = { - generateContent, + generateText, }; diff --git a/genai/text-generation/textgen-config-with-txt.js b/genai/text-generation/textgen-config-with-txt.js new file mode 100644 index 0000000000..46d4e7b1c4 --- /dev/null +++ b/genai/text-generation/textgen-config-with-txt.js @@ -0,0 +1,66 @@ +// 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 client = 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 client.models.generateContent({ + model: 'gemini-2.5-flash', + contents: 'Why is the sky blue?', + config: config, + }); + + console.log(response.text); + + // Example response: + // { + // "explanation": "The sky appears blue due to a phenomenon called Rayleigh scattering. When ... + // } + + return response.text; +} + +// [END googlegenaisdk_textgen_config_with_txt] + +module.exports = { + generateContent, +}; diff --git a/genai/text-generation/textgen-with-mute-video.js b/genai/text-generation/textgen-with-mute-video.js new file mode 100644 index 0000000000..f2a7805064 --- /dev/null +++ b/genai/text-generation/textgen-with-mute-video.js @@ -0,0 +1,65 @@ +// 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_mute_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 generateText( + projectId = GOOGLE_CLOUD_PROJECT, + location = GOOGLE_CLOUD_LOCATION +) { + const client = new GoogleGenAI({ + vertexai: true, + project: projectId, + location: location, + }); + + const response = await client.models.generateContent({ + model: 'gemini-2.5-flash', + contents: [ + { + role: 'user', + parts: [ + { + fileData: { + mimeType: 'video/mp4', + fileUri: + 'gs://cloud-samples-data/generative-ai/video/ad_copy_from_video.mp4', + }, + }, + { + text: 'What is in the video?', + }, + ], + }, + ], + }); + + console.log(response.text); + + // Example response: + // The video shows several people surfing in an ocean with a coastline in the background. The camera ... + + return response.text; +} +// [END googlegenaisdk_textgen_with_mute_video] + +module.exports = { + generateText, +};