Skip to content

Commit a169efc

Browse files
author
Guiners
committed
adding count token samples
1 parent 62dbb27 commit a169efc

File tree

4 files changed

+167
-0
lines changed

4 files changed

+167
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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 written agreement, 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 License for the License.
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
// [START googlegenaisdk_counttoken_localtokenizer_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 counttokenLocalTokenizerCompute() {
24+
const client = new GoogleGenAI({
25+
vertexai: true,
26+
project: GOOGLE_CLOUD_PROJECT,
27+
location: GOOGLE_CLOUD_LOCATION,
28+
httpOptions: {apiVersion: 'v1'},
29+
});
30+
31+
const response = await client.models.computeTokens({
32+
model: 'gemini-2.5-flash',
33+
contents: "What's the longest word in the English language?",
34+
});
35+
36+
console.log(response.tokensInfo);
37+
38+
// Example output:
39+
// {
40+
// tokensInfo: [
41+
// {
42+
// role: 'user',
43+
// tokenIds: [...],
44+
// tokens: [...],
45+
// }
46+
// ]
47+
// }
48+
49+
return response.tokensInfo;
50+
}
51+
// [END googlegenaisdk_counttoken_localtokenizer_compute_with_txt]
52+
53+
module.exports = {
54+
counttokenLocalTokenizerCompute,
55+
};
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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_localtokenizer_with_txt]
18+
19+
const {GoogleGenAI} = require('@google/genai');
20+
21+
const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT;
22+
const GOOGLE_CLOUD_LOCATION = process.env.GOOGLE_CLOUD_LOCATION || 'global';
23+
24+
async function counttokenLocalTokenizer() {
25+
const client = new GoogleGenAI({
26+
vertexai: true,
27+
project: GOOGLE_CLOUD_PROJECT,
28+
location: GOOGLE_CLOUD_LOCATION,
29+
httpOptions: {apiVersion: 'v1'},
30+
});
31+
32+
const response = await client.models.countTokens({
33+
model: 'gemini-2.5-flash',
34+
contents: "What's the highest mountain in Africa?",
35+
});
36+
37+
console.log(response.totalTokens);
38+
39+
// Example output:
40+
// { totalTokens: 10 }
41+
42+
return response.totalTokens;
43+
}
44+
// [END googlegenaisdk_counttoken_localtokenizer_with_txt]
45+
46+
module.exports = {
47+
counttokenLocalTokenizer,
48+
};
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-localtokenizer-compute-with-txt.js');
22+
const {delay} = require('./util');
23+
24+
describe('counttoken-localtokenizer-compute-with-txt', () => {
25+
it('should return totalTokens from text prompt', async function () {
26+
this.timeout(18000);
27+
this.retries(4);
28+
await delay(this.test);
29+
const output = await sample.counttokenLocalTokenizerCompute(projectId);
30+
assert(output.length > 0);
31+
});
32+
});
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-localtokenizer-with-txt.js');
22+
const {delay} = require('./util');
23+
24+
describe('counttoken-localtokenizer-with-txt', () => {
25+
it('should return totalTokens from text prompt', async function () {
26+
this.timeout(18000);
27+
this.retries(4);
28+
await delay(this.test);
29+
const output = await sample.counttokenLocalTokenizer(projectId);
30+
assert(output > 0);
31+
});
32+
});

0 commit comments

Comments
 (0)