Function Calling
Translation in Progress
This page is being translated. Content below is a placeholder.
What is Function Calling
Function Calling lets LLMs request execution of predefined functions.
Declaring Functions
Use JSON Schema format:
typescript
const functionDeclaration = {
name: 'read_file',
description: 'Read the contents of a file',
parameters: {
type: 'object',
properties: {
path: {
type: 'string',
description: 'Path to the file to read'
}
},
required: ['path']
}
}Complete Workflow
typescript
// 1. Define tools
const tools = [{
functionDeclarations: [readFileDeclaration, writeFileDeclaration]
}]
// 2. Create model with tools
const model = genAI.getGenerativeModel({
model: 'gemini-2.5-flash',
tools
})
// 3. Start chat
const chat = model.startChat()
const response = await chat.sendMessage('Read package.json')
// 4. Handle tool calls
const calls = response.response.functionCalls()
if (calls?.length) {
const results = []
for (const call of calls) {
// Execute tool
const result = await executeFunction(call.name, call.args)
results.push({
functionResponse: {
name: call.name,
response: result
}
})
}
// 5. Send results back to LLM
const finalResponse = await chat.sendMessage(results)
console.log(finalResponse.response.text())
}Summary
- Function Calling = LLM requests tool execution
- Declare tools using JSON Schema
- Execute tools and return results to LLM
Next
Learn about tool interface design: Tool Interface Design →