Skip to content

Commit aa40ac1

Browse files
committed
refactor: migrate countTokens samples to @google/genai, update config, and remove deprecated totalBillableCharacters
1 parent 93f3522 commit aa40ac1

4 files changed

Lines changed: 50 additions & 45 deletions

File tree

generative-ai/snippets/count-tokens/countTokens.js

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// limitations under the License.
1414

1515
// [START generativeaionvertexai_gemini_token_count]
16-
const {VertexAI} = require('@google-cloud/vertexai');
16+
const {GoogleGenAI} = require('@google/genai');
1717

1818
/**
1919
* TODO(developer): Update these variables before running the sample.
@@ -23,27 +23,32 @@ async function countTokens(
2323
location = 'us-central1',
2424
model = 'gemini-2.0-flash-001'
2525
) {
26-
// Initialize Vertex with your Cloud project and location
27-
const vertexAI = new VertexAI({project: projectId, location: location});
28-
29-
// Instantiate the model
30-
const generativeModel = vertexAI.getGenerativeModel({
31-
model: model,
26+
// Initialize the client with your Cloud project and location
27+
const client = new GoogleGenAI({
28+
vertexai: true,
29+
project: projectId,
30+
location: location,
3231
});
3332

34-
const req = {
35-
contents: [{role: 'user', parts: [{text: 'How are you doing today?'}]}],
36-
};
33+
const contents = [
34+
{role: 'user', parts: [{text: 'How are you doing today?'}]},
35+
];
3736

3837
// Prompt tokens count
39-
const countTokensResp = await generativeModel.countTokens(req);
38+
const countTokensResp = await client.models.countTokens({
39+
model: model,
40+
contents: contents,
41+
});
4042
console.log('Prompt tokens count: ', countTokensResp);
4143

4244
// Send text to gemini
43-
const result = await generativeModel.generateContent(req);
45+
const result = await client.models.generateContent({
46+
model: model,
47+
contents: contents,
48+
});
4449

4550
// Response tokens count
46-
const usageMetadata = result.response.usageMetadata;
51+
const usageMetadata = result.usageMetadata;
4752
console.log('Response tokens count: ', usageMetadata);
4853
}
4954
// [END generativeaionvertexai_gemini_token_count]

generative-ai/snippets/count-tokens/countTokensAdvanced.js

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
// limitations under the License.
1414

1515
// [START generativeaionvertexai_gemini_token_count_advanced]
16-
const {VertexAI} = require('@google-cloud/vertexai');
17-
16+
const {GoogleGenAI} = require('@google/genai');
1817
/**
1918
* TODO(developer): Update these variables before running the sample.
2019
*/
@@ -23,42 +22,42 @@ async function countTokens(
2322
location = 'us-central1',
2423
model = 'gemini-2.0-flash-001'
2524
) {
26-
// Initialize Vertex with your Cloud project and location
27-
const vertexAI = new VertexAI({project: projectId, location: location});
28-
29-
// Instantiate the model
30-
const generativeModel = vertexAI.getGenerativeModel({
31-
model: model,
25+
// Initialize client with your Cloud project and location
26+
const client = new GoogleGenAI({
27+
vertexai: true,
28+
project: projectId,
29+
location: location,
3230
});
3331

34-
const req = {
35-
contents: [
36-
{
37-
role: 'user',
38-
parts: [
39-
{
40-
file_data: {
41-
file_uri:
42-
'gs://cloud-samples-data/generative-ai/video/pixel8.mp4',
43-
mime_type: 'video/mp4',
44-
},
32+
const contents = [
33+
{
34+
role: 'user',
35+
parts: [
36+
{
37+
fileData: {
38+
fileUri: 'gs://cloud-samples-data/generative-ai/video/pixel8.mp4',
39+
mimeType: 'video/mp4',
4540
},
46-
{text: 'Provide a description of the video.'},
47-
],
48-
},
49-
],
50-
};
41+
},
42+
{text: 'Provide a description of the video.'},
43+
],
44+
},
45+
];
46+
47+
const countTokensResp = await client.models.countTokens({
48+
model: model,
49+
contents: contents,
50+
});
5151

52-
const countTokensResp = await generativeModel.countTokens(req);
5352
console.log('Prompt Token Count:', countTokensResp.totalTokens);
54-
console.log(
55-
'Prompt Character Count:',
56-
countTokensResp.totalBillableCharacters
57-
);
5853

5954
// Sent text to Gemini
60-
const result = await generativeModel.generateContent(req);
61-
const usageMetadata = result.response.usageMetadata;
55+
const result = await client.models.generateContent({
56+
model: model,
57+
contents: contents,
58+
});
59+
60+
const usageMetadata = result.usageMetadata;
6261

6362
console.log('Prompt Token Count:', usageMetadata.promptTokenCount);
6463
console.log('Candidates Token Count:', usageMetadata.candidatesTokenCount);

generative-ai/snippets/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"dependencies": {
1616
"@google-cloud/aiplatform": "^3.12.0",
1717
"@google-cloud/vertexai": "github:googleapis/nodejs-vertexai",
18+
"@google/genai": "^2.7.0",
1819
"axios": "^1.6.2",
1920
"supertest": "^7.0.0"
2021
},

generative-ai/snippets/test/count-tokens/countTokensAdvanced.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,6 @@ describe('Count tokens advanced', async () => {
3838
);
3939

4040
assert(output.match(/Prompt Token Count: \d+/));
41-
assert(output.match(/Prompt Character Count: \d+/));
41+
assert(output.match(/Total Token Count: \d+/));
4242
});
4343
});

0 commit comments

Comments
 (0)