What is Function Calling?
Function calling (also called tool use) is a capability that allows language models to generate structured outputs that trigger external functions or APIs. Instead of generating text, the model outputs structured parameters for predefined functions.
How It Works
-
Define Functions
- Name and description
- Parameter schema
- Expected behavior
-
User Query
- Natural language request
- Context about task
-
Model Decision
- Determine if function needed
- Select appropriate function
- Generate parameters
-
Execution
- Call external function
- Return results to model
-
Response
- Model uses results
- Generates final answer
Function Definition Example
{
"name": "get_weather",
"description": "Get weather for a location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "City name"
},
"unit": {
"type": "string",
"enum": ["celsius", "fahrenheit"]
}
},
"required": ["location"]
}
}
Use Cases
- API integrations
- Database queries
- Calculations
- Data retrieval
- System actions
Best Practices
- Clear function descriptions
- Validate parameters
- Handle errors gracefully
- Limit available functions
- Secure function execution