OpenAI API Function Calling - Hands-on Tutorial for AI Developers!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
open AI recently announced something called function calling and in this video we're going to explore everything that you need to know about function calling with Hands-On code which I'll share it in the YouTube description to start with what is function calling that's what's something that we need to understand if you are a developer and you have been developing applications or web applications using open AI especially chat GPT type applications you would have been struggling with wanting which is to get a proper Json output there are a lot of libraries that were already trying to help you to get a proper Json output a Json output is like this for example you want like there is a lot of things in the message and the output that you want is just like this it's not a very straightforward from chat GPD and the open AI APA has not been helping for this until now so to get that to have the model intelligently choose to Output a Json object that contains arguments to call a function a particular function that you want to call is what the function calling is trying to do it's actually more than that but in a simplest way you have a prompt that you send it to API and that opening API gets a gives a response back to you and that response is not a text response but you can extract and Json object from that like certain objects from that is what function calling at the foundation level is trying to help you now this is a new way for you to connect the GPT capabilities to external tools and apis in fact to charge gbt plugins so these models have been fine-tuned to both detect when a function needs to be called based on the user input and also to respond with a Json that adheres to the functions signature that sticks to the functions signature so that's very important like if you have a template of a function that takes a particular argument this will help you get that in the structured data back now let's jump into our examples now the function oils can help you do three things I'll show you the examples the first one is it can help you create chat Bots like intelligent chat Bots that can answer questions by calling external tools for example you can call an API or you can call chat GPT plugins or you can call anything that you want it could be inside link chain it could be outside long chain it could be anywhere simply with an open a APA so first thing where you are going to use function callers to create chat Bots or to create communication platforms where the user is asking question that cannot be answered by default only with charge apt or gpt4 so it has to go to an external tool and in this case you need to make an APA call and that is where you can use function call the second one is when you want to convert your natural language into an APA call so the first one is you're going to call an API based on the output the second one is to convert your natural language into an APA call or a database query like there is a question and you want to convert into a SQL query you can do that with function calls and finally the foundation like I said you have a text and you want to extract structured data like a Json from the text you can use function call these are like three different aspects of how you can use function calls but at the foundation like we discussed it's basically trying to convert your text to response into a Json so that you can play with that object what are the examples the first example is let's say you want to build a system in which a user can send an email like part of the conversation they can send an email like for example if the user sends a message saying email Anya to see if she wants to get coffee next day next Friday so for a typical email you need three things one you need to know how to call the email which is the function the second one you need to know the two which is to whom you are sending it and the third one is the body of the email one of the most important aspect so leaving out the function call in itself so you need two main components one to whom you are sending the email second one is what is that email that you want to send if you look at this particular text it's very obvious as a human being that you want to send the email to Anya which is the two address and what is the body of the email like do you want to get coffee next Friday that's the body of the email so now this is one of the examples where you can take this text you can send this text to chat CPT and charge GPT will give you the email body like this the email function which is you already have caught and the two address which is Anya's email ID from you know some other database that you can look into and the body of the email that would probably say you want to get coffee next Friday this the like the simplest example the next one is if you want to call weather API so typically when you ask questions like what's the weather and weather like in Bengaluru chat GPT will not give you an answer that's the example that we are going to see but to call a weather API you need to know the location of for or city for which you want to see the weather and you want to see what unit you want the result to be in Celsius or Fahrenheit different countries measured whether in different unit and especially in India it's very common to measure weather in celsius like if you see in Bangalore so people would say 32 degree celsius so you are giving this Choice as well so now it's a weather API let's say that whether API is called some APA and you're going to use a function called get underscore current underscore weather and it's going to use two arguments and chat GPT or gpt4 API can give you this as a response if you use function calls the third example is a very popular example that everybody is almost interested in these days is to convert a natural language question into a function or a SQL query so if you are if you have a chatbot internally in your company you you are going to let people ask questions so you can ask a question who are my top 10 customers this month and the function call will enable you to build a system that can translate that natural language question into a function like this or a SQL query like this how many orders did Acme Inc place last month that can be converted into SQL query thanks to function calls and finally the most foundational simplest example is you have got a bunch of text like for example you've got a Wikipedia article and all you want to know is the details or the date of birth of the people mentioned in the Wikipedia article so you can have a function here and that function needs things like the name of the people mentioned in the Wikipedia article their birth date and the location these can be extracted from the Wikipedia article thanks to the function call that is available in opening APA now that's enough of talking now we are going to get in the Hands-On coding example so this example is going to give you a complete information about how you can use function call in your own code for this we are using open a package but if you want to make this also as part of let's say your HTTP request you can use the rest of the part of the code to do it but right now I'm using open AI APA to show you how to do that first pip install APA sorry open AI which is a python library that helps you connect with open a APA once you have installed open a python package then you can import open Ai and we are going to use certain elements that open a actually suggested in their documentation tenacity retry weight random exponential and stop after attempt is to make retry calls for the API if you are trying this code on your terminal this will give you a colored output you know if you're going to have a chat it will give you a colored output that's why we are using colored and of course to make any function called we need request next we need to choose what model that we are going to use we are going to use the GPT 3.5 turbo 0613 which is the latest model of the charge GPT version we're going to use that the next one is you need to have your open a API key so that can be added by open a DOT APA underscore key equals the API key that you have got please make sure you do not hard code the APA key for example this could be part of your environment variable where you can use input osos.n1 and extract that as well or even if you write this you can next delete the particular component so that that is not available as part of the code in itself but the most important thing is do not do not do not give that as part of the key the next thing that you need to do is you need to it's up to you you can either make the call directly or you can have like a utility function which is specified in opening a cookbook to have this entire setup like where you can create a function that's called chat completion request I'm not going to get into the details here but simply this is like a utility function that wraps around how you make calls to open AI API so this is where the open a APA request is and you can make the call here so that's that's simply what this is doing but again you can do it without this also you can directly use the open a package and do it instead of using request also that's up to you completely so if you want to use request you can use this particular piece if you do not want to use request then you can directly go with opening APA the next thing is we are going to Define a simple class that's for conversation because we're trying to build like a chat bot where you can chat and it gives you a response and it also has got a role so you can Define this class conversation I think very serious in this like I said if you are using within terminal it is going to give you color but everything other than that is a very simple open a APA I've got a separate video on open a APN itself if you have detail if you have questions I'll link that in the YouTube description so you can check it out now is the main part where you have to first specify a function specification you have to tell chart GPT how the function is going to look like and that's where you're going to specify okay the name of the function is get underscore weather underscore get underscore current underscore whether this is the name of the function then you give a description about what the function is and you're going to specify the parameters of the function what type of parameter it is it is a type object and what are the properties the properties are one it is a location the second one it is a format the location is what we want like a Bengaluru a San Francisco California whatever it is and the format is where we are going to specify it should be either Celsius or Fahrenheit and inside location you're going to specify the type and the description and description is how charge GPT will understand what to pick from that response or the text and the format again you have got to give the enum like if you are going to manually give the categories and the description what the template temperature unit to use infer this from the user's location so if I'm trying to ask for Bengaluru it would assume that I am I live in India and India people mostly use Celsius or if I'm asking it from the US it would assume that okay in the US people usually use it or denote it in Fahrenheit so I should represent it in Fahrenheit so that is a that is a decision that open a API will make here based on the function call and the particular jio what are the required Fields you need at least location and format this is how you specify a function specification this function specification is going to tell open AI APA to particularly look for these particular details and we're going to just see so now we have already defined the class so we're going to invoke it like we're going to create an object conversation and we're going to add a user message and then say user is saying who's that this is the role and what is the weather like today in Bengaluru Bangalore without a function column first calling this without a function call so I'm going to say chat completion request which is what we call we defined at the start chat completion request and I'm sending the conversation history here that's a conversation conversation history and the function is none so we are not giving any specification here and then the response is 200 which means we have got a successful response and if I print that particular text you can see that it says I'm sorry but as an EA language model I do not have real-time data I suggest checking in reliable weather website or using a weather application to get the current weather information for Bangalore this is not the desired application what we want to do is we want to use charge gp2 to extract these information and ultimately call a weather APA where we will send two information one is the location second one is the format so that the weather API can give us the response back that is not possible without function call so what we are going to do now is we are going to do the same call we're going to do the same chat completion request making calls to open a APA with the same conversation but now with functions so this functions where does it come from it comes from the function specification that we just gave within which we specified what the function should be what are the arguments that we are looking for what are the types of the arguments and what kind of details it has got that's what we have specified in the function specification and that is a call that we are going to make here so we make the call same information same conversation but with functions unlike before where we do not give the function once we send that we get the response back once we get the response back it's a Json object we can print that object and then see we have the chat ID we have the object we have the timestamp when it was created we have the model when it was the model that was used to create this and we have the choices within that you can see the message inside the message you can see what is the role the assistant which means it's a response back from chat GPT for the user message the content there is no but the content is done because it's it's ultimately like we are getting the function call the function call the name of the function is get current weather and the arguments is location is Bangalore and the format is Celsius finish reason why did it finish it's a function call it's not the message response content and the total prompt tokens 102 completion token is 25 total token is 127 for which we are going to be built and I can literally import Json and load the response in Json and get the location and format which now I can pass to a weather API to get the weather for Bengaluru if you are building an application to show you another example let's look for another region where they are going to use Fahrenheit so I'm going to say what is the weather so let me copy this text come back here what is the weather like today in um let's say New York okay say this is a message that get added to the conversation now I'm going to call this this is with the function call I'm going to call this and once I have called this you can see it's going to give me the entire response and okay in New York also they use celsius I don't know I thought it might be fair in it maybe it's not cool anyways import Json Json printed and then it gives New York and Celsius you can now ask for a different city San Fran Cisco add it ask it print the response San Francisco format Celsius I think maybe something wrong with the cell the format thing I'm not sure I don't I don't think in the US they use celsius um my bad um but maybe let me ask for Berlin Berlin function column it gives the Celsius for everything so maybe something is wrong with that but you get the point the whole point is to send a question to charge GPT but instead of making charge apt answer it you get structured data from that which can be now send it to an external EPA and like I said there are more things that you can do whether you want to convert the natural language into a database query or extract structured information from a given page this all could be done using function calls this is a tremendous update if you have been thinking something like I'm thinking this is the territory where land chain was an Undisputed King now looks like chart GPT is entering into this you can literally use land chain tools for the same thing but now charge GPT has or open a has stepped into that territory I'm really looking forward to see how this is going to play in the longer term but for now um it seems like an exciting thing especially if you are a developer who had to call external apis based on charge GPT or open a responses this is an amazing opportunity to use function calls I hope this was helpful to you I will link the Google collab notebook in the YouTube description and I will also link my open AI API video in the YouTube description and also the documentation if you have any questions let me know in the comment section otherwise thank you so much for watching the video see you in another video Happy prompting
Info
Channel: 1littlecoder
Views: 4,341
Rating: undefined out of 5
Keywords: ai, machine learning, artificial intelligence
Id: PPsptBoHIiE
Channel Id: undefined
Length: 17min 20sec (1040 seconds)
Published: Wed Jun 14 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.