Skip to content
33 changes: 19 additions & 14 deletions generative-ai/snippets/count-tokens/countTokens.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,37 +13,42 @@
// limitations under the License.

// [START generativeaionvertexai_gemini_token_count]
const {VertexAI} = require('@google-cloud/vertexai');
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.0-flash-001'
model = 'gemini-2.5-flash'
) {
// Initialize Vertex with your Cloud project and location
const vertexAI = new VertexAI({project: projectId, location: location});

// Instantiate the model
const generativeModel = vertexAI.getGenerativeModel({
model: model,
// Initialize the client with your Cloud project and location
const client = new GoogleGenAI({
vertexai: true,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Out of curiosity why we are keeping this flag?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The vertexai: true flag is a switch that tells the SDK: Connect to the enterprise version of Google Cloud.
If you leave it out, the code assumes you are a standard user and will ask for a traditional API Key

project: projectId,
location: location,
});

const req = {
contents: [{role: 'user', parts: [{text: 'How are you doing today?'}]}],
};
const contents = [
{role: 'user', parts: [{text: 'How are you doing today?'}]},
];

// Prompt tokens count
const countTokensResp = await generativeModel.countTokens(req);
const countTokensResp = await client.models.countTokens({
model: model,
contents: contents,
});
console.log('Prompt tokens count: ', countTokensResp);

// Send text to gemini
const result = await generativeModel.generateContent(req);
const result = await client.models.generateContent({
model: model,
contents: contents,
});

// Response tokens count
const usageMetadata = result.response.usageMetadata;
const usageMetadata = result.usageMetadata;
console.log('Response tokens count: ', usageMetadata);
}
// [END generativeaionvertexai_gemini_token_count]
Expand Down
65 changes: 32 additions & 33 deletions generative-ai/snippets/count-tokens/countTokensAdvanced.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,52 +13,51 @@
// limitations under the License.

// [START generativeaionvertexai_gemini_token_count_advanced]
const {VertexAI} = require('@google-cloud/vertexai');

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.0-flash-001'
model = 'gemini-2.5-flash'
) {
// Initialize Vertex with your Cloud project and location
const vertexAI = new VertexAI({project: projectId, location: location});

// Instantiate the model
const generativeModel = vertexAI.getGenerativeModel({
model: model,
// Initialize client with your Cloud project and location
const client = new GoogleGenAI({
vertexai: true,
project: projectId,
location: location,
});

const req = {
contents: [
{
role: 'user',
parts: [
{
file_data: {
file_uri:
'gs://cloud-samples-data/generative-ai/video/pixel8.mp4',
mime_type: 'video/mp4',
},
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.'},
],
},
],
};
},
{text: 'Provide a description of the video.'},
],
},
];

const countTokensResp = await client.models.countTokens({
model: model,
contents: contents,
});

const countTokensResp = await generativeModel.countTokens(req);
console.log('Prompt Token Count:', countTokensResp.totalTokens);
console.log(
'Prompt Character Count:',
countTokensResp.totalBillableCharacters
);

// Sent text to Gemini
const result = await generativeModel.generateContent(req);
const usageMetadata = result.response.usageMetadata;
// 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,28 @@
// limitations under the License.

// [START generativeaionvertexai_function_calling_advanced]
const {
VertexAI,
FunctionDeclarationSchemaType,
} = require('@google-cloud/vertexai');
const {GoogleGenAI} = require('@google/genai');

const functionDeclarations = [
const tools = [
{
function_declarations: [
functionDeclarations: [
{
name: 'get_product_sku',
description:
'Get the available inventory for a Google products, e.g: Pixel phones, Pixel Watches, Google Home etc',
description: 'Get the available inventory for Google products',
parameters: {
type: FunctionDeclarationSchemaType.OBJECT,
type: 'OBJECT',
properties: {
productName: {type: FunctionDeclarationSchemaType.STRING},
productName: {type: 'STRING'},
},
},
},
{
name: 'get_store_location',
description: 'Get the location of the closest store',
parameters: {
type: FunctionDeclarationSchemaType.OBJECT,
type: 'OBJECT',
properties: {
location: {type: FunctionDeclarationSchemaType.STRING},
location: {type: 'STRING'},
},
},
},
Expand All @@ -47,49 +43,39 @@ const functionDeclarations = [
];

const toolConfig = {
function_calling_config: {
functionCallingConfig: {
mode: 'ANY',
allowed_function_names: ['get_product_sku'],
allowedFunctionNames: ['get_product_sku'],
},
};

const generationConfig = {
temperature: 0.95,
topP: 1.0,
maxOutputTokens: 8192,
};

/**
* TODO(developer): Update these variables before running the sample.
*/
async function functionCallingAdvanced(
projectId = 'PROJECT_ID',
location = 'us-central1',
model = 'gemini-2.0-flash-001'
model = 'gemini-2.5-flash'
) {
// Initialize Vertex with your Cloud project and location
const vertexAI = new VertexAI({project: projectId, location: location});
// Initialize client with your Cloud project and location
const client = new GoogleGenAI({
vertexai: true,
project: projectId,
location: location,
});

// Instantiate the model
const generativeModel = vertexAI.preview.getGenerativeModel({
const result = await client.models.generateContent({
model: model,
contents: 'Do you have the White Pixel 8 Pro 128GB in stock in the US?',
config: {
tools: tools,
toolConfig: toolConfig,
temperature: 0.95,
topP: 1.0,
maxOutputTokens: 8192,
},
});

const request = {
contents: [
{
role: 'user',
parts: [
{text: 'Do you have the White Pixel 8 Pro 128GB in stock in the US?'},
],
},
],
tools: functionDeclarations,
tool_config: toolConfig,
generation_config: generationConfig,
};
const result = await generativeModel.generateContent(request);
console.log(JSON.stringify(result.response.candidates[0].content));
console.log(JSON.stringify(result.functionCalls));
}
// [END generativeaionvertexai_function_calling_advanced]

Expand Down
42 changes: 19 additions & 23 deletions generative-ai/snippets/function-calling/functionCallingBasic.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,20 @@
// limitations under the License.

// [START generativeaionvertexai_function_calling_basic]
const {
VertexAI,
FunctionDeclarationSchemaType,
} = require('@google-cloud/vertexai');
const {GoogleGenAI} = require('@google/genai');

const functionDeclarations = [
const tools = [
{
function_declarations: [
functionDeclarations: [
{
name: 'get_current_weather',
description: 'get weather in a given location',
parameters: {
type: FunctionDeclarationSchemaType.OBJECT,
type: 'OBJECT',
properties: {
location: {type: FunctionDeclarationSchemaType.STRING},
location: {type: 'STRING'},
unit: {
type: FunctionDeclarationSchemaType.STRING,
type: 'STRING',
enum: ['celsius', 'fahrenheit'],
},
},
Expand All @@ -46,24 +43,23 @@ const functionDeclarations = [
async function functionCallingBasic(
projectId = 'PROJECT_ID',
location = 'us-central1',
model = 'gemini-2.0-flash-001'
model = 'gemini-2.5-flash'
) {
// Initialize Vertex with your Cloud project and location
const vertexAI = new VertexAI({project: projectId, location: location});
// Initialize client with your Cloud project and location
const client = new GoogleGenAI({
vertexai: true,
project: projectId,
location: location,
});

// Instantiate the model
const generativeModel = vertexAI.preview.getGenerativeModel({
const result = await client.models.generateContent({
model: model,
contents: 'What is the weather in Boston?',
config: {
tools: tools,
},
});

const request = {
contents: [
{role: 'user', parts: [{text: 'What is the weather in Boston?'}]},
],
tools: functionDeclarations,
};
const result = await generativeModel.generateContent(request);
console.log(JSON.stringify(result.response.candidates[0].content));
console.log(JSON.stringify(result.functionCalls));
}
// [END generativeaionvertexai_function_calling_basic]

Expand Down
Loading
Loading