Home Knowledge Base Function Calling in LLMs

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

CategoryExamples
InformationWeb search, database query, API calls
ComputationCalculator, code execution
ActionSend email, create event, update record
RetrievalRAG search, document lookup

Best Practices

Open Source Alternatives

ModelFunction Calling Support
Llama 3Via special tokens/prompts
MistralNative support
GorillaTrained for API calling
NexusRavenFunction calling focused
function callingtool usejson

Related Topics

Explore 500+ Semiconductor & AI Topics

From EUV lithography to CUDA optimization — search the full knowledge base or chat with our AI assistant.