Skip to content

Commit e9b3b27

Browse files
author
Guiners
committed
adding samples
1 parent 62dbb27 commit e9b3b27

11 files changed

+605
-9
lines changed
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
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_boundingbox_with_txt_img]
18+
const {GoogleGenAI} = require('@google/genai');
19+
20+
const {createCanvas, loadImage} = require('canvas');
21+
const fetch = require('node-fetch');
22+
const fs = require('fs');
23+
24+
const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT;
25+
const GOOGLE_CLOUD_LOCATION = process.env.GOOGLE_CLOUD_LOCATION || 'global';
26+
27+
async function fetchImageAsBase64(uri) {
28+
const response = await fetch(uri);
29+
const buffer = await response.buffer();
30+
return buffer.toString('base64');
31+
}
32+
33+
async function plotBoundingBoxes(imageUri, boundingBoxes) {
34+
console.log('Creating bounding boxes');
35+
const image = await loadImage(imageUri);
36+
const canvas = createCanvas(image.width, image.height);
37+
const ctx = canvas.getContext('2d');
38+
39+
ctx.drawImage(image, 0, 0);
40+
41+
const colors = ['red', 'blue', 'green', 'orange'];
42+
43+
boundingBoxes.forEach((bbox, i) => {
44+
const [yMin, xMin, yMax, xMax] = bbox.box_2d;
45+
46+
const absYMin = Math.floor((yMin / 1000) * image.height);
47+
const absXMin = Math.floor((xMin / 1000) * image.width);
48+
const absYMax = Math.floor((yMax / 1000) * image.height);
49+
const absXMax = Math.floor((xMax / 1000) * image.width);
50+
51+
ctx.strokeStyle = colors[i % colors.length];
52+
ctx.lineWidth = 4;
53+
ctx.strokeRect(absXMin, absYMin, absXMax - absXMin, absYMax - absYMin);
54+
55+
ctx.fillStyle = colors[i % colors.length];
56+
ctx.font = '20px Arial';
57+
ctx.fillText(bbox.label, absXMin + 8, absYMin + 20);
58+
});
59+
60+
fs.writeFileSync('output.png', canvas.toBuffer('image/png'));
61+
console.log('Saved output to file: output.png');
62+
}
63+
64+
async function createBoundingBox(
65+
projectId = GOOGLE_CLOUD_PROJECT,
66+
location = GOOGLE_CLOUD_LOCATION
67+
) {
68+
const client = new GoogleGenAI({
69+
vertexai: true,
70+
project: projectId,
71+
location: location,
72+
});
73+
74+
const systemInstruction = `
75+
Return bounding boxes as an array with labels.
76+
Never return masks. Limit to 25 objects.
77+
If an object is present multiple times, give each object a unique label
78+
according to its distinct characteristics (colors, size, position, etc.).
79+
`;
80+
81+
const safetySettings = [
82+
{
83+
category: 'HARM_CATEGORY_DANGEROUS_CONTENT',
84+
threshold: 'BLOCK_ONLY_HIGH',
85+
},
86+
];
87+
88+
const imageUri =
89+
'https://storage.googleapis.com/generativeai-downloads/images/socks.jpg';
90+
const base64Image = await fetchImageAsBase64(imageUri);
91+
92+
const boundingBoxSchema = {
93+
type: 'ARRAY',
94+
description: 'List of bounding boxes for detected objects',
95+
items: {
96+
type: 'OBJECT',
97+
title: 'BoundingBox',
98+
description: 'Represents a bounding box with coordinates and label',
99+
properties: {
100+
box_2d: {
101+
type: 'ARRAY',
102+
description:
103+
'Bounding box coordinates in format [y_min, x_min, y_max, x_max]',
104+
items: {
105+
type: 'INTEGER',
106+
format: 'int32',
107+
},
108+
minItems: '4',
109+
maxItems: '4',
110+
},
111+
label: {
112+
type: 'STRING',
113+
description: 'Label describing the object within the bounding box',
114+
},
115+
},
116+
required: ['box_2d', 'label'],
117+
},
118+
};
119+
120+
const response = await client.models.generateContent({
121+
model: 'gemini-2.5-flash',
122+
contents: [
123+
{
124+
role: 'user',
125+
parts: [
126+
{
127+
text: 'Output the positions of the socks with a face. Label according to position in the image.',
128+
},
129+
{
130+
inlineData: {
131+
data: base64Image,
132+
mimeType: 'image/jpeg',
133+
},
134+
},
135+
],
136+
},
137+
],
138+
config: {
139+
systemInstruction: systemInstruction,
140+
safetySettings: safetySettings,
141+
responseMimeType: 'application/json',
142+
temperature: 0.5,
143+
responseSchema: boundingBoxSchema,
144+
},
145+
});
146+
147+
const candidate = response.candidates[0].content.parts[0].text;
148+
const boundingBoxes = JSON.parse(candidate);
149+
150+
console.log('Bounding boxes:', boundingBoxes);
151+
152+
await plotBoundingBoxes(imageUri, boundingBoxes);
153+
return boundingBoxes;
154+
}
155+
// [END googlegenaisdk_boundingbox_with_txt_img]
156+
157+
module.exports = {
158+
createBoundingBox,
159+
};
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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_embeddings_docretrieval_with_txt]
18+
const {GoogleGenAI} = require('@google/genai');
19+
20+
const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT;
21+
22+
async function generateEmbeddingsForRetrieval(
23+
projectId = GOOGLE_CLOUD_PROJECT
24+
) {
25+
const client = new GoogleGenAI(projectId);
26+
27+
const prompt = [
28+
"How do I get a driver's license/learner's permit?",
29+
"How long is my driver's license valid for?",
30+
"Driver's knowledge test study guide",
31+
];
32+
33+
const response = await client.models.embedContent({
34+
model: 'gemini-embedding-001',
35+
contents: prompt,
36+
config: {
37+
taskType: 'RETRIEVAL_DOCUMENT', // Optional
38+
outputDimensionality: 3072, // Optional
39+
title: "Driver's License", // Optional
40+
},
41+
});
42+
43+
console.log(response);
44+
45+
// Example response:
46+
// embeddings=[ContentEmbedding(values=[-0.06302902102470398, 0.00928034819662571, 0.014716853387653828, -0.028747491538524628, ... ],
47+
// statistics=ContentEmbeddingStatistics(truncated=False, token_count=13.0))]
48+
// metadata=EmbedContentMetadata(billable_character_count=112)
49+
50+
return response;
51+
}
52+
// [END googlegenaisdk_embeddings_docretrieval_with_txt]
53+
54+
module.exports = {
55+
generateEmbeddingsForRetrieval,
56+
};
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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_vertexai_express_mode]
18+
const {GoogleGenAI} = require('@google/genai');
19+
const API_KEY = 'PUT HERE YOUR API KEY';
20+
21+
async function generateWithApiKey(apiKey = API_KEY) {
22+
const client = new GoogleGenAI({
23+
vertexai: true,
24+
apiKey: apiKey,
25+
});
26+
27+
const response = await client.models.generateContentStream({
28+
model: 'gemini-2.5-flash',
29+
contents: 'Explain bubble sort to me.',
30+
});
31+
32+
console.log(response.text);
33+
34+
// Example response:
35+
// Bubble Sort is a simple sorting algorithm that repeatedly steps through the list
36+
37+
return response;
38+
}
39+
// [END googlegenaisdk_vertexai_express_mode]
40+
41+
module.exports = {
42+
generateWithApiKey,
43+
};

