Function Calling in LLMs
What is Function Calling? Function calling allows LLMs to output structured requests to call external functions/tools, enabling them to take actions and access real-time information.
How It Works
User Query: "What is the weather in Tokyo?"
|
v
LLM: {"function": "get_weather", "arguments": {"location": "Tokyo"}}
|
v
System: Execute function with arguments
|
v
Function Result: {"temp": 22, "condition": "sunny"}
|
v
LLM: "The weather in Tokyo is 22C and sunny."
OpenAI Function Calling
Define Functions
tools = [{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get current weather for a location",
"parameters": {
"type": "object",
"properties": {
"location": {"type": "string", "description": "City name"},
"unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}
},
"required": ["location"]
}
}
}]
Call API
response = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "Weather in Tokyo?"}],
tools=tools,
tool_choice="auto"
)
# Check if model wants to call a function
if response.choices[0].message.tool_calls:
tool_call = response.choices[0].message.tool_calls[0]
function_name = tool_call.function.name
arguments = json.loads(tool_call.function.arguments)
# Execute function
result = execute_function(function_name, arguments)
# Send result back to model for final response
messages.append(response.choices[0].message)
messages.append({
"role": "tool",
"tool_call_id": tool_call.id,
"content": json.dumps(result)
})
final = client.chat.completions.create(model="gpt-4o", messages=messages)
Common Function Types
| Category | Examples |
|---|---|
| Information | Web search, database query, API calls |
| Computation | Calculator, code execution |
| Action | Send email, create event, update record |
| Retrieval | RAG search, document lookup |
Best Practices
- Clear, specific function descriptions
- Validate function arguments before execution
- Handle function errors gracefully
- Limit number of available functions (reduce confusion)
- Test with adversarial inputs
Open Source Alternatives
| Model | Function Calling Support |
|---|---|
| Llama 3 | Via special tokens/prompts |
| Mistral | Native support |
| Gorilla | Trained for API calling |
| NexusRaven | Function calling focused |
function callingtool usejson
Explore 500+ Semiconductor & AI Topics
From EUV lithography to CUDA optimization — search the full knowledge base or chat with our AI assistant.