From b2368ebda83704e552506cbae4410fd3c8ca6970 Mon Sep 17 00:00:00 2001 From: Angel Caamal Date: Fri, 17 Jul 2026 19:22:47 +0000 Subject: [PATCH] chore: decommission obsolete count tokens advanced sample --- .../count-tokens/countTokensAdvanced.js | 69 ------------------- .../count-tokens/countTokensAdvanced.test.js | 43 ------------ 2 files changed, 112 deletions(-) delete mode 100644 generative-ai/snippets/count-tokens/countTokensAdvanced.js delete mode 100644 generative-ai/snippets/test/count-tokens/countTokensAdvanced.test.js diff --git a/generative-ai/snippets/count-tokens/countTokensAdvanced.js b/generative-ai/snippets/count-tokens/countTokensAdvanced.js deleted file mode 100644 index df90a199cf..0000000000 --- a/generative-ai/snippets/count-tokens/countTokensAdvanced.js +++ /dev/null @@ -1,69 +0,0 @@ -// Copyright 2024 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. - -const {GoogleGenAI} = require('@google/genai'); -/** - * TODO(developer): Update these variables before running the sample. - */ -async function countTokens( - projectId = 'PROJECT_ID', - location = 'us-central1', - model = 'gemini-2.5-flash' -) { - // Initialize client with your Cloud project and location - const client = new GoogleGenAI({ - vertexai: true, - project: projectId, - location: location, - }); - - const contents = [ - { - role: 'user', - parts: [ - { - fileData: { - fileUri: 'gs://cloud-samples-data/generative-ai/video/pixel8.mp4', - mimeType: 'video/mp4', - }, - }, - {text: 'Provide a description of the video.'}, - ], - }, - ]; - - const countTokensResp = await client.models.countTokens({ - model: model, - contents: contents, - }); - - console.log('Prompt Token Count:', countTokensResp.totalTokens); - - // Send text to Gemini - const result = await client.models.generateContent({ - model: model, - contents: contents, - }); - - const usageMetadata = result.usageMetadata; - - console.log('Prompt Token Count:', usageMetadata.promptTokenCount); - console.log('Candidates Token Count:', usageMetadata.candidatesTokenCount); - console.log('Total Token Count:', usageMetadata.totalTokenCount); -} - -countTokens(...process.argv.slice(2)).catch(err => { - console.error(err.message); - process.exitCode = 1; -}); diff --git a/generative-ai/snippets/test/count-tokens/countTokensAdvanced.test.js b/generative-ai/snippets/test/count-tokens/countTokensAdvanced.test.js deleted file mode 100644 index e3b8d54da6..0000000000 --- a/generative-ai/snippets/test/count-tokens/countTokensAdvanced.test.js +++ /dev/null @@ -1,43 +0,0 @@ -// Copyright 2024 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 cp = require('child_process'); -const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); - -const projectId = process.env.CAIP_PROJECT_ID; -const location = process.env.LOCATION; -const model = 'gemini-2.5-flash'; - -describe('Count tokens advanced', async () => { - /** - * TODO(developer): Uncomment these variables before running the sample.\ - * (Not necessary if passing values as arguments) - */ - // const projectId = 'YOUR_PROJECT_ID'; - // const location = 'YOUR_LOCATION'; - // const model = 'gemini-2.5-flash'; - - it('should count tokens in a multimodal prompt', async () => { - const output = execSync( - `node ./count-tokens/countTokensAdvanced.js ${projectId} ${location} ${model}` - ); - - assert(output.match(/Prompt Token Count: \d+/)); - assert(output.match(/Total Token Count: \d+/)); - }); -});