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
3 changes: 1 addition & 2 deletions genai/count-tokens/counttoken-resp-with-txt.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.

'use strict';
// [START generativeaionvertexai_gemini_token_count]

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

Expand Down Expand Up @@ -41,7 +41,6 @@ async function countTokens(
return response.usageMetadata;
}
// [END googlegenaisdk_counttoken_resp_with_txt]
// [END generativeaionvertexai_gemini_token_count]

module.exports = {
countTokens,
Expand Down
2 changes: 0 additions & 2 deletions genai/count-tokens/counttoken-with-txt-vid.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

'use strict';

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

Expand Down Expand Up @@ -48,7 +47,6 @@ async function countTokens(
return response.totalTokens;
}
// [END googlegenaisdk_counttoken_with_txt_vid]
// [END generativeaionvertexai_gemini_token_count_advanced]

module.exports = {
countTokens,
Expand Down
4 changes: 0 additions & 4 deletions genai/text-generation/textgen-sys-instr-with-txt.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@

'use strict';

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

Expand Down Expand Up @@ -53,8 +51,6 @@ async function generateContent(
return response.text;
}
// [END googlegenaisdk_textgen_sys_instr_with_txt]
// [END generativeaionvertexai_gemini_system_instruction]
// [END generativeaionvertexai_gemini_translate]

module.exports = {
generateContent,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.

'use strict';
// [START generativeaionvertexai_gemini_audio_transcription]

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

Expand Down Expand Up @@ -63,7 +63,6 @@ async function generateText(
}

// [END googlegenaisdk_textgen_transcript_with_gcs_audio]
// [END generativeaionvertexai_gemini_audio_transcription]

module.exports = {
generateText,
Expand Down
3 changes: 1 addition & 2 deletions genai/text-generation/textgen-with-gcs-audio.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.

'use strict';
// [START generativeaionvertexai_gemini_audio_summarization]

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

Expand Down Expand Up @@ -55,7 +55,6 @@ async function generateText(
return response.text;
}
// [END googlegenaisdk_textgen_with_gcs_audio]
// [END generativeaionvertexai_gemini_audio_summarization]

module.exports = {
generateText,
Expand Down
3 changes: 1 addition & 2 deletions genai/text-generation/textgen-with-pdf.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.

'use strict';
// [START generativeaionvertexai_gemini_pdf]

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

Expand Down Expand Up @@ -59,7 +59,6 @@ async function generateText(
}

// [END googlegenaisdk_textgen_with_pdf]
// [END generativeaionvertexai_gemini_pdf]

module.exports = {
generateText,
Expand Down
3 changes: 1 addition & 2 deletions genai/text-generation/textgen-with-txt-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.

'use strict';
// [START generativeaionvertexai_stream_text_basic]

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

Expand Down Expand Up @@ -43,7 +43,6 @@ async function generateContent(
return response_text;
}
// [END googlegenaisdk_textgen_with_txt_stream]
// [END generativeaionvertexai_stream_text_basic]

module.exports = {
generateContent,
Expand Down
3 changes: 1 addition & 2 deletions genai/text-generation/textgen-with-txt.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.

'use strict';
// [START generativeaionvertexai_gemini_generate_from_text_input]

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

Expand All @@ -40,7 +40,6 @@ async function generateContent(
return response.text;
}
// [END googlegenaisdk_textgen_with_txt]
// [END generativeaionvertexai_gemini_generate_from_text_input]

module.exports = {
generateContent,
Expand Down
2 changes: 0 additions & 2 deletions genai/tools/tools-func-desc-with-txt.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

'use strict';

// [START generativeaionvertexai_function_calling_advanced]
// [START googlegenaisdk_tools_func_desc_with_txt]
const {GoogleGenAI, Type} = require('@google/genai');

Expand Down Expand Up @@ -100,7 +99,6 @@ async function generateFunctionDesc(
}

// [END googlegenaisdk_tools_func_desc_with_txt]
// [END generativeaionvertexai_function_calling_advanced]

module.exports = {
generateFunctionDesc,
Expand Down
59 changes: 59 additions & 0 deletions generative-ai/snippets/count-tokens/countTokens.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// 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.

// [START generativeaionvertexai_gemini_token_count]
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 the client with your Cloud project and location
const client = new GoogleGenAI({
vertexai: true,
project: projectId,
location: location,
});

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

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

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

// Response tokens count
const usageMetadata = result.usageMetadata;
console.log('Response tokens count: ', usageMetadata);
}
// [END generativeaionvertexai_gemini_token_count]

countTokens(...process.argv.slice(2)).catch(err => {
console.error(err.message);
process.exitCode = 1;
});
71 changes: 71 additions & 0 deletions generative-ai/snippets/count-tokens/countTokensAdvanced.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// 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.

// [START generativeaionvertexai_gemini_token_count_advanced]
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);
}
// [END generativeaionvertexai_gemini_token_count_advanced]

countTokens(...process.argv.slice(2)).catch(err => {
console.error(err.message);
process.exitCode = 1;
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// 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.

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

const tools = [
{
functionDeclarations: [
{
name: 'get_product_sku',
description: 'Get the available inventory for Google products',
parameters: {
type: 'OBJECT',
properties: {
productName: {type: 'STRING'},
},
},
},
{
name: 'get_store_location',
description: 'Get the location of the closest store',
parameters: {
type: 'OBJECT',
properties: {
location: {type: 'STRING'},
},
},
},
],
},
];

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

/**
* TODO(developer): Update these variables before running the sample.
*/
async function functionCallingAdvanced(
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 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,
},
});
console.log(JSON.stringify(result.functionCalls));
}
// [END generativeaionvertexai_function_calling_advanced]

functionCallingAdvanced(...process.argv.slice(2)).catch(err => {
console.error(err.message);
process.exitCode = 1;
});
Loading
Loading