Skip to content

Commit ba6eee7

Browse files
authored
Merge branch 'main' into main
2 parents 78f6186 + ba38c71 commit ba6eee7

35 files changed

+296
-136
lines changed

genai/controlled-generation/ctrlgen-with-enum-schema.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ async function generateContent(
3636
};
3737

3838
const response = await ai.models.generateContent({
39-
model: 'gemini-2.0-flash',
39+
model: 'gemini-2.5-flash',
4040
contents: 'What type of instrument is an oboe?',
4141
config: {
4242
responseMimeType: 'text/x.enum',
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+
};

genai/count-tokens/counttoken-with-txt-vid.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ async function countTokens(
3838
};
3939

4040
const response = await ai.models.countTokens({
41-
model: 'gemini-2.0-flash',
41+
model: 'gemini-2.5-flash',
4242
contents: [video, 'Provide a description of the video.'],
4343
});
4444

genai/count-tokens/counttoken-with-txt.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ async function countTokens(
3131
});
3232

3333
const response = await ai.models.countTokens({
34-
model: 'gemini-2.0-flash',
34+
model: 'gemini-2.5-flash',
3535
contents: 'What is the highest mountain in Africa?',
3636
});
3737

genai/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"test": "c8 mocha -p -j 2 --timeout 2400000 test/*.test.js test/**/*.test.js"
1414
},
1515
"dependencies": {
16-
"@google/genai": "^0.13.0",
16+
"@google/genai": "1.12.0",
1717
"axios": "^1.6.2",
1818
"supertest": "^7.0.0"
1919
},
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
});

0 commit comments

Comments
 (0)