Skip to content

Commit 15023fb

Browse files
GuinersGuinersholtskinner
authored
feat(genai): Add samples for Embeddings, Provisioned Throughput, Safety (#4196)
Co-authored-by: Guiners <rkoza@softserveinc.com> Co-authored-by: Holt Skinner <13262395+holtskinner@users.noreply.github.com>
1 parent abc11a6 commit 15023fb

18 files changed

+551
-5
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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({
26+
vertexai: true,
27+
project: projectId,
28+
});
29+
30+
const prompt = [
31+
"How do I get a driver's license/learner's permit?",
32+
"How long is my driver's license valid for?",
33+
"Driver's knowledge test study guide",
34+
];
35+
36+
const response = await client.models.embedContent({
37+
model: 'gemini-embedding-001',
38+
contents: prompt,
39+
config: {
40+
taskType: 'RETRIEVAL_DOCUMENT', // Optional
41+
outputDimensionality: 3072, // Optional
42+
title: "Driver's License", // Optional
43+
},
44+
});
45+
46+
console.log(response);
47+
48+
// Example response:
49+
// embeddings=[ContentEmbedding(values=[-0.06302902102470398, 0.00928034819662571, 0.014716853387653828, -0.028747491538524628, ... ],
50+
// statistics=ContentEmbeddingStatistics(truncated=False, token_count=13.0))]
51+
// metadata=EmbedContentMetadata(billable_character_count=112)
52+
53+
return response;
54+
}
55+
// [END googlegenaisdk_embeddings_docretrieval_with_txt]
56+
57+
module.exports = {
58+
generateEmbeddingsForRetrieval,
59+
};

genai/embeddings/package.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "nodejs-genai-embeddings-samples",
3+
"version": "0.0.1",
4+
"private": true,
5+
"license": "Apache-2.0",
6+
"author": "Google LLC",
7+
"repository": {
8+
"type": "git",
9+
"url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git"
10+
},
11+
"engines": {
12+
"node": ">=16.0.0"
13+
},
14+
"files": [
15+
"*.js"
16+
],
17+
"scripts": {
18+
"test": "c8 mocha -p -j 2 --timeout 2400000 test/*.test.js test/**/*.test.js"
19+
},
20+
"dependencies": {
21+
"@google/genai": "1.30.0"
22+
},
23+
"devDependencies": {
24+
"c8": "^10.0.0",
25+
"chai": "^4.5.0",
26+
"mocha": "^10.0.0"
27+
}
28+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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('../embeddings-docretrieval-with-txt.js');
22+
const {delay} = require('../../test/util');
23+
24+
describe('embeddings-docretrieval-with-txt', () => {
25+
it('should return an object containing embeddings and metadata', async function () {
26+
this.retries(4);
27+
await delay(this.test);
28+
const result = await sample.generateEmbeddingsForRetrieval(projectId);
29+
assert.containsAllKeys(result, ['embeddings', 'metadata']);
30+
});
31+
});
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 = 'YOUR_EXPRESS_MODE_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/express-mode/package.json

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"name": "nodejs-genai-express-mode-samples",
3+
"version": "0.0.1",
4+
"private": true,
5+
"license": "Apache-2.0",
6+
"author": "Google LLC",
7+
"repository": {
8+
"type": "git",
9+
"url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git"
10+
},
11+
"engines": {
12+
"node": ">=16.0.0"
13+
},
14+
"files": [
15+
"*.js"
16+
],
17+
"scripts": {
18+
"test": "c8 mocha -p -j 2 --timeout 2400000 test/*.test.js test/**/*.test.js"
19+
},
20+
"dependencies": {
21+
"@google/genai": "1.30.0"
22+
},
23+
"devDependencies": {
24+
"c8": "^10.0.0",
25+
"chai": "^4.5.0",
26+
"mocha": "^10.0.0",
27+
"proxyquire": "^2.1.3"
28+
}
29+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
const {delay} = require('../../test/util');
20+
const proxyquire = require('proxyquire').noCallThru();
21+
22+
describe('vertexai-express-mode', () => {
23+
it('should call generateContentStream and return the mocked response', async function () {
24+
this.timeout(10000);
25+
26+
this.retries(4);
27+
await delay(this.test);
28+
29+
const mockGenerateContentStreamResult = {
30+
text: 'Bubble sort works by repeatedly swapping adjacent elements until sorted.',
31+
};
32+
33+
class MockModels {
34+
async generateContentStream() {
35+
return mockGenerateContentStreamResult;
36+
}
37+
}
38+
39+
class MockGoogleGenAI {
40+
constructor() {
41+
this.models = new MockModels();
42+
}
43+
}
44+
45+
const sample = proxyquire('../api-key-example.js', {
46+
'@google/genai': {GoogleGenAI: MockGoogleGenAI},
47+
});
48+
49+
const response = await sample.generateWithApiKey('FAKE_API_KEY');
50+
51+
assert.strictEqual(response.text, mockGenerateContentStreamResult.text);
52+
});
53+
});

genai/image-generation/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
},
1212
"dependencies": {
1313
"@google/genai": "1.34.0",
14-
"@google-cloud/storage": "^7.17.3"
14+
"@google-cloud/storage": "^7.17.3",
15+
"date-fns": "^4.1.0"
1516
},
1617
"devDependencies": {
1718
"c8": "^10.0.0",

genai/image-generation/test/imggen-mmflash-edit-img-with-txt-img.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ describe('imggen-mmflash-edit-img-with-txt-img', async () => {
2828
this.retries(4);
2929
await delay(this.test);
3030
const response = await sample.generateImage(projectId, location);
31+
console.log(response);
3132
assert(response);
3233
});
3334
});
1.2 MB
Loading

genai/output-folder/image.png

-103 KB
Loading

0 commit comments

Comments
 (0)