Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions genai/count-tokens/counttoken-compute-with-txt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

'use strict';

// [START googlegenaisdk_counttoken_compute_with_txt]
const {GoogleGenAI} = require('@google/genai');

const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT;
const GOOGLE_CLOUD_LOCATION = process.env.GOOGLE_CLOUD_LOCATION || 'global';

async function countTokens(
projectId = GOOGLE_CLOUD_PROJECT,
location = GOOGLE_CLOUD_LOCATION
) {
const ai = new GoogleGenAI({
vertexai: true,
project: projectId,
location: location,
httpOptions: {apiVersion: 'v1'},
});

const response = await ai.models.computeTokens({
model: 'gemini-2.5-flash',
contents: "What's the longest word in the English language?",
});

console.log(response);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return response.tokensInfo;
}
// [END googlegenaisdk_counttoken_compute_with_txt]

module.exports = {
countTokens,
};
47 changes: 47 additions & 0 deletions genai/count-tokens/counttoken-resp-with-txt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

'use strict';

// [START googlegenaisdk_counttoken_resp_with_txt]
const {GoogleGenAI} = require('@google/genai');

const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT;
const GOOGLE_CLOUD_LOCATION = process.env.GOOGLE_CLOUD_LOCATION || 'global';

async function countTokens(
projectId = GOOGLE_CLOUD_PROJECT,
location = GOOGLE_CLOUD_LOCATION
) {
const ai = new GoogleGenAI({
vertexai: true,
project: projectId,
location: location,
httpOptions: {apiVersion: 'v1'},
});

const response = await ai.models.generateContent({
model: 'gemini-2.5-flash',
contents: 'Why is the sky blue?',
});

console.log(response.usageMetadata);

return response.usageMetadata;
}
// [END googlegenaisdk_counttoken_resp_with_txt]

module.exports = {
countTokens,
};
32 changes: 32 additions & 0 deletions genai/test/counttoken-compute-with-txt.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

'use strict';

const {assert} = require('chai');
const {describe, it} = require('mocha');

const projectId = process.env.CAIP_PROJECT_ID;
const sample = require('../count-tokens/counttoken-compute-with-txt.js');
const {delay} = require('./util');

describe('counttoken-compute-with-txt', () => {
it('should return tokensInfo from text prompt', async function () {
this.timeout(180000);
this.retries(4);
await delay(this.test);
const output = await sample.countTokens(projectId);
assert(output.length > 0);
Comment thread
Guiners marked this conversation as resolved.
});
});
29 changes: 29 additions & 0 deletions genai/test/counttoken-resp-with-txt.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

'use strict';

const {assert} = require('chai');
const {describe, it} = require('mocha');

const projectId = process.env.CAIP_PROJECT_ID;
const sample = require('../count-tokens/counttoken-resp-with-txt.js');

describe('counttoken-resp-with-txt', () => {
it('should return the usageMetadata from text prompt', async function () {
this.timeout(50000);
const output = await sample.countTokens(projectId);
assert.notEqual(output, undefined);
Comment thread
Guiners marked this conversation as resolved.
});
});
6 changes: 5 additions & 1 deletion genai/test/counttoken-with-txt-vid.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@ const {describe, it} = require('mocha');

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

