Function calling (also known as tool calling) lets AI models invoke external tools and APIs — fetching real-time data, performing calculations, or integrating with your existing systems.
Model Performance: All chat models support function calling, but Kimi K2.5 offers exceptional tool calling capabilities. Kimi K2.5 is recommended for agentic workflows and complex tool calling scenarios.
Here’s a simple example of how to implement function calling with a weather API:
from tinfoil import TinfoilAIimport json# Initialize client with Kimi K2.5 (recommended for tool calling)client = TinfoilAI( api_key="<YOUR_API_KEY>")# Define the tool/functiontools = [ { "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 requestresponse = 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 functionmessage = response.choices[0].messageif 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)