AI / Machine Learning
-
June 21, 2023

How to use OpenAI Function Calling feature on GPT-s API’s

OpenAI's ever-growing repertoire of features has just launched a new feature: "functions calling". In this blog post, we're going to discuss how you can leverage this feature to extend your existing product's capabilities by providing a natural language interface for interacting with them, and also show a simple Node.js application making use of it.

What is "OpenAI Function Calling"?

OpenAi function calling is a new feature that allows the OpenAI models, like GPT-3.5 turbo and GPT-4, to suggest you to call functions written in your code in response to natural language inputs from users. It works by telling the model which functions are available on your system, describing their purpose and parameters. OpenAI can then identify when a user's input corresponds to these descriptions and call the associated function. This brings a new level of interaction and user-friendliness, enabling non-technical users to interact with technical functionalities using simple language.

Implications and Opportunities

This "functions calling" feature brings to the table a new channel to interact with your product. You can now integrate a chat assistant into your applications and execute tasks based on natural language inputs. This can be an excellent way to engage with users who prefer conversational interfaces or need a more accessible way to interact with your application's features.

By providing an additional control layer, you can also authenticate users and even personalize their experience. You can provide the OpenAI's chat model with the context of the conversation, making it possible to differentiate between different users, their preferences, and access rights. This allows for a secure, yet user-friendly, way to execute sensitive actions like purchases or modifications of user data.

Use Cases of OpenAI Function Calling

Now that we know the concept and potential of the OpenAi Function Calling feature, let's dive into some practical scenarios. It's important to understand that these applications are viable because you retain full control over the channel of interaction. In other words, you can implement safeguards like user authentication, ensuring that each user can only trigger actions they are authorized to perform. This gives you the freedom to personalize and secure the experience without compromising the convenience that this natural language interface offers.

In the following section, we will explore a range of use cases, from e-commerce platforms to smart home devices, to illustrate how the OpenAi Function Calling feature can be integrated and the value it can provide. Remember, these are just examples—the possibilities are limited only by the functions your software can perform and the natural language descriptions you can provide for them.

Marketplace E-commerce Platform

With the help of function calling, users can interact with an online marketplace in a more intuitive and accessible manner. By providing descriptions to the code functions that perform tasks such as search and purchase, users can trigger these functions through natural language instructions. For example, a user could ask, "Find me blue women's running shoes in size 7," and the AI-powered app can execute the function that performs this search. Similarly, the user could say, "Buy this pair of shoes and ship it to my home address," and the app can call the function to place an order.

Online Banking

Customers of online banking platforms can manage their accounts using natural language requests. They could say, "Transfer $500 from my savings to my checking account," or "Show me my last five transactions," and the AI-powered app could execute these requests. Importantly, the app can handle authentication functions as well, ensuring that sensitive transactions remain secure.

Customer Support

By integrating this feature into a customer support platform, the AI model can execute support functions in response to customer requests. A customer could ask, "Reset my password," or "Show me how to use feature X," and the AI model could choose to execute the requested action, saving time and effort for both customers and support staff.

Smart Home Devices

OpenAi Function Calling can also be integrated into smart home devices to allow users to control their devices with natural language commands. Users could say, "Set the living room temperature to 72 degrees," or "Play my morning playlist in the kitchen," and the AI model can suggest to call the appropriate functions on the devices.

Data Analysis Tools

In data analytics platforms, users could request specific analyses or reports using natural language. They could say, "Show me sales data for Q1 2023," or "Generate a report on customer churn rate for last year," and the AI model can execute the appropriate functions to provide the requested data.

Hands on: a To-Do list using Node.js and Slack

In this example we will walk you through the process of building a simple To-Do list application written for Node.js.

For the setup, you will need to import the latest version of the official package openai (currently 3.3.0).

In this example the user can interact with the application through Slack with a chatbot powered by OpenAI API and function calling.

We will split the code into three parts, each contained in a separate file - todo_list.js, functions.js, and server.js. Let's dive into each of these files, step by step.

Step 1: Creating todo_list.js for Core Functionality

Our journey begins with the todo_list.js file. This is where we will implement the core functions for our application that we will tell GPT API that are available:

CODE: https://gist.github.com/diego-suarez/970d48dc61367933c21e43d8938858dd.js?file=todo_list.js

This file exposes three functions: getTodoList, addTodoItem, and removeTodoItem. These functions perform operations on the todoList array just as an example, but here you may want to perform real actions on your system.

Step 2: Defining Function Schemas in functions.js

Next, we need to provide the schema definition for each function in the format that the API expects. This is where functions.js comes into the picture. The schema is essentially a blueprint of each function and will help the API understand the function's purpose, its parameters, and what it returns.

