Function Calling

Function calling (also known as tool calling) allows AI models to interact with external tools and APIs, enabling them to perform actions beyond text generation. This feature lets you build AI applications that can fetch real-time data, perform calculations, and integrate with your existing systems.
Model Performance: All chat models support function calling, but Qwen3 Coder 480B and Qwen 2.5 72B offer exceptional tool calling capabilities. Qwen3 Coder 480B is specifically designed for agentic coding workflows and provides state-of-the-art performance for complex tool calling scenarios.

Basic Example

Here’s a simple example of how to implement function calling with a weather API:
from tinfoil import TinfoilAI
import json

# Initialize client with Qwen3 Coder 480B or Qwen 2.5 72B (recommended for tool calling)
client = TinfoilAI(
    api_key="<YOUR_API_KEY>"
)

# Define the tool/function
tools = [
    {
        "type": "function",
        "function": {
            "name": "get_weather",
            "description": "Get current weather for a specific location",
            "parameters": {
                "type": "object",
                "properties": {
                    "location": {
                        "type": "string",
                        "description": "The city and state, e.g. San Francisco, CA"
                    }
                },
                "required": ["location"]
            }
        }
    }
]

# Mock weather function (replace with real API call)
def get_weather(location):
    return f"The weather in {location} is sunny, 22°C"

# Make the initial request
response = client.chat.completions.create(
    model="<MODEL_NAME>",
    messages=[
        {"role": "user", "content": "What's the weather like in New York?"}
    ],
    tools=tools,
    tool_choice="auto"
)

# Check if the model wants to call a function
message = response.choices[0].message
if message.tool_calls:
    # Process each tool call
    for tool_call in message.tool_calls:
        if tool_call.function.name == "get_weather":
            # Parse function arguments
            args = json.loads(tool_call.function.arguments)
            location = args["location"]
            
            # Call the function
            weather_result = get_weather(location)
            
            # Send the function result back to the model
            messages = [
                {"role": "user", "content": "What's the weather like in New York?"},
                message,  # Assistant's message with tool call
                {
                    "role": "tool",
                    "content": weather_result,
                    "tool_call_id": tool_call.id
                }
            ]
            
            # Get the final response
            final_response = client.chat.completions.create(
                model="<MODEL_NAME>",
                messages=messages,
                tools=tools,
                tool_choice="auto"
            )
            
            print(final_response.choices[0].message.content)
else:
    print(message.content)

Multiple Tools Example

You can define multiple tools for more complex workflows:
tools = [
    {
        "type": "function",
        "function": {
            "name": "get_weather",
            "description": "Get current weather for a location",
            "parameters": {
                "type": "object",
                "properties": {
                    "location": {"type": "string"}
                },
                "required": ["location"]
            }
        }
    },
    {
        "type": "function",
        "function": {
            "name": "calculate",
            "description": "Perform mathematical calculations",
            "parameters": {
                "type": "object",
                "properties": {
                    "expression": {
                        "type": "string",
                        "description": "Mathematical expression to evaluate"
                    }
                },
                "required": ["expression"]
            }
        }
    }
]

def calculate(expression):
    # Safe evaluation of mathematical expressions
    try:
        result = eval(expression)
        return str(result)
    except:
        return "Error: Invalid mathematical expression"

# The model can now choose between weather and calculation functions
response = client.chat.completions.create(
    model="<MODEL_NAME>",
    messages=[
        {"role": "user", "content": "What's 15 * 23 + 45?"}
    ],
    tools=tools,
    tool_choice="auto"
)

Best Practices

  1. Choose the Right Model: Among the models offered on Tinfoil API, Qwen3 Coder 480B provides state-of-the-art agentic coding and function calling capabilities, while Qwen 2.5 72B offers excellent general-purpose function calling
  2. Clear Descriptions: Write detailed function descriptions to help the model understand when to use each tool
  3. Parameter Validation: Always validate function parameters before execution
  4. Error Handling: Implement proper error handling for function calls
  5. Security: Never execute untrusted code - validate all function arguments
  6. Testing: Test your functions independently before integrating with the AI model

Common Use Cases

  • API Integration: Fetch real-time data from external APIs
  • Database Queries: Retrieve information from your databases
  • Calculations: Perform complex mathematical operations
  • File Operations: Read, write, or process files
  • System Commands: Execute system operations (with proper security measures)
  • Third-party Services: Integrate with external services and platforms

Model Comparison

Different AI models have varying capabilities for function calling due to their training data, architecture, and fine-tuning approaches. Some models were specifically trained on function calling examples and structured outputs, while others prioritize conversational abilities or reasoning tasks. The model’s size, training methodology, and post-training optimization all influence how well it can understand function schemas, generate valid JSON, and decide when to use tools appropriately.
ModelBest ForQuality
Qwen3 Coder 480BAgentic coding and tool callsBest
Qwen 2.5 72BComplex tool workflows, multiple functionsExcellent
Mistral Small 3.1 24BSimple to moderate tool callingGood
DeepSeek R1Complex reasoning, function calls, advanced tasksPoor
Llama 3.3 70BSimple function calls, conversational AIGood