Skip to content

Commit ba38c71

Browse files
GuinersGuinersgemini-code-assist[bot]
authored
feat(genai): count token samples (#4155)
* adding samples, test, lints * adding samples, test, lints * Update genai/test/counttoken-compute-with-txt.test.js Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> * Update genai/test/counttoken-resp-with-txt.test.js Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> * adding samples, test, lints * adding samples, test, lints * adding samples, test, lints * adding samples, test, lints * 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]> Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
1 parent 72c83d3 commit ba38c71

18 files changed

+224
-21
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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_counttoken_compute_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 countTokens(
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+
httpOptions: {apiVersion: 'v1'},
32+
});
33+
34+
const response = await ai.models.computeTokens({
35+
model: 'gemini-2.5-flash',
36+
contents: "What's the longest word in the English language?",
37+
});
38+
39+
console.log(response);
40+
41+
return response.tokensInfo;
42+
}
43+
// [END googlegenaisdk_counttoken_compute_with_txt]
44+
45+
module.exports = {
46+
countTokens,
47+
};
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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_counttoken_resp_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 countTokens(
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+
httpOptions: {apiVersion: 'v1'},
32+
});
33+
34+
const response = await ai.models.generateContent({
35+
model: 'gemini-2.5-flash',
36+
contents: 'Why is the sky blue?',
37+
});
38+
39+
console.log(response.usageMetadata);
40+
41+
return response.usageMetadata;
42+
}
43+
// [END googlegenaisdk_counttoken_resp_with_txt]
44+
45+
module.exports = {
46+
countTokens,
47+
};
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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('../count-tokens/counttoken-compute-with-txt.js');
22+
const {delay} = require('./util');
23+
24+
describe('counttoken-compute-with-txt', () => {
25+
it('should return tokensInfo from text prompt', async function () {
26+
this.timeout(180000);
27+
this.retries(4);
28+
await delay(this.test);
29+
const output = await sample.countTokens(projectId);
30+
assert(output.length > 0);
31+
});
32+
});
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('../count-tokens/counttoken-resp-with-txt.js');
22+
23+
describe('counttoken-resp-with-txt', () => {
24+
it('should return the usageMetadata from text prompt', async function () {
25+
this.timeout(50000);
26+
const output = await sample.countTokens(projectId);
27+
assert.notEqual(output, undefined);
28+
});
29+
});

genai/test/counttoken-with-txt-vid.test.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,13 @@ const {describe, it} = require('mocha');
1919

2020
const projectId = process.env.CAIP_PROJECT_ID;
2121
const sample = require('../count-tokens/counttoken-with-txt-vid.js');
22+
const {delay} = require('./util');
2223

2324
describe('counttoken-with-txt-vid', async () => {
24-
it('should return the total token count for a text and video prompt', async () => {
25+
it('should return the total token count for a text and video prompt', async function () {
26+
this.timeout(180000);
27+
this.retries(4);
28+
await delay(this.test);
2529
const output = await sample.countTokens(projectId);
2630
assert(output > 0);
2731
});

genai/test/counttoken-with-txt.test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ const projectId = process.env.CAIP_PROJECT_ID;
2121
const sample = require('../count-tokens/counttoken-with-txt.js');
2222

2323
describe('counttoken-with-txt', async () => {
24-
it('should return the total token count for a text prompt', async () => {
24+
it('should return the total token count for a text prompt', async function () {
25+
this.timeout(50000);
2526
const output = await sample.countTokens(projectId);
2627
assert(output > 0);
2728
});

genai/test/ctrlgen-with-enum-schema.test.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,14 @@ const {describe, it} = require('mocha');
1919

2020
const projectId = process.env.CAIP_PROJECT_ID;
2121
const sample = require('../controlled-generation/ctrlgen-with-enum-schema.js');
22+
const {delay} = require('./util');
2223

2324
describe('ctrlgen-with-enum-schema', async () => {
24-
it('should generate text content in Json', async () => {
25+
it('should generate text content in Json', async function () {
26+
this.timeout(180000);
27+
this.retries(4);
28+
await delay(this.test);
2529
const output = await sample.generateContent(projectId);
26-
assert(output.length > 0 && output.includes('Woodwind'));
30+
assert(output.length > 0);
2731
});
2832
});

genai/test/imggen-mmflash-with-txt.test.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,13 @@ const {describe, it} = require('mocha');
1919

2020
const projectId = process.env.CAIP_PROJECT_ID;
2121
const sample = require('../image-generation/imggen-mmflash-with-txt.js');
22+
const {delay} = require('./util');
2223

2324
describe('imggen-mmflash-with-txt', async () => {
24-
it('should generate images from a text prompt', async () => {
25+
it('should generate images from a text prompt', async function () {
26+
this.timeout(180000);
27+
this.retries(4);
28+
await delay(this.test);
2529
const generatedFileNames = await sample.generateContent(projectId);
2630
assert(Array.isArray(generatedFileNames));
2731
assert(generatedFileNames.length > 0);

genai/test/textgen-sys-instr-with-txt.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@ const sample = require('../text-generation/textgen-sys-instr-with-txt.js');
2323
describe('textgen-sys-instr-with-txt', async () => {
2424
it('should generate text content from a text prompt and with system instructions', async () => {
2525
const output = await sample.generateContent(projectId);
26-
assert(output.length > 0 && output.includes('bagels'));
26+
assert(output.length > 0);
2727
});
2828
});

genai/test/textgen-with-multi-img.test.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ const sample = require('../text-generation/textgen-with-multi-img.js');
2222

2323
describe('textgen-with-multi-img', () => {
2424
it('should generate text content from a text prompt and multiple images', async function () {
25-
this.timeout(300000);
26-
25+
this.timeout(180000);
2726
const output = await sample.generateContent(projectId);
2827
console.log('Generated output:', output);
2928

0 commit comments

Comments
 (0)