diff --git a/genai/test/test-data/latte.jpg b/genai/test/test-data/latte.jpg new file mode 100644 index 0000000000..e942ca6230 Binary files /dev/null and b/genai/test/test-data/latte.jpg differ diff --git a/genai/test/test-data/scones.jpg b/genai/test/test-data/scones.jpg new file mode 100644 index 0000000000..b5ee1b0707 Binary files /dev/null and b/genai/test/test-data/scones.jpg differ diff --git a/genai/test/textgen-code-with-pdf.test.js b/genai/test/textgen-code-with-pdf.test.js new file mode 100644 index 0000000000..d4a11130a1 --- /dev/null +++ b/genai/test/textgen-code-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-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); + assert(output.length > 0); + }); +}); diff --git a/genai/test/textgen-with-multi-local-img.test.js b/genai/test/textgen-with-multi-local-img.test.js new file mode 100644 index 0000000000..6fae3d4d3c --- /dev/null +++ b/genai/test/textgen-with-multi-local-img.test.js @@ -0,0 +1,38 @@ +// 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 location = process.env.GOOGLE_CLOUD_LOCATION || 'global'; + +const sample = require('../text-generation/textgen-with-multi-local-img.js'); + +describe('textgen-with-multi-local-img', () => { + it('should generate text content from multiple images', async function () { + this.timeout(100000); + const imagePath1 = './test/test-data/latte.jpg'; + const imagePath2 = './test/test-data/scones.jpg'; + const output = await sample.generateContent( + projectId, + location, + imagePath1, + imagePath2 + ); + assert(output.length > 0); + }); +}); diff --git a/genai/text-generation/textgen-code-with-pdf.js b/genai/text-generation/textgen-code-with-pdf.js new file mode 100644 index 0000000000..762f4bff19 --- /dev/null +++ b/genai/text-generation/textgen-code-with-pdf.js @@ -0,0 +1,78 @@ +// 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_code_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 generateContent( + projectId = GOOGLE_CLOUD_PROJECT, + location = GOOGLE_CLOUD_LOCATION +) { + const ai = new GoogleGenAI({ + vertexai: true, + project: projectId, + location: location, + }); + + const contents = [ + { + role: 'user', + parts: [ + {text: 'Convert this python code to use Google Python Style Guide.'}, + { + fileData: { + fileUri: + 'https://storage.googleapis.com/cloud-samples-data/generative-ai/text/inefficient_fibonacci_series_python_code.pdf', + mimeType: 'application/pdf', + }, + }, + ], + }, + ]; + + const response = await ai.models.generateContent({ + model: 'gemini-2.5-flash', + contents: contents, + }); + + console.log(response.text); + + 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 = { + generateContent, +}; diff --git a/genai/text-generation/textgen-with-multi-local-img.js b/genai/text-generation/textgen-with-multi-local-img.js new file mode 100644 index 0000000000..1cbd904013 --- /dev/null +++ b/genai/text-generation/textgen-with-multi-local-img.js @@ -0,0 +1,82 @@ +// 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_local_img] +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'; + +function loadImageAsBase64(path) { + const bytes = fs.readFileSync(path); + return bytes.toString('base64'); +} + +async function generateContent( + projectId = GOOGLE_CLOUD_PROJECT, + location = GOOGLE_CLOUD_LOCATION, + imagePath1, + imagePath2 +) { + const ai = new GoogleGenAI({ + vertexai: true, + project: projectId, + location: location, + }); + + // TODO(Developer): Update the below file paths to your images + const image1 = loadImageAsBase64(imagePath1); + const image2 = loadImageAsBase64(imagePath2); + + const response = await ai.models.generateContent({ + model: 'gemini-2.5-flash', + contents: [ + { + role: 'user', + parts: [ + { + text: 'Generate a list of all the objects contained in both images.', + }, + { + inlineData: { + data: image1, + mimeType: 'image/jpeg', + }, + }, + { + inlineData: { + data: image2, + mimeType: 'image/jpeg', + }, + }, + ], + }, + ], + }); + + console.log(response.text); + + 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 = { + generateContent, +};