Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added genai/test/test-data/latte.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added genai/test/test-data/scones.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
29 changes: 29 additions & 0 deletions genai/test/textgen-code-with-pdf.test.js
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-code-with-pdf.js');

describe('textgen-code-with-pdf', async () => {
Comment thread
Guiners marked this conversation as resolved.
Outdated
it('should generate text content from a pdf', async function () {
this.timeout(100000);
const output = await sample.generateContent(projectId);
assert(output.length > 0);
});
});
38 changes: 38 additions & 0 deletions genai/test/textgen-with-multi-local-img.test.js
Original file line number Diff line number Diff line change
@@ -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', async () => {
Comment thread
Guiners marked this conversation as resolved.
Outdated
it('should generate text content from a pdf', async function () {
Comment thread
Guiners marked this conversation as resolved.
Outdated
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);
});
});
78 changes: 78 additions & 0 deletions genai/text-generation/textgen-code-with-pdf.js
Original file line number Diff line number Diff line change
@@ -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,
};
82 changes: 82 additions & 0 deletions genai/text-generation/textgen-with-multi-local-img.js
Original file line number Diff line number Diff line change
@@ -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,
};