Skip to content
OpenAI Function Calling: Examples to Get Started

OpenAI Function Calling: Examples to Get Started

In the ever-evolving landscape of artificial intelligence (AI), OpenAI has emerged as a game-changer. Its innovative approach to function calling is revolutionizing how developers interact with AI models. This article delves into the intricacies of OpenAI function calling, providing a detailed guide on its potential and how to leverage it for your applications.

📚

Understanding OpenAI Function Calling

Consider an API call submitted to https://api.openai.com/v1/chat/completions with the OPEN_API_KEY defined in the header. The aim of this call is to generate a JSON file that can be used to schedule a meeting. The JSON document sent to the model would look something like this:

{
  "model": "gpt-3.5-turbo-0613",
  "messages": [
    {
      "role": "user",
      "content": "Schedule a meeting with John Doe next Tuesday at 3 PM."
    }
  ],
  "functions": [
    {
      "name": "schedule_meeting",
      "description": "Please schedule a meeting.",
      "parameters": {
        "type": "object",
        "properties": {
          "attendee": {
            "type": "string",
            "description": "Attendee for the meeting"
          },
          "date": {
            "type": "string",
            "description": "Date of the meeting"
          },
          "time": {
            "type": "string",
            "description": "Time of the meeting"
          }
        }
      }
    }
  ]
}

The model generates a JSON output that can be used to call the schedule_meeting function from your code.

Real-World Applications of OpenAI Function Calling

Consider a stock market analysis example. The user utterance is defined as: "What's the current price of Apple stocks?". The parameters defined are: ticker_symbol.

{
  "model": "gpt-3.5-turbo-0613",
  "messages": [
    {
      "role": "user",
      "content": "What's the current price of Apple stocks?"
    }
  ],
  "functions": [
    {
      "name": "get_stock_price",
      "description": "Get current stock price",
      "parameters": {
        "type": "object",
        "properties": {
          "ticker_symbol": {
            "type": "string",
            "description": "Ticker symbol of the stock"
          }
        }
      }
    }
  ]
}

The model then generates a JSON output that can be used to call the get_stock_price function from your code.

The Power of Function Calling in OpenAI

Function calling in OpenAI is a significant leap in the right direction. It allows the Large Language Model (LLM) to structure output not only into natural conversational language but also into a format that can be consumed by another system, as opposed to a human. This feature structures output for machine consumption in the form of an API, as opposed to human consumption in the form of unstructured natural language.

There are a few considerations to keep in mind. Programmatically, the chatbot or Conversational UI will have to know that the output to the LLM must be in JSON format. Hence, there will have to be some classification to detect output type. A predefined template will have to exist for input to the completion LLM. The JSON template guides the LLM on how to populate the values. The parameters that should be populated must be well defined.

Real-World Applications of OpenAI Function Calling

OpenAI function calling has a wide range of applications. For instance, it can be used to create chatbots that answer questions by calling external APIs. It can also convert natural language into structured JSON data or extract structured data from

text.

Consider a travel booking example. The user utterance is defined as: "I need to book a trip from Bonn to Amsterdam for my wife, mother, and my two sons and daughter. I will also be joining them. The airline must fly direct." The parameters defined are: destination, departure, number_people, and travel_model.

{
  "model": "gpt-3.5-turbo-0613",
  "messages": [
    {
      "role": "user",
      "content": "I need to book a trip from Bonn to Amsterdam for my wife, mother and by two sons and daughter. I will also be joining them. The airline must fly direct."
    }
  ],
  "functions": [
    {
      "name": "book_travel",
      "description": "Book travel",
      "parameters": {
        "type": "object",
        "properties": {
          "destination": {
            "type": "string",
            "description": "Your travel destination."
          },
          "departure": {
            "type": "string",
            "description": "From where are you traveling"
          },
          "number_people": {
            "type": "string",
            "description": "How many people are traveling"
          },
          "travel_mode": {
            "type": "string",
            "description": "What mode of travel will it be."
          }
        }
      }
    }
  ]
}

The model then generates a JSON output that can be used to call the book_travel function from your code.

OpenAI Function Calling: A Step Towards the Future

OpenAI's function calling capability is a testament to the rapid advancements in AI technology. It allows developers to more reliably get structured data back from the model. This feature is a new way to more reliably connect GPT's capabilities with external tools and APIs.

In June 2023, OpenAI announced updates including more steerable API models, function calling capabilities, longer context, and lower prices. Developers can now describe functions to gpt-4-0613 and gpt-3.5-turbo-0613, and have the model intelligently choose to output a JSON object containing arguments to call those functions.

