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.
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.
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.
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:
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.
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:
pip install openai#import csv
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
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
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.
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,
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
{‘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.
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:
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
#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)
{‘name’: ‘Rakesh,’ ‘major’: ‘neuroscience,’ ‘school’: ‘IIT Madras,’ ‘grades’: 8, ‘club’: ‘Neuro Club’} {‘name’: ‘IIT Madras,’ ‘country’: ‘India,’ ‘no_of_students’: 9000} |
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:
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
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
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. |
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.
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.
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.
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.
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.