What is Function Calling - Enabling ChatGPT to interact with structured data from the outside
Function calling is a new feature provided by OpenAI in its Chat Completions API. It allows developers to use JSON Schema to describe functions and enables the model to determine whether to call these functions based on user input (Prompt) and which function to call. It then returns a JSON object that conforms to the function signature.
The process of function calling can be divided into three main steps:
-
When calling the OpenAI API, provide a list of functions and user input (Prompt). The model will parse the user input (Prompt) to determine which function to call and identify the correct parameters required for calling the provided function.
-
Use the JSON object returned by the OpenAI model to call the corresponding function.
-
Send the result of the function back to the model for summarization or formatting, making it more understandable for humans.
Function Calling Benefits and Use Cases:
-
Function calling allows developers to retrieve structured data from the model more reliably, rather than just obtaining a piece of text. This facilitates connecting the model's output with external tools or APIs to achieve more complex functionalities.
-
Function calling provides developers with more flexibility in controlling the model's behavior by offering different functions for different purposes. For example, a "send_email(to: string, body: string)" function can be provided to allow the model to send emails, or an "sql_query(query: string)" function can be provided to let the model execute database queries.
-
Function calling simplifies the process of writing prompts by allowing developers to provide instructions or examples for the model to complete a task, without the need for complex logic or rules. For instance, an "extract_people_data(people: [{name: string, birthday: string, location: string}])" function can be provided to allow the model to extract information about people from a Wikipedia article.