genai/package.json

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,18 @@
1313
"test": "c8 mocha -p -j 2 --timeout 2400000 test/*.test.js test/**/*.test.js"
1414
},
1515
"dependencies": {
16-
"@google/genai": "1.20.0",
16+
"@google/genai": "^0.13.0",
1717
"axios": "^1.6.2",
18-
"google-auth-library": "^10.3.0",
19-
"luxon": "^3.7.1",
20-
"proxyquire": "^2.1.3",
21-
"node-fetch": "^3.3.2",
22-
"openai": "^5.19.1",
2318
"supertest": "^7.0.0"
2419
},
2520
"devDependencies": {
2621
"c8": "^10.0.0",
2722
"chai": "^4.5.0",
2823
"mocha": "^10.0.0",
29-
"node-fetch": "^3.3.2",
30-
"proxyquire": "^2.1.3",
3124
"sinon": "^18.0.0",
32-
"uuid": "^10.0.0"
25+
"uuid": "^10.0.0",
26+
"proxyquire": "^2.1.3",
27+
"canvas": "^3.1.0",
28+
"node-fetch": "^2.7.0"
3329
}
3430
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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_provisionedthroughput_with_txt]
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 generateWithProvisionedThroughput(
24+
projectId = GOOGLE_CLOUD_PROJECT,
25+
location = GOOGLE_CLOUD_LOCATION
26+
) {
27+
const client = new GoogleGenAI({
28+
vertexai: true,
29+
project: projectId,
30+
location: location,
31+
httpOptions: {
32+
apiVersion: 'v1',
33+
headers: {
34+
// Options:
35+
// - "dedicated": Use Provisioned Throughput
36+
// - "shared": Use pay-as-you-go
37+
// https://cloud.google.com/vertex-ai/generative-ai/docs/use-provisioned-throughput
38+
'X-Vertex-AI-LLM-Request-Type': 'shared',
39+
},
40+
},
41+
});
42+
43+
const response = await client.models.generateContent({
44+
model: 'gemini-2.5-flash',
45+
contents: 'How does AI work?',
46+
});
47+
48+
console.log(response.text);
49+
50+
// Example response:
51+
// Okay, let's break down how AI works. It's a broad field, so I'll focus on the ...
52+
// Here's a simplified overview:
53+
// ...
54+
55+
return response.text;
56+
}
57+
58+
// [END googlegenaisdk_provisionedthroughput_with_txt]
59+
60+
module.exports = {
61+
generateWithProvisionedThroughput,
62+
};

0 commit comments

Comments
 (0)