OpenAI GPT-4 Function Calling: Unlimited Potential

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what is going on everybody and welcome to a bit of an update and exhibition of this new function calling capability as well as like some of these other API updates uh but mostly things are just faster and cheaper now but the big thing here is function calling and I think this is going to be uh a massive change in programming just in general but also the kind of intersection of programming and Ai and adding sort of intelligence to your programming so what I want to do is just show some quick examples of kind of how I've played with it so far as well as kind of run through their example I don't think that their their documentation is good enough but I think it leaves a little bit to be uh desired I suppose uh in thinking about what are all the things that we really can do with this because this is unbelievably powerful so anyways uh with that let me pull up my example and in this case I'm just importing open AI we will use I thought Json at some point down here but anyway we'll get to that point and then this this notebook I'll put up on GitHub so there will be a link in the description so really I just kind of want to quickly like run through these examples you don't have to follow along or anything like that just know hey this is this is like a possibility now and uh this is thundering and lightning outside apparently this is really cool uh ominous isn't uh anyway so here we go so historically if you wanted to do something like this like for example uh if if you had a user and that was just chatting with gpt4 they might ask you know hey what's the weather like in Boston and if you just sent that to the API via the usual chat completion you'd get a response like this I'm an AI language model I can't provide real-time information blah blah blah blah blah right and it even suggests where you could find that which isn't you know useful but it doesn't actually answer the question but we as programmers we could totally have a function on our end that does pull weather data but to do that you would need deterministic information like what's the location and also did they actually want the weather data for that location right so uh so in this case uh imagine you have and this is the example that openai used and this is like the only one which is kind of a bummer because like I said this is so powerful uh that I think uh we're gonna see some crazy stuff coming from this capability but for for now this is the example so imagine this is your function and somehow you're getting the weather data based on some location and this in this case it's really doing nothing it's just kind of returning the weather info for some location but you could imagine you have a function that gets the weather information based on some location and this is something that gbd4 just can't do for you yet because it's not connected to the internet it can't pull data it can't scrape data but you can as a programmer so imagine you've got this function and you know you if if the user wants weather data you know you need the location that they requested and so you want to be able to pull that sort of structured data from gbd4 and historically while this has been possible with like pre-prompts and kind of making these sort of like forms it's been clunky you've had to do a lot of r d to figure out which which one works best and all that kind of stuff for us here uh it just works um and you can kind of force it to work as opposed to historically like with some of the projects I've been working on sometimes it doesn't work and it's hard if not impossible to like force it to work so anyways to get this to work now what we have here is a new kind of completion where first thing to note is we have a new model uh this is gpd40613 which is like the version number I suppose uh so you will need to use that I forget when exactly the old GPD or the current gpt4 gets deprecated but you do need this for now eventually you won't need this little end piece but anyways so here we still pass the exact same thing that we did before like oh up to this point everything's the same just a slightly different model version but then we add this new thing which is functions uh and in here this is where you describe the function that you may or may not use based on a user's input and this one thing to note does get passed to gpt4 it will suck up your context so if you uh have you you don't you could have as many functions note this is plural so you could have a lot of functions like right now this is just one function but you could have tons of them that are possible so you can already imagine like an actual like you know Jarvis like home assistant you could do all kinds of stuff you could dim your lights you could get the weather you could play music you could play a video you could order something on Amazon right you could do anything right you could have functions for all those things and you could automatically detect and determine and extract what exactly it is the user wants so anyways here you just describe the function uh that you may or may not use so in this case it's get current weather that's the name of the function it should you for all of this again it goes into the context and this version of gpd4 has been trained to handle this type of input and context so it's going to be exceptionally good at this kind of structure but the more effort you put into making this be as descriptive and useful as possible noting that this is part of the context I think you will achieve better and better results so we name it get current weather and that is also just happens to be the name of the function it wouldn't have to be but if uh if gpt4 in this case does detect that this is the function that should be used given a user's input it would be convenient I think if the names do match equally then it will be very easy for you to understand oh we want to run this function but you don't really have the function doesn't even need to actually exist and this is an important an important thing there doesn't have to be a function and I'll show an example at the end of there isn't any function but we're using this functionality to do cool stuff now um so anyway will you describe the function again this is just this is up to you to come up with a good description that will help gpd4 to determine are we trying to run a function or is this some other piece of input because alongside you know playing music and all these other things you could also just ask the agent you know how are you doing today we don't need to run a function for that we're just going to answer that so uh anyways we describe the entire function and then we start defining these parameters and what what this new function calling which I think is a not really an appropriate name but whatever uh all it's doing is attempting to generate the parameters that you will pass to the function that you're describing here so again it's hard to kind of wrap your mind around that or at least it was for me to be like oh this is all we're doing we're just extracting structured data period um and so so now we've got this function now we're going to Define all of the parameters that are in here and basically it's going to be in here in this case we have one parameter and that is location so we just need to know where is this person asking for the weather and then this just further defines that location and here you can start to severely restrict the possibilities of a location for example and then here finally you can say it is required that we have a location but you could imagine in the future that there might be some parameters that are that may be passed but not necessarily required so it's cool that we can even do that finally we get to function called Auto there are three options here you have function call Auto this means gpd4 will kind of automatically determine whether or not you actually want to run you know this function and get output as output that is the parameters to this function or you can pass none and that will essentially force it to not use any of your functions do not you know so if the user just says hey what's going on or whatever don't try to pass it through a function and then you finally can pass a very specific you can pass like name equals and the exact function that you 100 do want it to run and this again is extremely powerful because in the past even if you supplied a form and you told it to fill out this form and you tried to force it to do what you wanted it to do sometimes it just wouldn't or it would change the form or it would change the formatting of the form and then you'd have to parse it or figure out how to part it was so annoying whereas this just forces it to happen I hit some keys I don't think I changed anything but uh anyway so so this is how you build the new completion and this is just this is just Json so this is just how to describe a Json object basically so you can pass anything you want in here so we'll we'll come to that that fits the Json description anyways continue on so I just described all that so then if we are to run this notice that I did function call Auto so we're letting it determine if this function should be run in this case it does determine that the function should be run it's a function call Finish reason and then we come down here um the message this is where you would normally get content but in this case there is no content part of me wishes that you could tell gpt4 to also produce that content I understand we're saving tokens by not doing that but if anyone from open AI is watching which I doubt uh uh it would be nice to be able to say hey populate content or if there is a way someone let me know in the comments down below because you will oftentimes still want the agent to respond in some way besides this but anyways nonetheless we carry on uh and you you still could because you could still pass a query yet again and then just say function none right and get the response it would just cost you one more query and one more weight for that query uh anyway continue on the function call does run we get the argument for location in Boston and then it tells us the name of the function because in this case we just have the one so it's obvious which function it was but you could imagine you could have hundreds of well probably not hundreds because you'd probably blow context but you could have a lot you could have tens probably dozens um okay so so that's pretty cool so we get okay the location Boston then theoretically you pass that through to your your function that uh grabs weather for Boston for example obviously this is hard to read or hard to use so at least in my tinkering this is how I converted it there might be better ways to do it but essentially you can use this this right here is still an it's like an open AI Json object but it's like a specific to open AI so it's their own custom Json object apparently but it does have an attribute that we can or a method that we can convert to dict and then we're just grabbing that function call and then argument which is like literally a function call arguments uh then from here we're loading it via the Json module and then we can print it out and then print a location so in this case we're printing funks so it's just location Boston but then if we really wanted to grab that location Boston now we're ready to do whatever the heck we want to do with it so um not only can we extract information from a user's input so in this case everything that gpt4 needed to populate this so all the structured data that we wanted gpt4 to extract was contained in that user's input but it doesn't have to be so one of the examples I've shown here is the previous video I did was term GPT and the idea was you know hey we used gpt4 all the time to produce uh you know python code or commands that we might run in the terminal what if we just automated that whole process so I'm using a relatively decently long pre-prompt because I I need to do in that case I do a One-Shot learning example and then basically have it fill out you know the rest on its own uh but then I have to parse that and sometimes it doesn't follow what I told it to follow especially if you're using GPT 3.5 which by the way you can use all of this that I'm showing right now I'm using gpd4 but you can also do it through GPT 3.5 which is huge because that's way cheaper faster everything and again the reason I was not using GPD 3.5 was because it did not follow my instructions as well as gpd4 but now I haven't tried with gbd 3.5 but this should work just un like way better I expected it will I bet we can do term GPT with 3.5 now I'll probably end up updating it to do that but this also completely changes basically it it nullifies all of the code up to this point for term GPT because this replaces it entirely so imagine you have a prompt that says um oops well this is my function call so I guess I'll describe that first but this is the prompt it's like how do I install tensorflow for my GPU so in this case I want to get all of the commands to do that and then theoretically we could run them through the terminal but what I really want to extract here is all the terminal commands now if we just let gbd4 answer this it will provide a bunch of text and then it will provide those commands but then we'd still have to extract them and it's possible to do this just by seeking out probably like the the tilde tilty tilde bash and then tilde tilt utility in this case but I would get less wrapped up in in doing that and like well we could do that without doing this but really what instead I'm trying to show is not only can you extract structured data from a user's input we can use gbd4 to respond with extremely perfectly deterministic structured data so this is just an example of that so someone asks how do I install tensorflow on my GPU so okay fine so the message that we pass is literally that prompt and then here I just described it up above but I'll just describe it down here as well the function we're calling is get commands this function doesn't even exist it doesn't exist but but then I describe this function as get a list of bash commands on an Ubuntu machine and in fact I almost probably would change that uh yeah so I changed it up here I think but I forgot to change it down there but get a list of bash commands to run on a Ubuntu something like that might even make more sense but again this is what I mean by this is going to be used in the context so the better you describe this and kind of uh formulate this the better your responses are likely to be but anyways running it this way we then say the parameters type object commands the type of so it's the commands is the parameter name the type in this case is an array and guess what an array can be of any length so so here maybe we're expecting two commands maybe we're expecting 12 commands doesn't matter because this is an array so then the type what's going to be contained in that array it's going to be a bunch of strings and then the description of each little type is a terminal command string uh and then the description of commands is a list of terminal commands that are a list of terminal command strings to be executed again this is all like my first run through and then I attempted to kind of fix it here uh purely because I was kind of wording this in terms of what I wanted to be extracted but really the way that you want to think about this is as if you have a function even if you don't have a function as if you had a function you're describing how that function works and then here you're describing what is that parameter and in this case I'm it's as if I'm still trying to tell gpd4 hey I want you to get a list I think that's a that this will work as you'll see in a moment but I think you can you can prompt it better by envisioning it as you are describing the function that will be passed through because this is how the model's been trained so you want to keep this in mind as you're creating this stuff if you want to have the best results now in this case just as an example a function call is auto so I left it as Auto now if we run this you can see that the reply content has nothing to do with a function call at all instead it just tells me to install tensorflow you'd normally follow these steps right that's not what we want this is not uh structured in any way for us so that's what happens when you do auto but instead we can set it the function call to say name and then we want to actually run get commands so this instructs and informs gpd4 kind of hints and nudges that no no don't respond to me as like an agent respond to me with this function parameters and and doing that changing nothing else it indeed responds in that way and I and again as I go through this I think that I think I could have probably written this description better to describe the function itself not what I wanted it not what I wanted gbd4 to do describe the function itself because again that's not really what it was trained to do but nonetheless despite my uh failings it does respond in the correct way it is a function call it determines that okay these are the arguments this is for function get commands and then now we can go ahead and format it in this way and now you can see here this is that exact list of commands so again Not only was this you you know getting structured data from an unstructured response that what would otherwise have been an unstructured response where we're like eliciting these exact commands and in fact if I go back up here I don't I never even really took a deep look at this I don't know yeah there's some there are some like bash but even here you can see uh this one didn't even use like that bash moniker so again you're going to be looking for you know these cases of double or triple tilde but sometimes it says bash sometimes it'll say python all that it'll be very you just could not you couldn't rely on that you know it was so tedious whereas now you can rely I I I can't quite say it's like going to be 100 accurate but I I think that uh at least again I didn't even do this right and uh I still got exactly what I wanted so it's pretty cool so um yeah so I can't as I wrote here and I still just uh it just puts a big old smile on my face because like this is so powerful that I just I can't wait to see what people do with this because this is just so completely open-ended because you can do like anything and you can properly tie in AI into otherwise deterministic programs which I just before now that has been very hard and very finicky and tough very tough to do uh whereas now it's like no it's totally possible now so uh one more example that I want to show before I'll wrap it up this is already longer than I expected this to be um imagine uh you have the following so this might be another way that you would elicit different types of things through like a form or something like that so imagine you've got user input the input could be whatever it wants but then you want to have various personalities to your AI maybe you've got a nice one a Mean One a sarcastic one whatever this could be as much as you want so in this case what I've said is I just asked a simple question is it safe to drink water from a dehumidifier um and uh and then we just pass it as normal again make sure of course you're using that latest 0613 gbd4 and then again I don't this function doesn't exist I have in this case especially I've known like in this one uh the get commands um this function would eventually be written because you would need to you know os.system or whatever the commands that it comes up with in this case you still would probably have a function because you're going to pick which personality but again it just doesn't have to exist but get varied personality responses so the description of this function correctly written is it ingests the various various personality responses the parameters that we accept we have a sassy and sarcastic and then a happy and helpful but you could put as many as you wanted in here if you know whatever you can think of Talks Like a Pirate whatever and again so string string the description of this string is a sassy and sarcastic version of the response to a user's query a happy and helpful version of the response to a user's query so you can already see where I'm going here and then required is both of them but again we could pass different variations of all of them also different variate some of them wouldn't necessarily be required so when applicable you could have a sassy and sarcastic response and then when it just really doesn't make any sense uh you wouldn't need that or something like that so then again we are going to force the function call and then here we get you know what's pretty hard to read but then we can kind of split those apart and then we can get the sassy and sarcastic version oh sure if you fancy a little microbial microbial cocktail go right ahead but seriously don't um yes that's exactly what I was after a sassy and sarcastic response um very good and then the happy and helpful uh I'm sorry I don't know if that's really good for happy and helpful but it's not recommended to drink water for me anymore while it's extracting water from Air dehumidifiers uh don't filter or a clean water okay so you can see here that okay we just extracted two different variations of a response again this is like uh you know 0.000 of what can be done with this sort of capability but really what I wanted to drive home is it's not just extracting information from a user's input it's it's a it's just creating structured data whether it's from a user's input or from what otherwise would have been the output from gpt4 uh it's just it's really cool and I can't I did not see this coming certainly not this fast um and this is always this has been a thing everyone's been doing it such an obvious thing to create um I can't believe that they actually like further train the model to do this very specific thing um and I I'm just kind of Blown Away I didn't see this coming I don't think that they hinted that they were even doing this so um yeah that's just really cool so uh anyway that's all for now like I said I'll put a link in the description to uh this exact uh notebook but as you can see you can probably come up with your own but getting the kind of structure down and just kind of starting to work with this stuff and kind of getting used because like I said this my first attempt was the term GPT thing where I like I I already made the mistake like I'm not trying to extract I'm not trying to tell gpd4 hey produce me this list I'm actually trying to it already knows that it needs to do the thing based on that description of that function so so anyway just a little stuff like that it's going to take a little bit to get used to like this pair dime um but it's just so cool it's so cool um so anyways if you guys have any questions comments concerns whatever you know the deal feel free to leave them below uh also if you have like ideas or or things that you think um that can be done here that are a little more than what I've already brought up feel free to leave those below because like I said I think there's going to be a lot of uh I hate to use the word like hacky stuff but that's kind of what I mean but not in like a a bad way uh I think there's gonna be really cool applications and like I said I just I can't this is something that I would have expected to come from like an open source model I'm very surprised that they've added this functionality to to gpt4 um so yeah cool see you guys in another video
Info
Channel: sentdex
Views: 203,493
Rating: undefined out of 5
Keywords: python, programming
Id: 0lOSvOoF2to
Channel Id: undefined
Length: 23min 49sec (1429 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.