Introduction to OpenAI Function Calling

Yana Khare Last Updated : 06 Oct, 2023
7 min read

According to Forbes, the AI market is predicted to reach $1,811.8 billion by 2030. Introducing the OpenAI API models like Davinci, GPT Turbo, GPT Turbo 3.5, or GPT 4 is taking the world of artificial intelligence by storm. The introduction of the OpenAI API models like Davinci, GPT Turbo, GPT Turbo 3.5, or GPT 4 is taking the world of artificial intelligence by storm.

Function calling by Open AI
Source: Cobus Greyling | Medium

The AI scene transfigured as the OpenAI API language models came with many features but had some limitations in data extraction. Engineers revealed function calling to overcome this constraint and ease their work. OpenAI function calling is quickly gaining popularity among developers and engineers due to its advanced features.

The Need for Open AI Function Calling

In the tech-centric domain, the Open AI language models dominate all the machine learning models with their chat-for-chat and text-generation models.

Traditionally, engineers used prompt engineering in Open AI API to obtain the appropriate response, and they employed regular expressions (RegEx) for unstructured data. Although RegEx is effective, developers have to use complex prompts, which are time-consuming, to get the desired result.

The introduction of OpenAI function calling in June of 2023 is helping to tackle this issue. It made the OpenAI API more developer-friendly and minimized the need for RegEx. The GPT Turbo 3.5 and the GPT 4 models wisely use the function calling as an extended support, which acts as a blueprint for extracting structured data.

Exploring Open AI Function Calling

Function calling is a reliable way to fully use GPT model capabilities and make the chat completion API more efficient. OpenAI function calling takes user-defined functions as input and generates a better-structured output than RegEx. The developers are accustomed to working with predefined data structures and types. The outputs of GPT 3.5 and GPT 4 models are now regular and organized with user-defined inputs, making the data extraction process smoother for developers. Some advantages of using OpenAI function calling for developers are:

  • Enhanced Results: With the help of structured data, this function converts natural language into API calls and database queries for better results.
  • Customization: Due to the introduction of user-defined functions, the developers can customize the functions for extracting as the developers have control over the functions, unlike in RegEx.
  • Data Extraction: Developers can easily extract complex data using OpenAI function calling because they are accustomed to working with predefined data structures and types.

Getting Started

The organization’s introduction of ChatGPT has changed how people powerfully perceive AI. Due to this massive popularity, many people want to understand its logic. As beginners, here are some trusted resources to get started with OpenAI:

Python Tutorials: By getting started with programming with Python in the right direction, you can learn quickly and do wonders.

Webinars: Multiple great videos are available on the internet for Python and Open AI.

Python API can be learned by building a free chatbot in Python.

Using OpenAI Without Function Calling

To understand the leniency OpenAI function callings bring, we need to look at how GPT 3.5 Turbo model API checks whether the output is consistent without the function calling. First, generate your OpenAI secret key to access all the tools on the website.

Go to the OpenAI website > Create account > Validate Your Account > Go to your account and manage API keys > Create a key.

To use OpenAI without function calling, let’s take an example and test by creating a new notebook in Google Colab Notebook:

  • Install OpenAI using the Google Colab notebook cell.
pip install openai#import csv
  • Write a description for a student.
  • student_one = “Rakesh is a second-year majoring in neuroscience at IIT Madras. He has an 8 CGPA. Rakesh is known for his biotech skills and is a member of the university’s Neuro Club.”
  • Let’s write one in the next cell.

prompt = f'''Extract the following information from the provided text and return it as JSON:
name, major, college, grade, club.
It is the text to extract the information from:{student_one}'''

#import csv
  • Now, let’s program the API to get the response from the prompt.
import openai 
import json
openai_response = openai.ChatCompletion.create(
    model = 'gpt-3.5-turbo',
    messages = [{'role': 'user', 'content': prompt}])
answer = openai_response['choices'][0]['message']['content']
answer_json = json.loads(answer)
answer_json

#import csv
  • It will give us the following output: {‘name’: ‘Rakesh,’ ‘major’: ‘neuroscience,’ ‘college’: ‘IIT Madras,’ ‘grade’: ‘8’, ‘club’: ‘Neuro Club’}

Using OpenAI with Function Calling

Now, we can write functions that suit our needs to get accurate results with OpenAI function calling. It has improved the process of using APIs for developers. Below is the correct syntax with the example for extracting student information to write the OpenAI custom function:

function_one = [
    {
        'name': 'info_of_student',
        'description': 'Get information of student from the text',
        'parameters': {
            'type': 'object',
            'properties': {
                'name': {
                    'type': 'string',
                    'description': 'Name of the student'
                },
                'major': {
                    'type': 'string',
                    'description': 'Major subject.'
                },
                'school': {
                    'type': 'string',
                    'description': 'College name.'
                },
                'grades': {
                    'type': 'integer',
                    'description': 'CGPA of the student.'
                },
                'club': {
                    'type': 'string',
                    'description': 'Clubs joined by student. '
                }}}}]

#import csv

Hooray! You have successfully created your first custom function using OpenAI function calling.

Applying OpenAI Function Calling

After building the function, you can practically implement and write the function with the help of Python using the OpenAI library by creating prompts for data extraction. For instance,

  • Create the information for the new student as you like.
  • Use this code to implement the function calling.
