1313// limitations under the License.
1414
1515// [START generativeaionvertexai_function_calling_advanced]
16- const {
17- VertexAI,
18- FunctionDeclarationSchemaType,
19- } = require ( '@google-cloud/vertexai' ) ;
16+ const { GoogleGenAI} = require ( '@google/genai' ) ;
2017
21- const functionDeclarations = [
18+ const tools = [
2219 {
23- function_declarations : [
20+ functionDeclarations : [
2421 {
2522 name : 'get_product_sku' ,
26- description :
27- 'Get the available inventory for a Google products, e.g: Pixel phones, Pixel Watches, Google Home etc' ,
23+ description : 'Get the available inventory for Google products' ,
2824 parameters : {
29- type : FunctionDeclarationSchemaType . OBJECT ,
25+ type : ' OBJECT' ,
3026 properties : {
31- productName : { type : FunctionDeclarationSchemaType . STRING } ,
27+ productName : { type : ' STRING' } ,
3228 } ,
3329 } ,
3430 } ,
3531 {
3632 name : 'get_store_location' ,
3733 description : 'Get the location of the closest store' ,
3834 parameters : {
39- type : FunctionDeclarationSchemaType . OBJECT ,
35+ type : ' OBJECT' ,
4036 properties : {
41- location : { type : FunctionDeclarationSchemaType . STRING } ,
37+ location : { type : ' STRING' } ,
4238 } ,
4339 } ,
4440 } ,
@@ -47,49 +43,39 @@ const functionDeclarations = [
4743] ;
4844
4945const toolConfig = {
50- function_calling_config : {
46+ functionCallingConfig : {
5147 mode : 'ANY' ,
52- allowed_function_names : [ 'get_product_sku' ] ,
48+ allowedFunctionNames : [ 'get_product_sku' ] ,
5349 } ,
5450} ;
5551
56- const generationConfig = {
57- temperature : 0.95 ,
58- topP : 1.0 ,
59- maxOutputTokens : 8192 ,
60- } ;
61-
6252/**
6353 * TODO(developer): Update these variables before running the sample.
6454 */
6555async function functionCallingAdvanced (
6656 projectId = 'PROJECT_ID' ,
6757 location = 'us-central1' ,
68- model = 'gemini-2.0 -flash-001 '
58+ model = 'gemini-2.5 -flash'
6959) {
70- // Initialize Vertex with your Cloud project and location
71- const vertexAI = new VertexAI ( { project : projectId , location : location } ) ;
60+ // Initialize client with your Cloud project and location
61+ const client = new GoogleGenAI ( {
62+ vertexai : true ,
63+ project : projectId ,
64+ location : location ,
65+ } ) ;
7266
73- // Instantiate the model
74- const generativeModel = vertexAI . preview . getGenerativeModel ( {
67+ const result = await client . models . generateContent ( {
7568 model : model ,
69+ contents : 'Do you have the White Pixel 8 Pro 128GB in stock in the US?' ,
70+ config : {
71+ tools : tools ,
72+ toolConfig : toolConfig ,
73+ temperature : 0.95 ,
74+ topP : 1.0 ,
75+ maxOutputTokens : 8192 ,
76+ } ,
7677 } ) ;
77-
78- const request = {
79- contents : [
80- {
81- role : 'user' ,
82- parts : [
83- { text : 'Do you have the White Pixel 8 Pro 128GB in stock in the US?' } ,
84- ] ,
85- } ,
86- ] ,
87- tools : functionDeclarations ,
88- tool_config : toolConfig ,
89- generation_config : generationConfig ,
90- } ;
91- const result = await generativeModel . generateContent ( request ) ;
92- console . log ( JSON . stringify ( result . response . candidates [ 0 ] . content ) ) ;
78+ console . log ( JSON . stringify ( result . functionCalls ) ) ;
9379}
9480// [END generativeaionvertexai_function_calling_advanced]
9581
0 commit comments