OpenAI ChatGPT Functions
A couple of days ago OpenAI team released a pack of updates including new function calling capability. This is the newest approach in AI development services and it could become a game-changer in the Generative AI companies industry. Let`s start with the understanding of what are those ChatGPT functions, how they connected to generative AI technology, and why they are so important.
What is ChatGPT function calling?
Function calling is a new feature that allows Artificial Intelligence AI engineer to describe functions to the ChatGPT language model, enabling the model to respond with a JSON object containing arguments for those functions. This feature enhances the interaction between engineers and the Large Language Model (LLM) and provides a more structured and reliable way to leverage the model’s capabilities in various applications.
So, with the calling of any function, developers can define specific functions and their corresponding input arguments using JSON Schema. E.g., when a user interacts with ChatGPT and asks questions or provides input, the model can detect when a function needs to be called based on the user’s query. In response, ChatGPT generates a JSON object containing the necessary arguments for the identified function.
How does ChatGPT function work?
A function calling feature introduces the new parameter to the OpenAI ChatGPT API. This parameter list of functions. Each function has 3 fields: name, description, and its parameters. The name and description fields describe the name of the function and its functionality respectively.
The parameters field describes the parameters that the function takes. In order for the LLM to be able to generate an accurate function call, these parameters need to be described using the JSON schema specification. Let’s see how we may implement the ChatGPT function calling considering a stock market analysis example.
User input: “What’s the current price of Apple stocks?”
The model then generates a JSON output that can be used to call the get_stock_price function from your code.
When the large language model AI generates a function call in its response, it adds a new field function_call that contains the name of the function that the LLM wants to call and the values of the arguments for that function. We can easily read the value of the function_call field in the response and parse it as JSON.
If the LLM decides not to make a function call, then the function_call field in the response is not set, instead you will find the LLM’s reply in the content field. By checking if the function_call field is set in the response or not, we can know when the LLM is making a function call.
The rules of ChatGPT functions creation
There are several rules that will help you to create functions and avoid AI algorithms confusing.
1. Limit the number of functions
An excessive number of choices can lead to ChatGPT functioning inaccurately. Additionally, function descriptions consume tokens, and every token incurs a cost. Therefore, it is important to offer access to only necessary functions and avoid redundancy.
2. Combine functions
AI works much better when it can call a single function instead of chaining multiple function interactions. If an action requires calls to various functions, it is better to combine them into a single function and allow AI to call that grouping function instead.
3. Input validation
Never assume that function arguments generated by the AI software are correct. Treat all data received from AI as potentially incorrect and validate everything. ChatGPT can make errors just like humans do.
4. Return helpful error messages
Please, remember that ChatGPT can correct function arguments if the error message explains what went wrong. If a specific interaction consistently fails, it is a sign that the prompt or function descriptions need improvement.
Function Calling has opened up exciting possibilities for utilizing natural language understanding as an interface, ushering in a significant advancement. Although there remains ample uncharted territory, this article aims to lay the groundwork for comprehending the mechanics of function calling and its potential to elevate AI integration.