diff --git a/genai/test-data/describe_video_content.mp4 b/genai/test-data/describe_video_content.mp4 new file mode 100644 index 0000000000..93176ae76f Binary files /dev/null and b/genai/test-data/describe_video_content.mp4 differ diff --git a/genai/test/textgen-transcript-with-gcs-audio.test.js b/genai/test/textgen-transcript-with-gcs-audio.test.js new file mode 100644 index 0000000000..b1b7240bc9 --- /dev/null +++ b/genai/test/textgen-transcript-with-gcs-audio.test.js @@ -0,0 +1,32 @@ +// 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-transcript-with-gcs-audio.js'); +const {delay} = require('./util'); + +describe('textgen-transcript-with-gcs-audio', async () => { + it('should generate text content from gsc audio with transcript', async function () { + this.timeout(180000); + this.retries(4); + await delay(this.test); + const output = await sample.generateText(projectId); + assert(output.length > 0); + }); +}); diff --git a/genai/test/textgen-with-gcs-audio.test.js b/genai/test/textgen-with-gcs-audio.test.js new file mode 100644 index 0000000000..4a86977bcd --- /dev/null +++ b/genai/test/textgen-with-gcs-audio.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-gcs-audio'); + +describe('textgen-with-gcs-audio', async () => { + it('should generate text content from gsc audio', async function () { + this.timeout(300000); + const output = await sample.generateText(projectId); + assert(output.length > 0); + }); +}); diff --git a/genai/test/textgen-with-local-video.test.js b/genai/test/textgen-with-local-video.test.js new file mode 100644 index 0000000000..4a315dbe6d --- /dev/null +++ b/genai/test/textgen-with-local-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-local-video.js'); + +describe('textgen-with-local-video', async () => { + it('should generate text content from local video', async function () { + this.timeout(30000); + const output = await sample.generateText(projectId); + assert(output.length > 0); + }); +}); diff --git a/genai/test/textgen-with-pdf.test.js b/genai/test/textgen-with-pdf.test.js new file mode 100644 index 0000000000..9651bb465a --- /dev/null +++ b/genai/test/textgen-with-pdf.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-pdf.js'); + +describe('textgen-with-pdf', async () => { + it('should generate text content from pdf', async function () { + this.timeout(30000); + const output = await sample.generateText(projectId); + assert(output.length > 0); + }); +}); diff --git a/genai/test/textgen-with-youtube-video.test.js b/genai/test/textgen-with-youtube-video.test.js new file mode 100644 index 0000000000..4da0174f9a --- /dev/null +++ b/genai/test/textgen-with-youtube-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-youtube-video'); + +describe('textgen-with-youtube-video', async () => { + it('should generate text content from yt video', async function () { + this.timeout(300000); + const output = await sample.generateText(projectId); + assert(output.length > 0); + }); +}); diff --git a/genai/text-generation/textgen-code-with-pdf.js b/genai/text-generation/textgen-code-with-pdf.js index 7211d177bc..22a29d0393 100644 --- a/genai/text-generation/textgen-code-with-pdf.js +++ b/genai/text-generation/textgen-code-with-pdf.js @@ -53,24 +53,26 @@ async function generateText( console.log(response.text); + // Example response: + // Here's the Python code converted to adhere to the Google Python Style Guide, along with explanations for the changes: + // + // ```python + // """Calculates the Fibonacci sequence up to n numbers. + // + // This module provides a function to generate a Fibonacci sequence, + // demonstrating adherence to the Google Python Style Guide. + // """ + // + // def fibonacci(n: int) -> list[int]: + // """Calculates the Fibonacci sequence up to n numbers. + // + // This function generates the first 'n' terms of the Fibonacci sequence, + // starting with 0, 1, 1, 2... + // ... + return response.text; } -// Example response: -// Here's the Python code converted to adhere to the Google Python Style Guide, along with explanations for the changes: -// -// ```python -// """Calculates the Fibonacci sequence up to n numbers. -// -// This module provides a function to generate a Fibonacci sequence, -// demonstrating adherence to the Google Python Style Guide. -// """ -// -// def fibonacci(n: int) -> list[int]: -// """Calculates the Fibonacci sequence up to n numbers. -// -// This function generates the first 'n' terms of the Fibonacci sequence, -// starting with 0, 1, 1, 2... -// ... + // [END googlegenaisdk_textgen_code_with_pdf] module.exports = { diff --git a/genai/text-generation/textgen-transcript-with-gcs-audio.js b/genai/text-generation/textgen-transcript-with-gcs-audio.js new file mode 100644 index 0000000000..4c6daad893 --- /dev/null +++ b/genai/text-generation/textgen-transcript-with-gcs-audio.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_textgen_transcript_with_gcs_audio] +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 prompt = `Transcribe the interview, in the format of timecode, speaker, caption. + Use speaker A, speaker B, etc. to identify speakers.`; + + const response = await client.models.generateContent({ + model: 'gemini-2.5-flash', + contents: [ + {text: prompt}, + { + fileData: { + fileUri: 'gs://cloud-samples-data/generative-ai/audio/pixel.mp3', + mimeType: 'audio/mpeg', + }, + }, + ], + // Required to enable timestamp understanding for audio-only files + config: { + audioTimestamp: true, + }, + }); + + console.log(response.text); + + // Example response: + // [00:00:00] **Speaker A:** your devices are getting better over time. And so ... + // [00:00:14] **Speaker B:** Welcome to the Made by Google podcast where we meet ... + // [00:00:20] **Speaker B:** Here's your host, Rasheed Finch. + // [00:00:23] **Speaker C:** Today we're talking to Aisha Sharif and DeCarlos Love. ... + // ... + + return response.text; +} + +// [END googlegenaisdk_textgen_transcript_with_gcs_audio] + +module.exports = { + generateText, +}; diff --git a/genai/text-generation/textgen-with-gcs-audio.js b/genai/text-generation/textgen-with-gcs-audio.js new file mode 100644 index 0000000000..67a28619fa --- /dev/null +++ b/genai/text-generation/textgen-with-gcs-audio.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_with_gcs_audio] +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 prompt = + 'Provide a concise summary of the main points in the audio file.'; + + const response = await client.models.generateContent({ + model: 'gemini-2.5-flash', + contents: [ + { + fileData: { + fileUri: 'gs://cloud-samples-data/generative-ai/audio/pixel.mp3', + mimeType: 'audio/mpeg', + }, + }, + {text: prompt}, + ], + }); + + console.log(response.text); + + // Example response: + // Here's a summary of the main points from the audio file: + // The Made by Google podcast discusses the Pixel feature drops with product managers Aisha Sheriff and De Carlos Love. The key idea is that devices should improve over time, with a connected experience across phones, watches, earbuds, and tablets. + + return response.text; +} +// [END googlegenaisdk_textgen_with_gcs_audio] + +module.exports = { + generateText, +}; diff --git a/genai/text-generation/textgen-with-local-video.js b/genai/text-generation/textgen-with-local-video.js new file mode 100644 index 0000000000..d405bbbaef --- /dev/null +++ b/genai/text-generation/textgen-with-local-video.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_local_video] +const {GoogleGenAI} = require('@google/genai'); +const fs = require('fs'); + +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 videoContent = fs.readFileSync('test-data/describe_video_content.mp4'); + + const response = await client.models.generateContent({ + model: 'gemini-2.5-flash', + contents: [ + {text: 'hello-world'}, + { + inlineData: { + data: videoContent.toString('base64'), + mimeType: 'video/mp4', + }, + }, + {text: 'Write a short and engaging blog post based on this video.'}, + ], + }); + + console.log(response.text); + + // Example response: + // Okay, here's a short and engaging blog post based on the climbing video: + // **Title: Conquering the Wall: A Glimpse into the World of Indoor Climbing** + // ... + + return response.text; +} + +// [END googlegenaisdk_textgen_with_local_video] + +module.exports = { + generateText, +}; diff --git a/genai/text-generation/textgen-with-multi-local-img.js b/genai/text-generation/textgen-with-multi-local-img.js index 1cbd904013..855c875222 100644 --- a/genai/text-generation/textgen-with-multi-local-img.js +++ b/genai/text-generation/textgen-with-multi-local-img.js @@ -70,11 +70,13 @@ async function generateContent( console.log(response.text); + // Example response: + // Okay, here's a jingle combining the elements of both sets of images, focusing on ... + // ... + return response.text; } -// Example response: -// Okay, here's a jingle combining the elements of both sets of images, focusing on ... -// ... + // [END googlegenaisdk_textgen_with_multi_local_img] module.exports = { diff --git a/genai/text-generation/textgen-with-pdf.js b/genai/text-generation/textgen-with-pdf.js new file mode 100644 index 0000000000..14a0f87fb1 --- /dev/null +++ b/genai/text-generation/textgen-with-pdf.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_pdf] +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 prompt = `You are a highly skilled document summarization specialist. + Your task is to provide a concise executive summary of no more than 300 words. + Please summarize the given document for a general audience.`; + + const pdfFile = { + fileData: { + fileUri: 'gs://cloud-samples-data/generative-ai/pdf/1706.03762v7.pdf', + mimeType: 'application/pdf', + }, + }; + + const response = await client.models.generateContent({ + model: 'gemini-2.5-flash', + contents: [pdfFile, prompt], + }); + + console.log(response.text); + + // Example response: + // Here is a summary of the document in 300 words. + // The paper introduces the Transformer, a novel neural network architecture for + // sequence transduction tasks like machine translation. Unlike existing models that rely on recurrent or + // convolutional layers, the Transformer is based entirely on attention mechanisms. + // ... + + return response.text; +} + +// [END googlegenaisdk_textgen_with_pdf] + +module.exports = { + generateText, +}; diff --git a/genai/text-generation/textgen-with-youtube-video.js b/genai/text-generation/textgen-with-youtube-video.js new file mode 100644 index 0000000000..f037417b30 --- /dev/null +++ b/genai/text-generation/textgen-with-youtube-video.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_with_youtube_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 prompt = 'Write a short and engaging blog post based on this video.'; + + const ytVideo = { + fileData: { + fileUri: 'https://www.youtube.com/watch?v=3KtWfp0UopM', + mimeType: 'video/mp4', + }, + }; + + const response = await client.models.generateContent({ + model: 'gemini-2.5-flash', + contents: [ytVideo, prompt], + }); + + console.log(response.text); + + // Example response: + // Here's a short blog post based on the video provided: + // **Google Turns 25: A Quarter Century of Search!** + // ... + + return response.text; +} + +// [END googlegenaisdk_textgen_with_youtube_video] + +module.exports = { + generateText, +};