Generate text responses using Gemini API with external function calls in a chat scenario

Generate text responses using Gemini API with external function calls. This example demonstrates a chat scenario with two functions and two sequential prompts.

Code sample

Node.js

Before trying this sample, follow the Node.js setup instructions in the Vertex AI quickstart using client libraries. For more information, see the Vertex AI Node.js API reference documentation.

To authenticate to Vertex AI, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

const{
VertexAI,
FunctionDeclarationSchemaType,
}=require('@google-cloud/vertexai');
constfunctionDeclarations=[
{
function_declarations:[
{
name:'get_current_weather',
description:'get weather in a given location',
parameters:{
type:FunctionDeclarationSchemaType .OBJECT ,
properties:{
location:{type:FunctionDeclarationSchemaType .STRING },
unit:{
type:FunctionDeclarationSchemaType .STRING ,
enum:['celsius','fahrenheit'],
},
},
required:['location'],
},
},
],
},
];
constfunctionResponseParts=[
{
functionResponse:{
name:'get_current_weather',
response:{name:'get_current_weather',content:{weather:'super nice'}},
},
},
];
/**
 * TODO(developer): Update these variables before running the sample.
 */
asyncfunctionfunctionCallingStreamChat(
projectId='PROJECT_ID',
location='us-central1',
model='gemini-2.0-flash-001'
){
// Initialize Vertex with your Cloud project and location
constvertexAI=newVertexAI ({project:projectId,location:location});
// Instantiate the model
constgenerativeModel=vertexAI.getGenerativeModel ({
model:model,
});
// Create a chat session and pass your function declarations
constchat=generativeModel.startChat({
tools:functionDeclarations,
});
constchatInput1='What is the weather in Boston?';
// This should include a functionCall response from the model
constresult1=awaitchat.sendMessageStream(chatInput1);
forawait(constitemofresult1.stream ){
console.log(item.candidates [0]);
}
awaitresult1.response;
// Send a follow up message with a FunctionResponse
constresult2=awaitchat.sendMessageStream(functionResponseParts);
forawait(constitemofresult2.stream ){
console.log(item.candidates [0]);
}
// This should include a text response from the model using the response content
// provided above
constresponse2=awaitresult2.response;
console.log(response2.candidates [0].content .parts [0].text);
}

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser.

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.