-
Notifications
You must be signed in to change notification settings - Fork 2k
feat(genai): Sample/rest of samples #4195
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
7b72387
88ff0ce
7aa2a16
a5d6333
7cc334d
e20d7fd
9577b8e
79f65d5
23f5a05
42b4fc4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,159 @@ | ||||||||||
| // 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_boundingbox_with_txt_img] | ||||||||||
| const {GoogleGenAI} = require('@google/genai'); | ||||||||||
|
|
||||||||||
| const {createCanvas, loadImage} = require('canvas'); | ||||||||||
| const fetch = require('node-fetch'); | ||||||||||
| 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 fetchImageAsBase64(uri) { | ||||||||||
| const response = await fetch(uri); | ||||||||||
| const buffer = await response.buffer(); | ||||||||||
| return buffer.toString('base64'); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| async function plotBoundingBoxes(imageUri, boundingBoxes) { | ||||||||||
| console.log('Creating bounding boxes'); | ||||||||||
| const image = await loadImage(imageUri); | ||||||||||
| const canvas = createCanvas(image.width, image.height); | ||||||||||
| const ctx = canvas.getContext('2d'); | ||||||||||
|
|
||||||||||
| ctx.drawImage(image, 0, 0); | ||||||||||
|
|
||||||||||
| const colors = ['red', 'blue', 'green', 'orange']; | ||||||||||
|
|
||||||||||
| boundingBoxes.forEach((bbox, i) => { | ||||||||||
| const [yMin, xMin, yMax, xMax] = bbox.box_2d; | ||||||||||
|
|
||||||||||
| const absYMin = Math.floor((yMin / 1000) * image.height); | ||||||||||
| const absXMin = Math.floor((xMin / 1000) * image.width); | ||||||||||
| const absYMax = Math.floor((yMax / 1000) * image.height); | ||||||||||
| const absXMax = Math.floor((xMax / 1000) * image.width); | ||||||||||
|
|
||||||||||
| ctx.strokeStyle = colors[i % colors.length]; | ||||||||||
| ctx.lineWidth = 4; | ||||||||||
| ctx.strokeRect(absXMin, absYMin, absXMax - absXMin, absYMax - absYMin); | ||||||||||
|
|
||||||||||
| ctx.fillStyle = colors[i % colors.length]; | ||||||||||
| ctx.font = '20px Arial'; | ||||||||||
| ctx.fillText(bbox.label, absXMin + 8, absYMin + 20); | ||||||||||
| }); | ||||||||||
|
|
||||||||||
| fs.writeFileSync('output.png', canvas.toBuffer('image/png')); | ||||||||||
| console.log('Saved output to file: output.png'); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| async function createBoundingBox( | ||||||||||
| projectId = GOOGLE_CLOUD_PROJECT, | ||||||||||
| location = GOOGLE_CLOUD_LOCATION | ||||||||||
| ) { | ||||||||||
| const client = new GoogleGenAI({ | ||||||||||
| vertexai: true, | ||||||||||
| project: projectId, | ||||||||||
| location: location, | ||||||||||
| }); | ||||||||||
|
|
||||||||||
| const systemInstruction = ` | ||||||||||
| Return bounding boxes as an array with labels. | ||||||||||
| Never return masks. Limit to 25 objects. | ||||||||||
| If an object is present multiple times, give each object a unique label | ||||||||||
| according to its distinct characteristics (colors, size, position, etc). | ||||||||||
| `; | ||||||||||
|
|
||||||||||
| const safetySettings = [ | ||||||||||
| { | ||||||||||
| category: 'HARM_CATEGORY_DANGEROUS_CONTENT', | ||||||||||
| threshold: 'BLOCK_ONLY_HIGH', | ||||||||||
| }, | ||||||||||
| ]; | ||||||||||
|
|
||||||||||
| const imageUri = | ||||||||||
| 'https://storage.googleapis.com/generativeai-downloads/images/socks.jpg'; | ||||||||||
| const base64Image = await fetchImageAsBase64(imageUri); | ||||||||||
|
|
||||||||||
| const boundingBoxSchema = { | ||||||||||
| type: 'ARRAY', | ||||||||||
| description: 'List of bounding boxes for detected objects', | ||||||||||
| items: { | ||||||||||
| type: 'OBJECT', | ||||||||||
| title: 'BoundingBox', | ||||||||||
| description: 'Represents a bounding box with coordinates and label', | ||||||||||
| properties: { | ||||||||||
| box_2d: { | ||||||||||
| type: 'ARRAY', | ||||||||||
| description: | ||||||||||
| 'Bounding box coordinates in format [y_min, x_min, y_max, x_max]', | ||||||||||
| items: { | ||||||||||
| type: 'INTEGER', | ||||||||||
| format: 'int32', | ||||||||||
| }, | ||||||||||
| minItems: '4', | ||||||||||
| maxItems: '4', | ||||||||||
|
Comment on lines
+108
to
+109
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||||||
| }, | ||||||||||
| label: { | ||||||||||
| type: 'STRING', | ||||||||||
| description: 'Label describing the object within the bounding box', | ||||||||||
| }, | ||||||||||
| }, | ||||||||||
| required: ['box_2d', 'label'], | ||||||||||
| }, | ||||||||||
| }; | ||||||||||
|
|
||||||||||
| const response = await client.models.generateContent({ | ||||||||||
| model: 'gemini-2.5-flash', | ||||||||||
| contents: [ | ||||||||||
| { | ||||||||||
| role: 'user', | ||||||||||
| parts: [ | ||||||||||
| { | ||||||||||
| text: 'Output the positions of the socks with a face. Label according to position in the image.', | ||||||||||
| }, | ||||||||||
| { | ||||||||||
| inlineData: { | ||||||||||
| data: base64Image, | ||||||||||
| mimeType: 'image/jpeg', | ||||||||||
| }, | ||||||||||
| }, | ||||||||||
| ], | ||||||||||
| }, | ||||||||||
| ], | ||||||||||
| config: { | ||||||||||
| systemInstruction: systemInstruction, | ||||||||||
| safetySettings: safetySettings, | ||||||||||
| responseMimeType: 'application/json', | ||||||||||
| temperature: 0.5, | ||||||||||
| responseSchema: boundingBoxSchema, | ||||||||||
| }, | ||||||||||
| }); | ||||||||||
|
|
||||||||||
| const candidate = response.candidates[0].content.parts[0].text; | ||||||||||
| const boundingBoxes = JSON.parse(candidate); | ||||||||||
|
Comment on lines
+147
to
+148
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Accessing nested properties directly can lead to a
Suggested change
|
||||||||||
|
|
||||||||||
| console.log('Bounding boxes:', boundingBoxes); | ||||||||||
|
|
||||||||||
| await plotBoundingBoxes(imageUri, boundingBoxes); | ||||||||||
| return boundingBoxes; | ||||||||||
| } | ||||||||||
| // [END googlegenaisdk_boundingbox_with_txt_img] | ||||||||||
|
|
||||||||||
| module.exports = { | ||||||||||
| createBoundingBox, | ||||||||||
| }; | ||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| // 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_embeddings_docretrieval_with_txt] | ||
| const {GoogleGenAI} = require('@google/genai'); | ||
|
|
||
| const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT; | ||
|
|
||
| async function generateEmbeddingsForRetrieval( | ||
| projectId = GOOGLE_CLOUD_PROJECT | ||
| ) { | ||
| const client = new GoogleGenAI(projectId); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This sample appears to be for Vertex AI, but the |
||
|
|
||
| const prompt = [ | ||
| "How do I get a driver's license/learner's permit?", | ||
| "How long is my driver's license valid for?", | ||
| "Driver's knowledge test study guide", | ||
| ]; | ||
|
|
||
| const response = await client.models.embedContent({ | ||
| model: 'gemini-embedding-001', | ||
| contents: prompt, | ||
| config: { | ||
| taskType: 'RETRIEVAL_DOCUMENT', // Optional | ||
| outputDimensionality: 3072, // Optional | ||
| title: "Driver's License", // Optional | ||
| }, | ||
| }); | ||
|
|
||
| console.log(response); | ||
|
|
||
| // Example response: | ||
| // embeddings=[ContentEmbedding(values=[-0.06302902102470398, 0.00928034819662571, 0.014716853387653828, -0.028747491538524628, ... ], | ||
| // statistics=ContentEmbeddingStatistics(truncated=False, token_count=13.0))] | ||
| // metadata=EmbedContentMetadata(billable_character_count=112) | ||
|
|
||
| return response; | ||
| } | ||
| // [END googlegenaisdk_embeddings_docretrieval_with_txt] | ||
|
|
||
| module.exports = { | ||
| generateEmbeddingsForRetrieval, | ||
| }; | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,43 @@ | ||||||
| // 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_vertexai_express_mode] | ||||||
| const {GoogleGenAI} = require('@google/genai'); | ||||||
| const API_KEY = 'PUT HERE YOUR API KEY'; | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hardcoding credential placeholders like
Suggested change
|
||||||
|
|
||||||
| async function generateWithApiKey(apiKey = API_KEY) { | ||||||
| const client = new GoogleGenAI({ | ||||||
| vertexai: true, | ||||||
| apiKey: apiKey, | ||||||
| }); | ||||||
|
|
||||||
| const response = await client.models.generateContentStream({ | ||||||
| model: 'gemini-2.5-flash', | ||||||
| contents: 'Explain bubble sort to me.', | ||||||
| }); | ||||||
|
|
||||||
| console.log(response.text); | ||||||
|
|
||||||
| // Example response: | ||||||
| // Bubble Sort is a simple sorting algorithm that repeatedly steps through the list | ||||||
|
|
||||||
| return response; | ||||||
| } | ||||||
| // [END googlegenaisdk_vertexai_express_mode] | ||||||
|
|
||||||
| module.exports = { | ||||||
| generateWithApiKey, | ||||||
| }; | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,6 +22,9 @@ | |
| "chai": "^4.5.0", | ||
| "mocha": "^10.0.0", | ||
| "sinon": "^18.0.0", | ||
| "uuid": "^10.0.0" | ||
| "uuid": "^10.0.0", | ||
| "proxyquire": "^2.1.3", | ||
| "canvas": "^3.1.0", | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| "node-fetch": "^2.7.0" | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,62 @@ | ||||||
| // 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_provisionedthroughput_with_txt] | ||||||
| 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 generateWithProvisionedThroughput( | ||||||
| projectId = GOOGLE_CLOUD_PROJECT, | ||||||
| location = GOOGLE_CLOUD_LOCATION | ||||||
| ) { | ||||||
| const client = new GoogleGenAI({ | ||||||
| vertexai: true, | ||||||
| project: projectId, | ||||||
| location: location, | ||||||
| httpOptions: { | ||||||
| apiVersion: 'v1', | ||||||
| headers: { | ||||||
| // Options: | ||||||
| // - "dedicated": Use Provisioned Throughput | ||||||
| // - "shared": Use pay-as-you-go | ||||||
| // https://cloud.google.com/vertex-ai/generative-ai/docs/use-provisioned-throughput | ||||||
| 'X-Vertex-AI-LLM-Request-Type': 'shared', | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The sample is named
Suggested change
|
||||||
| }, | ||||||
| }, | ||||||
| }); | ||||||
|
|
||||||
| const response = await client.models.generateContent({ | ||||||
| model: 'gemini-2.5-flash', | ||||||
| contents: 'How does AI work?', | ||||||
| }); | ||||||
|
|
||||||
| console.log(response.text); | ||||||
|
|
||||||
| // Example response: | ||||||
| // Okay, let's break down how AI works. It's a broad field, so I'll focus on the ... | ||||||
| // Here's a simplified overview: | ||||||
| // ... | ||||||
|
|
||||||
| return response.text; | ||||||
| } | ||||||
|
|
||||||
| // [END googlegenaisdk_provisionedthroughput_with_txt] | ||||||
|
|
||||||
| module.exports = { | ||||||
| generateWithProvisionedThroughput, | ||||||
| }; | ||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hardcoding the output filename to
output.pngcan be problematic as it might overwrite an existing file in the directory where the script is run. Consider making the output filename unique, for example by including a timestamp. You'll also need to update the log message on the next line to use the dynamic filename.