OpenAI’s New Function Calling Guide

Janvi Kumari Last Updated : 14 Jan, 2025
3 min read

OpenAI has announced the release of its brand-new Function Calling Guide, designed to help developers extend the capabilities of OpenAI models by integrating custom tools and functions. Based on extensive user feedback, the guide has been revamped to be 50% shorter and clearer, featuring new best practices, in-doc function generation, and a fully functional example using a weather API. This update reflects OpenAI’s commitment to making AI tools more accessible and developer-friendly, empowering developers to leverage function calling more effectively in their applications.

How OpenAI Function Calling Works?

Function calling allows OpenAI models to interact with developer-defined tools, enabling them to perform tasks beyond generating text or audio. Here’s a simplified breakdown of the process:

  1. Define a Function: Create a function (e.g., get_weather) that the model can call.
  2. Model Decides to Call the Function: Based on the system prompt and user input, the model determines when to invoke the function.
  3. Execute the Function: Run the function code and return the results.
  4. Incorporate Results: The model uses the function’s output to generate its final response.
How OpenAI Function Calling Works
Source: OpenAI

The image shows the process of how function calling works between a developer and an AI model. Here’s a step-by-step breakdown:

  • Tool Definitions + Messages: The developer defines the tool (function) and sends a message. In this case, the get_weather(location) function is defined, and the user asks, “What’s the weather in Paris?”
  • Tool Calls: The model recognizes that it needs to call the get_weather function with the argument “paris”.
  • Execute Function Code: The developer (or system) executes the actual get_weather(“paris”) function. The function returns a response, e.g., {“temperature”: 14}.
  • Results: The result from the function ({“temperature”: 14}) is returned to the model along with all prior messages.
  • Final Response: The model uses the function result to generate a natural language response, such as “It’s currently 14°C in Paris.”

Also Read: Top 6 LLMs that Support Function Calling

A Quick Example: Weather API 

Let’s walk through a real-world example using a get_weather function. This function retrieves the current temperature for a given set of coordinates.

Step 1: Define the Function

import requests

def get_weather(latitude, longitude):
    response = requests.get(f"https://api.open-meteo.com/v1/forecast?latitude={latitude}&longitude={longitude}&current=temperature_2m,wind_speed_10m&hourly=temperature_2m,relative_humidity_2m,wind_speed_10m")
    data = response.json()
    return data['current']['temperature_2m']

Step 2: Call the Model with the Function Defined

from openai import OpenAI
import json

client = OpenAI(api_key="sk-api_key”)

tools = [{
    "type": "function",
    "function": {
        "name": "get_weather",
        "description": "Get current temperature for provided coordinates in celsius.",
        "parameters": {
            "type": "object",
            "properties": {
                "latitude": {"type": "number"},
                "longitude": {"type": "number"}
            },
            "required": ["latitude", "longitude"],
            "additionalProperties": False
        },
        "strict": True
    }
}]

messages = [{"role": "user", "content": "What's the weather like in Paris today?"}]

completion = client.chat.completions.create(
    model="gpt-4o",
    messages=messages,
    tools=tools,
)

Step 3: Execute the Function

tool_call = completion.choices[0].message.tool_calls[0]
args = json.loads(tool_call.function.arguments)

result = get_weather(args["latitude"], args["longitude"])

Step 4: Supply the Results to the Model

# Append the model's tool call message
messages.append(completion.choices[0].message)

# Append the result message as a string
messages.append({
    "role": "tool",
    "tool_call_id": tool_call.id,
    "content": json.dumps({"temperature": result})  # Convert the result to a JSON string
})

# Create the second chat completion
completion_2 = client.chat.completions.create(
    model="gpt-4o",
    messages=messages,
    tools=tools,
)

Step 5: Get the Final Response

print(completion_2.choices[0].message.content)

Output:

The current temperature in Paris is -2.8°C.

Best Practices for Function Calling 

To help you get the most out of function calling, here are some pro tips:

  1. Write Clear and Detailed Descriptions
    • Clearly describe the purpose of the function, its parameters, and its output.
    • Use the system prompt to guide the model on when (and when not) to use the function.
  2. Apply Software Engineering Best Practices
    • Make functions intuitive and obvious.
    • Use enums and object structures to prevent invalid states.
  3. Offload the Burden from the Model
    • Don’t make the model fill arguments you already know.
    • Combine functions that are always called in sequence.
  4. Keep the Number of Functions Small
    • Aim for fewer than 20 functions at a time for higher accuracy.
  5. Leverage OpenAI Resources
    • Use the Playground to generate and iterate on function schemas.
    • Consider fine-tuning for complex tasks or large numbers of functions.

To know more checkout OpenAI.

End Note

OpenAI’s revamped Function Calling Guide empowers developers to integrate custom tools seamlessly, making AI more accessible and practical. By simplifying processes, offering clear examples, and prioritizing user feedback, OpenAI enables developers to innovate and build solutions that harness the full potential of AI, driving real-world impact and creativity.

Hi, I am Janvi, a passionate data science enthusiast currently working at Analytics Vidhya. My journey into the world of data began with a deep curiosity about how we can extract meaningful insights from complex datasets.

Responses From Readers

Clear

We use cookies essential for this site to function well. Please click to help us improve its usefulness with additional cookies. Learn about our use of cookies in our Privacy Policy & Cookies Policy.

Show details