Function Calling
Function Calling
Function calling allows the model to generate structured output that calls external functions.
How It Works
- You define functions in your request using the
toolsparameter - The model decides when to call a function based on the conversation
- The model returns a
tool_callsobject with function name and arguments - You execute the function and return the result using
role: "tool" - The model continues with the function result
Defining 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, e.g. 'London'" }, "unit": { "type": "string", "enum": ["celsius", "fahrenheit"], "description": "Temperature unit" } }, "required": ["location"] } } } ]}Example Conversation
Step 1: User asks a question
{ "model": "gemma-4-26b", "messages": [ {"role": "user", "content": "What's the weather in London?"} ], "tools": [...]}Step 2: Model responds with tool call
{ "choices": [{ "message": { "role": "assistant", "content": null, "tool_calls": [{ "id": "call_abc123", "type": "function", "function": { "name": "get_weather", "arguments": "{\"location\": \"London\", \"unit\": \"celsius\"}" } }] }, "finish_reason": "tool_calls" }]}Step 3: You execute the function and return the result
{ "model": "gemma-4-26b", "messages": [ {"role": "user", "content": "What's the weather in London?"}, {"role": "assistant", "content": null, "tool_calls": [...]}, {"role": "tool", "tool_call_id": "call_abc123", "content": "{\"temperature\": 15, \"condition\": \"cloudy\"}"} ], "tools": [...]}Step 4: Model responds with natural language
{ "choices": [{ "message": { "role": "assistant", "content": "The current weather in London is 15°C and cloudy." }, "finish_reason": "stop" }]}Tool Choice
Control when the model calls functions:
| Value | Behavior |
|---|---|
"auto" | Model decides (default) |
"none" | Never call functions |
{"type": "function", "function": {"name": "fn"}} | Force specific function |
Python Example
from openai import OpenAI
client = OpenAI( api_key="your-api-key", base_url="https://cryptgpt.co/v1")
tools = [{ "type": "function", "function": { "name": "get_weather", "description": "Get weather for a location", "parameters": { "type": "object", "properties": { "location": {"type": "string"} }, "required": ["location"] } }}]
response = client.chat.completions.create( model="gemma-4-26b", messages=[{"role": "user", "content": "Weather in Paris?"}], tools=tools, tool_choice="auto")
# Check if the model wants to call a functionif response.choices[0].message.tool_calls: tool_call = response.choices[0].message.tool_calls[0] print(f"Function: {tool_call.function.name}") print(f"Arguments: {tool_call.function.arguments}")