CODE: https://gist.github.com/diego-suarez/e1a19ece2d2ab22b95189531ddaf5421.js?file=functions.js

It's critical to provide a clear description of the function's purpose, specify the parameter type, and offer a brief description of what is expected from the function.

The parameters object should be a JSON Schema that you can see in detail in its definition here.

Step 3: Setting Up server.js

Finally, let’s dig into server.js. This file contains the necessary setup for OpenAI API configuration and the application logic related to user interaction and function calling.

CODE: https://gist.github.com/diego-suarez/5b16f255d5741369afd7cdcf2c05ac66.js?file=setup_server.js

The setup initializes the OpenAI API with the API key. We then import the functions and their schema we previously defined.

We define an asynchronous function getOpenAiResponse which calls the OpenAI API with the proper payload and returns its response. The payload includes an array of past messages (history), a description of the assistant's role, and the schema of the functions that the assistant can execute.

CODE: https://gist.github.com/diego-suarez/1918053d91031b7fb8c78e290f8d8d31.js?file=get_response.js

Now, let's define a callFunction that executes the desired function based on the function call returned by the OpenAI API (will be used later):

CODE: https://gist.github.com/diego-suarez/b99bcbaec1ca7e575a62105ca3fc33ab.js?file=call_function.js

Finally, we listen to user messages, add them to the history, and ask OpenAI for the next action. Depending on the output, we either execute a function call and answer with its response, or just send a text message to the user.

CODE: https://gist.github.com/diego-suarez/b432cad3a690fca713284f95be0ebce9.js?file=on_message.js

Here, we get the response to the user message from OpenAI Api. If the API indicates a function call, we use callFunction to execute it and append the function's response to the conversation history, denoting the role as "function". This is an important step as it allows us to track the actions taken by the application and maintain a coherent conversation history.

Otherwise, if the response is simply a message, we add it to the history as a message from the assistant, and finally relay the response back to the user.

And there you have it, a simple yet efficient To-Do list application powered by the OpenAI Function Calling API! This article serves as an elementary guide to help you understand the API's functionality. For the complete source code, you can check the full GitHub repository here.

One last highlight, the new function_call parameter

With the introduction of the new function-calling feature, an additional parameter, named 'function_call', has been added alongside the ‘functions’ and the already existing ones. Its default value is set to 'auto'. This parameter accepts either 'auto' or a specific function name as its value. When 'auto' is set, the GPT model determines whether to execute a function call or to simply respond with text. Conversely, if a specific function name is provided (should match one from the list of available functions passed on the functions parameter), it compels the API to invariably respond with a function call to that specific function.

This feature can be beneficial, for instance, if it's already known that the response will be used in a function call, i.e., in a specific format. If you desire a preformatted function call with your preferred data types as arguments, this feature serves that purpose well. For the purpose of this example, however, we are using the default 'auto' setting. Consequently, the API's response will either be a function call or a text message.

For the record, here is the response when the model decided to call a function:

CODE: https://gist.github.com/diego-suarez/b8057a2492ac2757575c6bfa69c5fe49.js?file=function_call_response.js

And this one is when the response is a text message:

CODE: https://gist.github.com/diego-suarez/3c22ab7818d32c15965548bbcd14ef86.js?file=text_response.js

Final thoughts

The introduction of OpenAI's Function Calling feature starts a new era where technical functionalities and user interaction become seamlessly integrated. By providing a natural language interface that interacts with your application's functionalities, OpenAI enables the creation of more engaging and user-friendly products. The potential applications are vast, spanning multiple sectors including e-commerce, banking, customer support, smart home devices, and data analysis tools.

Throughout this article, we demonstrated how to leverage this feature by creating a simple To-Do list application in Node.js. This application utilizes OpenAI's chat model to interpret user inputs and call the corresponding functions.

An important feature we've explored in this guide is the 'function_call' parameter. This parameter grants developers flexibility in controlling the API's response. When set to 'auto', the model can choose whether to execute a function call or simply respond with a text message. However, specifying a particular function name forces the API to make a function call to that function, preserving its parameter structure. This feature is particularly useful when it is predetermined that the response will serve a function call, thereby offering a preformatted response that matches your preferred data types and arguments. It can also provide a specific structure or format for the response, eliminating the need for manual prompt specification and subsequent parsing on your end.

Nevertheless, it's crucial to remember that the use cases we've discussed are only a fraction of what's possible. The true potential of OpenAI Function Calling extends beyond the examples shared here and is only limited by the functions your software can perform and how you define them. As you experiment with this new feature, we encourage you to let your imagination lead and explore new applications. The possibilities are as limitless as the potential of AI itself. Happy coding!