Skip to content

Commit 40758cc

Browse files
GuinersGuiners
andauthored
feat(genai): Sample/textgen part3 (#4170)
* adding samples, test, lints * adding samples, test, lints * adding samples, test, lints * adding samples, test, lints * adding samples, test, lints --------- Co-authored-by: Guiners <[email protected]>
1 parent 03ab11d commit 40758cc

File tree

6 files changed

+227
-0
lines changed

6 files changed

+227
-0
lines changed

genai/test/test-data/latte.jpg

52 KB
Loading

genai/test/test-data/scones.jpg

384 KB
Loading
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
const {assert} = require('chai');
18+
const {describe, it} = require('mocha');
19+
20+
const projectId = process.env.CAIP_PROJECT_ID;
21+
const sample = require('../text-generation/textgen-code-with-pdf.js');
22+
23+
describe('textgen-code-with-pdf', () => {
24+
it('should generate text content from a pdf', async function () {
25+
this.timeout(100000);
26+
const output = await sample.generateContent(projectId);
27+
assert(output.length > 0);
28+
});
29+
});
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
const {assert} = require('chai');
18+
const {describe, it} = require('mocha');
19+
20+
const projectId = process.env.CAIP_PROJECT_ID;
21+
const location = process.env.GOOGLE_CLOUD_LOCATION || 'global';
22+
23+
const sample = require('../text-generation/textgen-with-multi-local-img.js');
24+
25+
describe('textgen-with-multi-local-img', () => {
26+
it('should generate text content from multiple images', async function () {
27+
this.timeout(100000);
28+
const imagePath1 = './test/test-data/latte.jpg';
29+
const imagePath2 = './test/test-data/scones.jpg';
30+
const output = await sample.generateContent(
31+
projectId,
32+
location,
33+
imagePath1,
34+
imagePath2
35+
);
36+
assert(output.length > 0);
37+
});
38+
});
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
// [START googlegenaisdk_textgen_code_with_pdf]
18+
const {GoogleGenAI} = require('@google/genai');
19+
20+
const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT;
21+
const GOOGLE_CLOUD_LOCATION = process.env.GOOGLE_CLOUD_LOCATION || 'global';
22+
23+
async function generateContent(
24+
projectId = GOOGLE_CLOUD_PROJECT,
25+
location = GOOGLE_CLOUD_LOCATION
26+
) {
27+
const ai = new GoogleGenAI({
28+
vertexai: true,
29+
project: projectId,
30+
location: location,
31+
});
32+
33+
const contents = [
34+
{
35+
role: 'user',
36+
parts: [
37+
{text: 'Convert this python code to use Google Python Style Guide.'},
38+
{
39+
fileData: {
40+
fileUri:
41+
'https://storage.googleapis.com/cloud-samples-data/generative-ai/text/inefficient_fibonacci_series_python_code.pdf',
42+
mimeType: 'application/pdf',
43+
},
44+
},
45+
],
46+
},
47+
];
48+
49+
const response = await ai.models.generateContent({
50+
model: 'gemini-2.5-flash',
51+
contents: contents,
52+
});
53+
54+
console.log(response.text);
55+
56+
return response.text;
57+
}
58+
// Example response:
59+
// Here's the Python code converted to adhere to the Google Python Style Guide, along with explanations for the changes:
60+
//
61+
// ```python
62+
// """Calculates the Fibonacci sequence up to n numbers.
63+
//
64+
// This module provides a function to generate a Fibonacci sequence,
65+
// demonstrating adherence to the Google Python Style Guide.
66+
// """
67+
//
68+
// def fibonacci(n: int) -> list[int]:
69+
// """Calculates the Fibonacci sequence up to n numbers.
70+
//
71+
// This function generates the first 'n' terms of the Fibonacci sequence,
72+
// starting with 0, 1, 1, 2...
73+
// ...
74+
// [END googlegenaisdk_textgen_code_with_pdf]
75+
76+
module.exports = {
77+
generateContent,
78+
};
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
// [START googlegenaisdk_textgen_with_multi_local_img]
18+
const {GoogleGenAI} = require('@google/genai');
19+
const fs = require('fs');
20+
21+
const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT;
22+
const GOOGLE_CLOUD_LOCATION = process.env.GOOGLE_CLOUD_LOCATION || 'global';
23+
24+
function loadImageAsBase64(path) {
25+
const bytes = fs.readFileSync(path);
26+
return bytes.toString('base64');
27+
}
28+
29+
async function generateContent(
30+
projectId = GOOGLE_CLOUD_PROJECT,
31+
location = GOOGLE_CLOUD_LOCATION,
32+
imagePath1,
33+
imagePath2
34+
) {
35+
const ai = new GoogleGenAI({
36+
vertexai: true,
37+
project: projectId,
38+
location: location,
39+
});
40+
41+
// TODO(Developer): Update the below file paths to your images
42+
const image1 = loadImageAsBase64(imagePath1);
43+
const image2 = loadImageAsBase64(imagePath2);
44+
45+
const response = await ai.models.generateContent({
46+
model: 'gemini-2.5-flash',
47+
contents: [
48+
{
49+
role: 'user',
50+
parts: [
51+
{
52+
text: 'Generate a list of all the objects contained in both images.',
53+
},
54+
{
55+
inlineData: {
56+
data: image1,
57+
mimeType: 'image/jpeg',
58+
},
59+
},
60+
{
61+
inlineData: {
62+
data: image2,
63+
mimeType: 'image/jpeg',
64+
},
65+
},
66+
],
67+
},
68+
],
69+
});
70+
71+
console.log(response.text);
72+
73+
return response.text;
74+
}
75+
// Example response:
76+
// Okay, here's a jingle combining the elements of both sets of images, focusing on ...
77+
// ...
78+
// [END googlegenaisdk_textgen_with_multi_local_img]
79+
80+
module.exports = {
81+
generateContent,
82+
};

0 commit comments

Comments
 (0)