Function calling allows LLMs to:
const functions = [
{
name: 'get_weather',
description: 'Get current weather for a city',
parameters: {
type: 'object',
properties: {
location: {
type: 'string',
description: 'City name'
}
},
required: ['location']
}
},
{
name: 'get_stock_price',
description: 'Get current stock price',
parameters: {
type: 'object',
properties: {
symbol: {
type: 'string',
description: 'Stock ticker symbol'
}
},
required: ['symbol']
}
}
];
// 1. User Message
{
role: 'user',
content: 'What's the weather like in London?'
}
// 2. LLM Response with Function Call
{
role: 'assistant',
content: null,
tool_calls: [{
id: 'call_abc123',
type: 'function',
function: {
name: 'get_weather',
arguments: '{"location":"London"}'
}
}]
}
// 3. Function Execution Result
{
role: 'tool',
content: '{"temperature": 18, "condition": "cloudy"}',
tool_call_id: 'call_abc123'
}
// 4. Final LLM Response
{
role: 'assistant',
content: 'The weather in London is currently cloudy with a temperature of 18°C.'
}
// Clear Match
User: "What's the stock price of Apple?"
LLM: Calls get_stock_price with {"symbol": "AAPL"}
// Ambiguous Match
User: "How's AAPL doing today?"
LLM: Could call get_stock_price or might give general info
// No Match
User: "Tell me about TypeScript"
LLM: Regular response, no function call
{
name: 'search_products',
description: 'Search product database',
parameters: {
type: 'object',
properties: {
query: { type: 'string' },
category: { type: 'string', optional: true }
}
}
}