function calling
Enables Gemini to invoke developer-defined functions with structured JSON arguments. Define tools, detect function call requests, execute them, and return results.
Syntax
gemini-api
{ tools: [{ functionDeclarations: [{ name, description, parameters }] }] }Example
gemini-api
const model = genAI.getGenerativeModel({
model: "gemini-1.5-pro",
tools: [{
functionDeclarations: [{
name: "get_weather",
description: "Get the weather for a city. Call when user asks about weather.",
parameters: {
type: FunctionDeclarationSchemaType.OBJECT,
properties: {
city: { type: FunctionDeclarationSchemaType.STRING },
},
required: ["city"],
},
}],
}],
});
const response = await model.generateContent("What's the weather in Tokyo?");
const call = response.response.candidates?.[0]?.content.parts[0].functionCall;
console.log(call?.name, call?.args); // "get_weather", { city: "Tokyo" }