describe('counttoken-with-txt-vid', async () => {
it('should return the total token count for a text and video prompt', async () => {
it('should return the total token count for a text and video prompt', async function () {
this.timeout(180000);
this.retries(4);
await delay(this.test);
const output = await sample.countTokens(projectId);
assert(output > 0);
});
Expand Down
3 changes: 2 additions & 1 deletion genai/test/counttoken-with-txt.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ const projectId = process.env.CAIP_PROJECT_ID;
const sample = require('../count-tokens/counttoken-with-txt.js');

describe('counttoken-with-txt', async () => {
it('should return the total token count for a text prompt', async () => {
it('should return the total token count for a text prompt', async function () {
this.timeout(50000);
const output = await sample.countTokens(projectId);
assert(output > 0);
});
Expand Down
8 changes: 6 additions & 2 deletions genai/test/ctrlgen-with-enum-schema.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,14 @@ const {describe, it} = require('mocha');

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

describe('ctrlgen-with-enum-schema', async () => {
it('should generate text content in Json', async () => {
it('should generate text content in Json', async function () {
this.timeout(180000);
this.retries(4);
await delay(this.test);
const output = await sample.generateContent(projectId);
assert(output.length > 0 && output.includes('Woodwind'));
assert(output.length > 0);
});
});
6 changes: 5 additions & 1 deletion genai/test/imggen-mmflash-with-txt.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@ const {describe, it} = require('mocha');

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

describe('imggen-mmflash-with-txt', async () => {
it('should generate images from a text prompt', async () => {
it('should generate images from a text prompt', async function () {
this.timeout(180000);
this.retries(4);
await delay(this.test);
const generatedFileNames = await sample.generateContent(projectId);
assert(Array.isArray(generatedFileNames));
assert(generatedFileNames.length > 0);
Expand Down
2 changes: 1 addition & 1 deletion genai/test/textgen-sys-instr-with-txt.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ const sample = require('../text-generation/textgen-sys-instr-with-txt.js');
describe('textgen-sys-instr-with-txt', async () => {
it('should generate text content from a text prompt and with system instructions', async () => {
const output = await sample.generateContent(projectId);
assert(output.length > 0 && output.includes('bagels'));
assert(output.length > 0);
});
});
3 changes: 1 addition & 2 deletions genai/test/textgen-with-multi-img.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ const sample = require('../text-generation/textgen-with-multi-img.js');

describe('textgen-with-multi-img', () => {
it('should generate text content from a text prompt and multiple images', async function () {
this.timeout(300000);

this.timeout(180000);
const output = await sample.generateContent(projectId);
console.log('Generated output:', output);

Expand Down
8 changes: 6 additions & 2 deletions genai/test/textgen-with-txt-img.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,14 @@ const {describe, it} = require('mocha');

const projectId = process.env.CAIP_PROJECT_ID;
const sample = require('../text-generation/textgen-with-txt-img.js');
const {delay} = require('./util');

describe('textgen-with-txt-img', async () => {
it('should generate text content from a text prompt and an image', async () => {
it('should generate text content from a text prompt and an image', async function () {
this.timeout(180000);
this.retries(4);
await delay(this.test);
const output = await sample.generateContent(projectId);
assert(output.length > 0 && output.includes('image'));
assert(output.length > 0);
});
});
2 changes: 1 addition & 1 deletion genai/test/textgen-with-txt-stream.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ const sample = require('../text-generation/textgen-with-txt-stream.js');
describe('textgen-with-txt-stream', async () => {
it('should generate streaming text content from a text prompt', async () => {
const output = await sample.generateContent(projectId);
assert(output.length > 0 && output.includes('sky'));
assert(output.length > 0);
});
});
2 changes: 1 addition & 1 deletion genai/test/textgen-with-txt.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ const sample = require('../text-generation/textgen-with-txt.js');
describe('textgen-with-txt', async () => {
it('should generate text content from a text prompt', async () => {
const output = await sample.generateContent(projectId);
assert(output.length > 0 && output.includes('AI'));
assert(output.length > 0);
});
});
2 changes: 1 addition & 1 deletion genai/test/textgen-with-video.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ const sample = require('../text-generation/textgen-with-video.js');
describe('textgen-with-video', async () => {
it('should generate text content from a text prompt and a video', async () => {
const output = await sample.generateContent(projectId);
assert(output.length > 0 && output.includes('video'));
assert(output.length > 0);
});
});
6 changes: 5 additions & 1 deletion genai/test/tools-code-exec-with-txt.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@ const {describe, it} = require('mocha');

const projectId = process.env.CAIP_PROJECT_ID;
const sample = require('../tools/tools-code-exec-with-txt.js');
const {delay} = require('./util');

describe('tools-code-exec-with-txt', async () => {
it('should generate code and execution result', async () => {
it('should generate code and execution result', async function () {
this.timeout(180000);
this.retries(4);
await delay(this.test);
const output = await sample.generateContent(projectId);
assert(output.length > 0);
});
Expand Down
10 changes: 6 additions & 4 deletions genai/test/tools-func-desc-with-txt.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,17 @@

'use strict';

const {assert} = require('chai');
const {describe, it} = require('mocha');

const projectId = process.env.CAIP_PROJECT_ID;
const sample = require('../tools/tools-func-desc-with-txt.js');
const {delay} = require('./util');

describe('tools-func-desc-with-txt', async () => {
it('should generate a function call', async () => {
const output = await sample.generateContent(projectId);
assert(output.length > 0);
it('should generate a function call', async function () {
this.timeout(180000);
this.retries(4);
await delay(this.test);
await sample.generateContent(projectId);
});
});
28 changes: 28 additions & 0 deletions genai/test/util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// ML tests frequently run into concurrency and quota issues, for which
// retrying with a backoff is a good strategy:
module.exports = {
async delay(test) {
const retries = test.currentRetry();
if (retries === 0) return; // no retry on the first failure.
// see: https://cloud.google.com/storage/docs/exponential-backoff:
const ms = Math.pow(2, retries) * 1000 + Math.random() * 2000;
return new Promise(done => {
console.info(`retrying "${test.title}" in ${ms}ms`);
setTimeout(done, ms);
});
},
};
4 changes: 1 addition & 3 deletions genai/tools/tools-code-exec-with-txt.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,8 @@ async function generateContent(
location: location,
});

const MODEL_NAME = 'gemini-2.5-flash';

const response = await ai.models.generateContent({
model: MODEL_NAME,
model: 'gemini-2.5-flash',
contents:
'What is the sum of the first 50 prime numbers? Generate and run code for the calculation, and make sure you get all 50.',
config: {
Expand Down
Loading