GPT-4 Function Calling Complete Guide | NEW ChaptGPT Models | OpenAI

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
opening eyes function calling API lets you generate structured data with chat GPT so if you want plug-in like functionality outside of the cheche BT interface you want to follow along I'll be covering all these topics and if you want a written version of this tutorial I'll leave that in the description as I said before function calling lets you reliably get structured data back from the GPT models for example you might ask for Json and instead of getting just a Json you get you know something before and something after but you want just a Json every single time that's where the GPD 3.5 turbo 0613 and gpd40613 models come in these will reliably return just Json so let's look at this quick example you might have a message like what is the weather like in Louisville Kentucky and based on your available functions like get current weather get historical weather get forecast weather and so on you want to figure out how to actually call that based on that user message so you pass the message as well as the functions and some extra metadata along to the 0613 apis and you'll get back Json every single time and it has a function call which is the function that it thinks you should call based on the input and the parameters to call it you can actually select which function you want it to fill in the data for I'll get into that later though now let's actually use the API I'll show you how to do this with python you need to pip install openai if you haven't get your chat CPT API key that's going to be exported in open AI underscore API underscore key has an environment variable if you don't know how to do this it's just export open AI underscore API unsure key then the variable so this is pretty much the same as a normal completion you have to import open head at the top or also we're going to be using Json so import Json this is the core of it main difference here is functions and function call so then also model the model is GPT 3.5 turbo zero six one three there is also of course the gpt4 equivalent which is you know just gbt-4-0613 and then the unique thing is functions and function call so functions is a list of the functions that are available and you have the name description and parameters I believe it says parameters is optional but I don't believe it is probably always want to have per annivers description and the name so we have this get current weather function up here and it just Returns the same thing every time basically takes a location and a unit uh just returns this Json this is just for an example here and then you can see here I have the description which the API is going to use to determine you know if this was multiple functions which one to use and then also we have if we have this Auto parameter set then it may or may not actually suggest a function call I have a prompt in the article that you can use to generate this Json schema but it's pretty straightforward with the name description parameters and then you know the different types of the parameters and if they're required or not so check out the prompt in the article if you want to generate this yourself or you can do it manually then going to function call equals Auto so it's Auto by default um and if you set it to Auto then you may or may not get a function suggestion you can set this manually I'll go over that a little bit more detail later let's go ahead and call this and see what happens with auto set with these parameters as you can see in choices and content is null normally for a you know regular completion um this would be set to the chat GPT response but instead we have function call and function call is set to the you have the name here which is get current weather because it's saying that we should use the get current weather function and then arguments which are the arguments for that function you also see that the finished reason is function call before we get into the more advanced usage and troubleshooting let me show you how you could actually call the function given that you have this response from chat GPT you can see I'm running this interactively with python attack I then the file name so I'm going to add this code right here and all this code is doing it's creating a dictionary where the name of the function maps to the actual function and then what I'm going to do here is extract the data from the chatpt response response specifically the name and the arguments so that's going to be the name and arguments here let's use json.loads to load this into the argument and function to call here I'm extracting the function that we need to call from the dictionary because we have the name mapping to the function variable we can just do this right here the function name from the API it will extract it from the dictionary and then we can call it like a normal function and pass it the arguments that chatpt gave us so I can do this real quick I'll copy this in since I run this interactively and show you what happens when I do this so we have again name arguments I'll paste this in and we'll see the response as you can see got the expected response from the function that we defined up here all right now let's finally dive deeper and go over these different things let's go over the function called parameter options multiple functions at once in a single request any structured data and non-existent functions as well as generating the function called schema so right here as you can see I have the different function calls and I have this set to none and what none is going to do none means that the model does not call a function and it just responds normally to the end user so it'll be a regular completion so let's see what this looks like here when we have this set to none I'm passing the Golden Gate Bridge is a suspension bridge spanning the Golden Gate blah blah blah blah and then my functions here which is process proper nouns it just processes an array of proper nouns and I'll get into why I'm doing this specific function in a second but function call is none and this is what we get right here we get a message back with content so content is for regular completions it is actually valid at Json it looks like but the reason that we don't want to do this if we expect structured data is this as I said before there could be some data before and after that is not Json so so if you were to you know try to load that you would fail in this case it would work but maybe it's inconsistent so this could be dangerous now there's also Auto and auto does kind of what you might expect so Auto will just choose between a regular completion or calling the function just like plugins work just have plugins work so if it thinks that a plug-in or a function would be useful it will say that you should call that and you can see which one it is by the finished reason you also just checked to see if the function call is present here but finish reason is another way so let's call this with Auto and see what happens as you can see finished reason is function call and also we have the function call here which is you know process proper nouns with the arguments set right here so that's great but there's also one more way maybe you know for a fact you want a function to be called you don't want chat to BT to maybe call it maybe not and in that case that's where function call set to the function name comes in so you can specifically say I know for a fact I want to call Process proper nouns just give me back the arguments for that function so let's call this one more time and it's not going to be any different as you can see it did call Process proper nouns and it did give you the correct arguments back clicking there I should clarify that the finished reason is only function call when you're using Auto and it decides to use a function call in this case since we're specifying a function call to be processed proper nouns this function specifically we're still getting the finishers and stop and real quick it's going to pause here and look at the other finished reasons it's right here there are a lot of cases where you need structured data from chat GPT and you don't need to write actual functions to use the function API so let's go over an example of that let's say that you're creating a web scraper and you're looking at the first sentence in the Golden Gate Bridge article on Wikipedia this is the first sentence you can extract the proper nouns from this article just by creating this dummy process proper nouns function you don't actually write it if you don't know what a proper noun is quick English lesson it's just a noun that serves as the name for a specific Place person or a thing so you know that's like London or open AI or Nick so those are proper nouns and if you look at this sentence that's Golden Gate Bridge Golden Gate and San Francisco Bay and Pacific Ocean so in order to extract those you can just come up with this dummy function and that will be enough so let's go look at this code right here so I have this right here it's coming it out it's not a real function you don't need to actually write anything here and so right here I'm defining the name the description it just processes an array of proper nouns this is kind of just hinting at chat apt that this is what the input should be and for parameters you can see it's nouns description of the parameter is just an array of proper nouns to be processed so just with that I didn't actually Define a real function I can call this function and you'll see as before that I will get all the proper nouns extracted from it I have arguments nouns and Golden Gate Bridge Golden Gate San Francisco Bay and Pacific Ocean as expected there are a lot of cases where you actually need to make multiple function calls at once so for example what if a user says what events should I go to in San Francisco on May 25th based on the weather if you have a get weather and a git events function know which one do you call well you kind of need to call both so you can emulate multiple function calls with a single request shout out to pre-nova for coming up with this solution here but basically all you need to do is pretend that you have a multi-funk function as I said before you don't actually need to have these functions you're just figuring out how to get a structured data back from the API so we're going to specify that the name is multifunct down here we're specifying it here so the description doesn't matter I believe I can drop that but in the properties we this is basically just what you would put for the get weather function Alone um so instead we have properties is get weather and instead of get weather you know we have all the specific parameters for that function itself which are you know location and for get events we also have location and date you know all the types you can look up Json schema if you don't know how to work this and again I'll get to my prompt in a second to show you how you can automatically generate this with chat GPT but basically we're just saying that the required parameters for multi-funk are all of the parameters for get weather and all the parameters for get events so after we do that I'll run this and as you can see in the output now we have the arguments for multi-funk which again is get weather and also get events so get events maps to its actual arguments right here which has the city and actually hallucinated the date but you know since it's structured you don't have to worry about the hallucination here because you can do you know get weather City and you don't even need to bother with the date pretend it's not there get events uh has the city as well as the date if you want to generate the function called Json schema quickly you can just use chat GPT for that I have this prompt in the article I Linked In the description so I just copied this pasted this I have my function right here that I added at the end it generated this Json schema for me back in the code I added added my schema for the function this is the function right here it just squares a number and then I'm asking it what is the 7 squared which isn't even a proper sentence I just realized because I have a kind of a typo there but let's see if it works anyway I'll run it as you can see arguments are num seven so it did work here's some general troubleshooting advice that might help I did see a lot of people saying that there was uh erroneously suggestions for a function called to python which doesn't exist here's one of the posts I saw on the community opening AI forum and some solutions for this could be specifically specifying a function to call with the function call parameter instead of maybe using Auto this could also apply to any other issues you have you know if chat with the apis not using the function you want obviously you can specifically say use this one this this function in particular uh you know it's saying to use Python you can try to use this as well if that doesn't work you can also try to add a dummy python function to your functions list with a description to kind of like poison that function for example here I added the python function with a description this function doesn't exist you can try other variations like the world's going to end if you call this function to try to avoid that then you can also use the gpd40613 function calling API or model um not sure how much better this will be but that's one tip and then also variations obviously for your function names and descriptions if you're uh you know more descriptive or use different language maybe that's like more commanding it could improve your results
Info
Channel: Nick Franck
Views: 549
Rating: undefined out of 5
Keywords: chatgpt, gpt, gpt-3.5-turbo-0613, gpt-4-0613, plugin, function calling, function call, chat gpt, openai, sam altman, gpt4, gp3, langchain
Id: N0pWog_6dIg
Channel Id: undefined
Length: 15min 30sec (930 seconds)
Published: Tue Jun 20 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.