This feature allows developers to create chatbots that answer questions by calling external tools, convert queries into function calls, convert natural language into API calls or database queries, and extract structured data from text. These use cases are enabled by new API parameters in the /v1/chat/completions endpoint, functions and function_call, that allow developers to describe functions to the model via JSON Schema, and optionally ask it to call a specific function.

A Concrete Use Case of OpenAI Function Calling: Solving Complex Mathematical Problems

OpenAI's function calling feature is not just a theoretical concept, but a practical tool that can be applied to solve complex problems. Let's dive into a concrete example that demonstrates its power. This example is based on a Twitter thread (opens in a new tab) by Santiago (@svpino).

Consider a complex mathematical question: "What's the result of 22 plus 5 in decimal added to the hexadecimal number A?" This question requires adding numbers in different formats, which can be quite challenging. However, with OpenAI's function calling feature, we can solve it by combining functions with prompts.

Here's how it works:

  1. Define two functions: one that knows how to add two decimal numbers, and the other that knows how to add two hexadecimal numbers. These functions are defined as part of the Chat Completion call.

  2. Implement these two functions. They parse the arguments and perform the sum.

  3. Call OpenAI's API repeatedly until a "stop" reason is found. Whenever the API finishes with a "function_call" reason, call the specific function and pass the results back to the API.

The output from the script will be:

  • 22 + 5 = 27 (decimal)
  • 27 + A = 31 (hex)
  • The result of adding 22 and 5 in decimal, and then adding the hexadecimal number A, is 31.

This shows that GPT used both functions to compute the final result! You can find the code for this example here (opens in a new tab).

This example demonstrates the power of OpenAI's function calling feature. It allows developers to define and use custom functions, enabling the model to solve complex problems that require multiple steps and different types of calculations.

Want to Generate Any Type of Charts Easily with the Power of ChatGPT? You can give VizGPT (opens in a new tab) a try, where you can use ChatGPT prompts to create any type of chart with No Code!

How to Create Charts with VizGPT (opens in a new tab)

VizGPT: Create Charts with the Power of ChatGPT (opens in a new tab)

Other Latest ChatGPT Updates for June 13, 2023

Besides the update for API function calling, OpenAI has recently announced (opens in a new tab) a series of groundbreaking updates to their API models, introducing new capabilities and more steerable versions of GPT-4 and GPT-3.5-turbo. These updates are set to revolutionize the way we interact with AI models, offering enhanced functionality and cost-effectiveness.

Context Boost to GPT-3.5-turbo

One of the most notable updates is the introduction of a new 16k context version of GPT-3.5-turbo. This means that the model can now consider up to 16,000 tokens of history when generating responses, significantly improving its ability to handle complex and lengthy conversations. This context boost allows for more nuanced and contextually aware responses, enhancing the overall user experience. This could be a significant blow to LangChain, but the future implications are yet to be seen.

Pricing Update

The pricing for GPT-4 and GPT-3.5-turbo has also been updated. GPT-4 now costs $0.03/1K tokens for 8K context and $0.06/1K tokens for 32K context. GPT-3.5-turbo comes in at an affordable $0.002/1K tokens. This competitive pricing makes these advanced AI models more accessible to developers and businesses of all sizes.

These updates mark a significant milestone in OpenAI's journey, bringing us one step closer to a future where AI models are more powerful, efficient, and accessible. As OpenAI continues to innovate and push the boundaries of what's possible with AI, we can expect to see even more exciting developments in the near future.

Safety Measures

In line with their commitment to safety, OpenAI is working diligently to mitigate potential exploits. They advise developers to use trusted tools and include user confirmation steps before performing real-world actions. This ensures that the AI models are used responsibly and that user data is protected. This could potentially mean that your favorite ChatGPT Jailbreak prompts will stop working.

Wrapping Up

This concludes our comprehensive guide on OpenAI's function calling feature. As we've seen, this feature is a powerful tool that can be used to solve complex problems and create more interactive and intelligent applications. As OpenAI continues to innovate and introduce new features, the possibilities for what we can achieve with AI are endless.

Frequently Asked Questions

  1. What is OpenAI's function calling feature?

    OpenAI's function calling feature allows developers to describe a function, and the model generates a JSON output containing arguments. This feature doesn't call any function itself, but it generates the JSON that can be used to call a function from your code.

  2. How does OpenAI's function calling feature work?

    Developers define functions as part of the Chat Completion call. The model then generates a JSON output that can be used to call the specific function from your code. The API is called repeatedly until a "stop" reason is found. Whenever the API finishes with a "function_call" reason, the specific function is called, and the results are passed back to the API.

  3. What are the applications of OpenAI's function calling feature?

    OpenAI's function calling feature has a wide range of applications. It can be used to create chatbots that answer questions by calling external APIs, convert natural language into structured JSON data, extract structured data from text, and solve complex problems that require multiple steps and different types of calculations.

📚