student_info = [student_one, student_two]
for stu in student_info:
    response = openai.ChatCompletion.create(
        model = 'gpt-3.5-turbo',
        messages = [{'role': 'user', 'content': stu}],
        functions = function_one,
        function_call = 'auto'
    )

    response = json.loads(response['choices'][0]['message']['function_call']['arguments'])
    print(response)#import csv
  • Below, you can see that using OpenAI function calling gives us a much better response and easily works for more prominent data students.
{‘name’: ‘Rakesh,’ ‘major’: ‘neuroscience,’ ‘school’: ‘IIT Madras,’ ‘grades’: 8, ‘club’: ‘Neuro Club’} {‘name’: ‘Suresh,’ ‘major’: ‘Astronomy,’ ‘school’: ‘IIT Delhi,’ ‘grades’: 7.6, ‘club’: ‘Planetary Club’}

The code without API functions works fine for a smaller number of students, as you can see above, but OpenAI function calling does wonders for a more significant amount of data, as you can see above.

Multiple Custom Functions with OpenAI Function Calling

You can use multiple custom functions to use the capabilities of GPT Models to the full extent. Now, let’s take the challenge of creating another example of using various functions for college information for students:

  • Create a description and write the function for entering college information.
function_two = [{
        'name': 'info_of_college',
        'description': 'Get the college information from text',
        'parameters': {
            'type': 'object',
            'properties': {
                'name': {
                    'type': 'string',
                    'description': 'Name of the College.'
                },
                'country': {
                    'type': 'string',
                    'description': 'Country of college.'
                },
                'no_of_students': {
                    'type': 'integer',
                    'description': 'Number of students in the school.'
                }
            }
        }}
 ]#import csv
  • Now, generate the college information using GPT and store it in the
#import csv
functions = [function_one[0], function_two[0]]
info = [student_one, college_one]
for n in info:
    response = openai.ChatCompletion.create(
        model = 'gpt-3.5-turbo',
        messages = [{'role': 'user', 'content': n}],
        functions = functions,
        function_call = 'auto'
    )
    response = json.loads(response['choices'][0]['message']['function_call']['arguments'])
    print(response)
  • Multiple functions streamline the workflow of the developers. The
{‘name’: ‘Rakesh,’ ‘major’: ‘neuroscience,’ ‘school’: ‘IIT Madras,’ ‘grades’: 8, ‘club’: ‘Neuro Club’} {‘name’: ‘IIT Madras,’ ‘country’: ‘India,’ ‘no_of_students’: 9000}

Building an Application

Multiple functions can be used to develop an entire application. The power of GPT models using OpenAI function calling is limitless, with the flexibility of using various function calls for creating big applications with more extensive data. Let’s create the application step-by-step by inserting different input data:

  • First, let’s write two Python functions for information extraction.
def student_info(name, major, school, cgpa, club):
    return f"{name} is a {major} student at {school} with a {cgpa} CGPA. Member of {club}."

def school_info(name, country, num_students):
    return f"{name} is in {country} with {num_students} students."
#import csv
  • Then, create a list of student_one, random_prompt, and college_one. Then, generate responses using text in the list and detect function calls by applying arguments and print outputs for all three samples.
info = [
    student_one,
    "Where is Paris?",
    college_one]

for i, sample in enumerate(info):
    response = openai.ChatCompletion.create(
        model = 'gpt-3.5-turbo',
        messages = [{'role': 'user', 'content': sample}],
        functions = [function_one[0], function_two[0]],
        function_call = 'auto'
    )
   
    response = response["choices"][0]["message"]
   
    if response.get('function_call'):
       
        function_used = response['function_call']['name']
       
        function_args  = json.loads(response['function_call']['arguments'])
       
        available_functions = {
            "info_of_college": get_college,
            "info_of_student": get_student
        }
       
        fuction_to_use = available_functions[function_used]
        response = fuction_to_use(*list(function_args .values()))
       
    else:
        response = response['content']
   
    print(f"\nans#{i+1}\n")
    print(response)#import csv
  • Woohoo! You created an application with OpenAI using multiple functions and answered according to the prompt.
ans#1 Rakesh is a student of neuroscience at IIT Madras. He has an 8 CGPA, and they are members of the Neuro Club. Ans #2 Paris is the capital city of France. It is located in the northern-central part of the country, in the region known as Île-de-France. Ans #3 IIT Madras is located in India with 9000 students.

Conclusion

From creating multiple functions to using custom functions for building applications, new tools like the OpenAPI function calling give developers more power to bring their code to life with innovative visions and boost OpenAI-based projects with Function Calling. The future holds pleasant surprises with each upcoming version of the API and the language models constantly trying to grow and enhance the functionalities.

Frequently Asked Questions

Q1. What is the function calling?

A. Function calling is a programming concept that involves invoking a specific function or subroutine within a program to perform a predefined task or operation.

Q2. What is function calling in GPT?

A. In GPT (Generative Pre-trained Transformer) models, function calling is an advanced feature that allows the model to call external functions or APIs based on user input, enabling more dynamic and interactive responses.

Q3. What is function calling in OpenAI models?

A. Function calling in OpenAI models involves calling a set of predefined functions within the model, allowing users to interact with external systems or APIs through the model’s responses. It enables more dynamic and context-aware interactions.

Q4. What is the difference between OpenAI function calling and plugins?

A. The difference between OpenAI function calling and plugins is that function calling is for interactions with external systems, while plugins are specific to the ChatGPT User Interface, used for enhancing its capabilities, such as connecting to external APIs.

A 23-year-old, pursuing her Master's in English, an avid reader, and a melophile. My all-time favorite quote is by Albus Dumbledore - "Happiness can be found even in the darkest of times if one remembers to turn on the light."

Responses From Readers

Congratulations, You Did It!
Well Done on Completing Your Learning Journey. Stay curious and keep exploring!

We use cookies essential for this site to function well. Please click to help us improve its usefulness with additional cookies. Learn about our use of cookies in our Privacy Policy & Cookies Policy.

Show details