GPTs are nice. Actually, they are awesome. Since the introduction of the first GPT (Generative Pre-trained Transformer) by OpenAI in 2018, its popularity has been on the rise, and it has seen exponential growth in adoption recently.
In the realm of Artificial Intelligence, GPTs are a specific implementation of Large Language Models (LLMs). GPTs are pre-trained on large data sets of unlabeled text (meaning, data elements that are not tagged with distinct identifiers or classifications) and are capable of generating human-like content based on its training. GPTs are great at tasks like generating text, summarizing text, and generating code based on your prompts. This is understandable, since the model is pre-trained on publicly available static data sources like Wikipedia, publicly available research papers, GitHub etc. However, can you make it work with dynamic data sources like APIs?
The answer is ‘Yes’, and the way to achieve that is by using the feature in GPT called Function Calling.
What is Function Calling?
Function Calling is a powerful feature of GPT through which the model can be fed dynamic data. The model can then use this dynamic data as the context to generate relevant content. Function calling empowers these GPTs (or Large Language Models, in general) to interact with real-time information to provide context-aware content. The latest models have been trained to detect when to use function calling to make the process completely seamless and transparent to users.
Use Cases of Function Calling
Function calling can be used in a wide range of scenarios where dynamic data or real-time information needs to be provided to the model for generating content. Following are some of the use cases where the power of Function calling can be utilized: –
- Create an assistant (e.g. Chatbot) that needs to call an external API (e.g. Weather forecast API), to respond to the user on their question (e.g. “Give me some suggestions on what to wear while visiting the National Park tomorrow?”)
- Convert natural language into API calls (e.g. convert “Who are my top 10 customers who have purchased from me at least 5 times?” to get_top_customers(min_purchases: int, limit: int) and call your internal API)
- Summarize the output of an API that returns a large amount of data
How does it work?
![Sequence of events in Function Calling](/wp-content/uploads/2024/05/GPT-Function-Calling-WhiteBlue-Cloud-Services-1024x483.jpg)
Following is the sequence of events that should occur when function calling is in action:
- User submits a query in natural language (e.g. plain English).
- The query is sent to the model along with the additional information of what all functions (APIs) are available.
- GPT, based on its logic, can choose to call one or more functions from the list of functions we have sent to the model.
- The model sends back the list of functions with their parameter values.
- Parse the response from the model and call the function(s) with the provided parameters.
- Receive the response from the function(s).
- Invoke the model once more and send the responses received from function(s).
- Model summarizes the responses and frames the final response in natural language.
- Receive the response from the Model.
- Show the response to the User.
Best Practices in Function Calling
As they say, “With great power comes great responsibility”. It is extremely important to follow some best practices while using this powerful feature to avoid unintended outcomes.
- When calling APIs of business applications, ensure that the APIs have built-in authentication. This ensures that the system only shows the content authorized for the user to the user. For example, if you have an API that gives the account balance of a user, it is important to ensure that the API is providing the account balance of only the user who is invoking the API.
- Always build user confirmation flows before taking an action that impacts the state of the resource that the API is exposing. For example, if you are integrating with an API that transfers money to another account, always build a confirmation flow that gives user a chance to review and take an informed decision on whether to go ahead with the operation.
- Last, but not the least, follow API Best practices while creating your REST APIs. Though you can add metadata to document your APIs that you want GPT to consider, if you follow the best practices in naming your APIs, it can help in avoiding some of these metadata, which in turn helps in reducing the number of tokens that are sent to GPT.
Summary
Function calling provides a way to take your structured enterprise data and convert it into an unstructured form. It provides a way to teach AI models how to access real-time information or data beyond their training set. For a business user, it opens up possibilities to ‘talk’ to your enterprise data using natural language. In the coming days, architects will undoubtedly find novel use cases to utilize the power of function calling for providing dynamic and context-aware responses to the users of their business applications using natural language.
Related: Check out this article (Unlock the Power of Azure OpenAI’s Function Calling) which provides a sample implementation on how you can practically use this feature.