ChatGPT Function Calling: Bridging the gap between AI and API Alex, June 20, 2023June 20, 2023 Since releasing ChatGPT to the public, OpenAI has continued to astound the internet. However, one feature in particular is currently making quite a stir among developers and tech fans alike. It’s called function calling, and it’s completely changing the game. But what does ChatGPT function calling entail? How does it function? How would it benefit your projects? And, most importantly, how do you get started? Don’t worry, we’ll answer all of these topics and more in a fun, beginner-friendly manner. We’ll even go through several instances to ensure you completely understand this new feature and how to use it. So, whether you’re an experienced developer or just curious about the potential of AI, let’s dig in and discover the amazing possibilities that ChatGPT function calling may provide. It’s time to translate those sophisticated AI principles into simple talks! What exactly is ChatGPT function calling? ChatGPT function calling is a groundbreaking feature available in latest OpenAI models such as gpt-3.5-turbo-0613 and gpt-4-0613. Rather than the conventional unstructured natural language, it entails employing OpenAI’s Language Models (LMs) to organise outputs into machine-friendly formats. In essence, you can specify a function in your API call, and the model will provide JSON output with arguments based on your input. It’s crucial to note that the conversation completion API doesn’t really execute any functions; instead, it generates JSON that you can use to call a function from your code. How can ChatGPT function calling assist me? ChatGPT function calling has some advantages over other techniques. For starters, it assists you in structuring the output of a conversation into a manner that another system can readily absorb. This feature arranges output for machine consumption in the form of an API request, as opposed to the unstructured natural language generally provided by a chatbot. Assume you have a chatbot that you wish to utilize to send an email. Instead of processing the chatbot’s natural language response to extract the essential information, you can generate a structured JSON output using ChatGPT function calling. This can be delivered directly to an email sending API without extra processing. Furthermore, function calls can be used to turn natural language into structured JSON data and to extract structured data from text. This is especially handy if you wish to automate operations or if your project includes working with organized data. Using the OpenAI API to call functions Before we go into the details of the ChatGPT function calling capability, it’s crucial to understand how the OpenAI API works in general. This API provides the entry point for interacting with OpenAI’s strong language models, and the good news is that it is simple to build. Here’s a little primer to help you get started: How to Install: First and foremost, you must install the OpenAI package. If you’re using Python, you can accomplish this with the pip command ‘pip install openai’. NPM may be used by Node.js users: ‘npm install openai’. API keys: After you’ve installed the package, you’ll need to obtain your API keys. (You can obtain one for free at https://platform.openai.com/account/api-keys). These are one-of-a-kind identifiers that allow your app to communicate with OpenAI’s models. To generate your API secret key, log onto your OpenAI dashboard, click on your profile icon at the top right, then go to ‘View API Keys’ and select ‘Create new secret key’. API requests: With your keys in hand, you may begin performing API calls! This will be accomplished by using a server-side language such as Python or JavaScript (Node) and sending them to your desired model endpoints. Always test your endpoints before proceeding. Retrieve custom APIs: It’s now time to get your bespoke APIs from JavaScript frameworks like React, Vue, or Angular. Present information: Last but not least, the user requests and model responses must be presented in a visually pleasing User Interface (UI). This stage takes your program from a simple API client to a fully functional, user-friendly application. How can OpenAI function calling be used in your projects? It is extremely simple to use the OpenAI function calling capability into your projects. Here’s a quick rundown of how you can accomplish it: Prepare your API call: Submit an API call to ‘https://api.openai.com/v1/chat/completions’, including your OPENAPIKEY in the header. Explain the function: Describe the function you want to run in the JSON document you submit to the model. This description should include the name of the function as well as the parameters it requires. User request: The user’s chatbot request should be organized so that the model can parse the essential information to populate the function’s arguments. Here’s an example of how to use this functionality for emailing: { "model": "gpt-3.5-turbo-0613", "messages": [ {"role": "user", "content": "Send an email to John Doe asking for the monthly report?"} ], "functions": [ { "name": "send_email", "description": "Please send an email.", "parameters": { "type": "object", "properties": { "to_address": { "type": "string", "description": "To address for email" }, "subject": { "type": "string", "description": "subject of the email" }, "body": { "type": "string", "description": "Body of the email" } } } } ] } The user’s request in this case is “Send an email to John Doe asking for the monthly report?” The API call describes the method “sendemail,” which takes three parameters: “toaddress,” “subject,” and “body.” The model’s output will be a structured JSON file that can be used to send an email using an API. While utilizing this functionality, keep the following points in mind: The LLM (Large Language Model) output must be in JSON format, therefore your chatbot or conversational UI must be able to handle this. For the input to the LLM, a preset template must exist. This instructs the LLM on how to fill in the values. The parameters that must be filled out must be carefully defined. A parsing error occurs from any failure in this area. An illustration of the power of ChatGPT function calling: a travel booking To illustrate further, take the following travel booking scenario: destination, departure, numberpeople, and travelmodel. The user’s request in this situation could be: “I need to book a trip from Paris to Rome for five people.” We’d rather take the train.” The model’s JSON input might look like this: { "model": "gpt-3.5-turbo-0613", "messages": [ {"role": "user", "content": "I need to book a trip from Paris to Rome for five people. We prefer to travel by train."} ], "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’s output will then be structured JSON, which can be used to book the trip via an API. Wrapping it up As we enter the conclusion of our study, it’s evident why OpenAI’s ChatGPT function calling is such an effective tool in the field of conversational AI. This pioneering feature combines natural language processing and machine-readable outputs, making it a terrific asset for both developers and producers. We can extract structured data, generate JSON output, or even invoke functions that interface with other systems or APIs by simply changing how we interact with ChatGPT. This adaptability opens up a whole new field of possibilities, making it easier than ever to integrate artificial intelligence into real-life applications. ChatGPT function calling is poised to become a vital element of our arsenal, whether it’s for sending emails, engaging with our calendar, tasks, booking trips, or a variety of other things. It’s not just a significant step forward for developers; it’s also a step toward a future in which AI is more interactive, helpful, and, ultimately, more human. So, as you begin your next project, keep in mind the power that function calling may provide. AI Talk ChatGPT