GPT Function Calling: Absolute Game Changer

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's going on guys welcome back in this video today we're going to learn about GPT function calling which is an absolute Game Changer when it comes to working with the language models from open AI so let us get right into it [Music] all right so we're going to learn about GPT function calling in this video today and this is going to be absolutely mind-blowing for those of you who have not heard about this yet GPT function calling essentially allows us to use the large language models the GPT models from open AI to call our own functions in an intelligent way and why is this important why is this impressive because of course the GPT models don't have access to current data they don't have access to the internet they don't have any access to real-time data they are trained up until September 2021 so everything after that is basically invisible to the GPT models so if you want to know something about the current weather about the stock price of a company the latest stock price of a company if you want to know something about current events GPT cannot answer that question however your functions can so you can query the Yahoo finance API you can query a weather API you can do something that gets some current data for from the internet and what you can do now with GPT function calling is you can use the large language models to call functions that you define based on a user prompt to then take the output of that function and give an intelligent response so this would be for example me asking GPT what is the stock price of Microsoft right now and then GPT would go call my function it would get the current stock price and the response would then be the latest stock price of Microsoft is this so it would take my prompt turn it into a function call call the function get the results take the result and turn it into an answer that is the power of function calling and we're going to go right into the code here but first of all we need to make sure that we have everything set up properly so what you need to have is you need to have the open AI package installed so you're going to need to install open AI using pip then of course you're going to need an open AI account or something or you need to have an API key that allows you to access some openai account um and once you have your open AI account you can just log in you can go to view API keys and you can create a new secret key or you can use one that you already created in the past and the important thing here of course is requests to the API cost credits so you will need to have some credits you will need to have some money however if you create a new account you're going to get I think at least it was like that for a time for some time you will get five dollars of free credits that you can use and also I think all the all the requests we're going to do in this video today are going to be below one cent or maybe one or two cents it's not going to be a lot so you don't need to spend ten dollars to follow along with the video here um so what you want to do is you want to create an API key you want to store it into a file and then we can get started by saying import open AI import Json this is core python now let me just zoom in a little bit here uh and in my directory here I have the key txt file which is containing my open AI API key and what I'm going to do now is I'm going to say open AI dot API underscore key equals and then open key.txt reading mode dot read like this this will now get my API key and load the API key into open AI so that I can actually send requests that will spend my money essentially so that is the setup now to see how it works immediately to not have to code too much before we get any impressive results we can just go to the documentation here for the GPT models and we can scroll down I think here to function calling and we can copy the example so we can go and copy this code here this is the example here and what this example does is it creates a sample function here a dummy function get current weather and all this function does is it returns a dictionary here a Json object essentially so this is no actual weather API that we're calling here later on in the video I will show you an actual example with stock prices but this now is just a dummy function so it's just going to return some weather information with a location it's just going to take the parameter put it into the dictionary here and that's it and it will also take the unit and display it here that's all we're going to do with that function so again this is just a dummy function nothing too impressive and then what we're going to do is we're going to have this function here where all the magic is going to happen I'm going to copy it here and I'm going to explain to you what is happening here you can also copy it yourself from this documentation here but I think it makes more sense to show it like that than to code it from scratch here because you will get first of all faster results and then we can also talk a little bit more about what is actually happening and not focus too much on just coding down what is already part of the documentation so the basic idea that we have here is we have a function run conversation it doesn't have to be a function you can also just write the code directly but essentially what we do here is we have some messages and the messages here are the message history so this is nothing new this is just the ordinary open AI chat completion structure we have a role user I think the other one was assistant or chatbot or something and one was system and here we just have the user asking a question what's the weather like in Boston and of course you can change this to whatever you like but we're now going to use this example here what's what's the weather like in Boston and then the important thing here is the functions list here so what we do here is we Define a list in this list we have a dictionary and in this dictionary we have some key value pairs essentially we have the name of the function in our case it's get current weather and we describe two chat GPT or to the GPT model what this function looks like what it does and um what the parameters are so all of this is text none of this is actually a reference to a function none of this is actually referencing anything this is just text so we're just saying to GPT look there is a function it has this name this is what it does we have those parameters those are mandatory this is the description for the individual parameters know that and when the user asks something think about whether dysfunction could help you with a solution or not and then call it or don't call it so that's the basic thing we just provide it with the information that such a function exists so we say the function is get current weather what does this function do here you can just use plain text to describe what the function does get the current weather in a given location and of course if you have a complex function it makes sense to specify exactly what the function does so the chat GPT knows or the GPT knows when to call that function and when not to call that function um and then we have the parameters key here which points to a dictionary and here we have basically just type object and then the properties here we have the parameters location and unit location is a string and what does this string contain the city and state for example San Francisco CA and then we have the unit it's of type string but it's also an enumeration it can be Celsius or Fahrenheit and then the required argument here is location so all of this I mentioned it for the third time I think all of this is just text there's nothing else but text here no reference no functionality no no code does not know what this function actually looks like we have it here but it doesn't know that it only knows that some function exists that does this with those parameters here and then what we do is we ask the open AI API that want to do a chat completion we say that the model is just GPT 3.5 because gpt4 is now limited you have to sign up for a waiting list I'm not part of that team that can use the gpd4 API yet so I use GPT 3.5 so you specify the model you specify the messages this is just ordinary chat completion but the new thing that you do now is you uh Define the function so you you pass the functions to this chat completion and you set function call to Auto so this basically just tells you that or this basically just says okay if you decide that a function should be called This is the functions or those are the functions that you have call the function and then as a response here we get a message and what we can do here is we can check if this response message contains a function call so if we get from this response message function call then basically chat GPT has decided to call a function now this doesn't have to be the case if the model doesn't perform well or if the model does perform very well and recognizes that it actually doesn't have to call a function so for example if you ask something like is Boston a sunny City in general this is not something that should call this function so in this case it shouldn't call the function in this case here what's the weather like in Boston it should call the function so if it calls the function we're going to get into this section here and all we do here is basically we just map the function string to the actual function so this is now python code we're now no longer in the open AI World we're now in Python again we get a response it tells us okay it wants to call a function now the functions that we know are get current weather the string is mapped to a function the string doesn't have to be the same by the way so I can also specify a different function name here and point to the same function so I can also call this weather function pointing to the actual get current weather function here um so then we get the function name we translate the function name to an actual function we call this function with the arguments that we get from this function call here and then what we do is we basically just call the function and uh we get the response from the function call in our case this is going to be this Json object here but it could be anything it could be some string it could be a number whatever and what we do then is we take that we append the message here uh two so so to the messages that we already have up here we append now the response message and um we basically feed the output into the chat GPT model again to then format it so why do we do that we could also just stop and just print the results but we want to have an answer from the chat bot so we don't want to just get a Json object we want to take that Json object feed it into chat GPT one more time to get a response that is actually in readable text forms so something like the weather in Boston is like this right now so that is that I'm going to run this now so that you can see that this actually works and I think this will return a Json object queue with a response so if you want to get just a response you just have to go for Content uh and what you can see here is now the current weather in Boston is 72 degrees Fahrenheit and sunny but it's also windy and this is just because of course we have here uh the static response so this is not actually a uh weather API that we're occurring here but we're going to change that now so this is the example from a documentation we're now going to implement our own example which is going to query the stock market the Yahoo finance API and it's going to give us the live information about stock prices so what we're going to do first is we're going to install a module called why Finance so pip3 install y Finance in my case already installed and we're going to then say import y Finance as YF and then what we want to do here is we want to define a function get stock price get stock price for a ticker symbol so we're not going to pass a company name obviously we're going to pass a ticker symbol the symbol so instead of passing Microsoft we would pass msft instead of passing Apple AAPL instead of passing Nvidia nvda so you would of course have to know these sticker symbols and we're not going to provide chat GPT with the ticker symbols we're going to expect it to already know them which of course can be a problem because if a company changes its name like Facebook to meta and it happened after September 2021 this could actually be a problem for chat gbt but we're going to ignore this now we're going to just return a string the string is going to be Yahoo finance ticker ticker history period equals one month doesn't actually matter too much and then we're going to say I lock negative one so the last entry essentially and this is a string now so this is our function it's a very simple function it quarries to Yahoo finance API so all I can do here is I can pass a ticker symbol msft and I would get a stock price or actually I think do I have to should I maybe get the close price yeah maybe I should get the close price of that there you go so I get just a floating Point number that gives me the current stock price of Microsoft nothing too fancy nothing too complicated now what I want to do here is I want to do the same thing that we did up here with a weather function but I want to do it with my stock function so I can just copy this here from the documentation the code and I can change it a little bit so I can say now okay first of all the conversation is going to get a parameter so I'm going to call this run conversation 2. user message and I'm going to just have a parameter in here so user message is what we're going to ask the function I want to describe is the get stock price function get the current stock price for a given ticker symbol it's important to mention that we're asking for a ticker symbol and not for a company name now chat GPT might be intelligent enough to realize this on its own so we don't have to necessarily um say that probably if we know that we're asking for a stock price it will probably use this ticker symbol right away but still it makes sense to be explicit here so of a company um and then we're going to say parameters we have the properties ticker ticker is going to be a string and the description of that is going to be the ticker or the stock ticker symbol for the company uh or for a company and then we can say example AAPL or Apple so it's good to provide an example here we're going to get rid of the second parameter here we don't need it and we're going to say required is only the ticker or only I mean ticker is going to be required so this is now our function definition we have a function get stock price gets the current stock price given a ticket symbol uh the parameter ticker is the stock ticker symbol for a company for example AAPL for Apple it's required and then we basically just feed this into the chat completion API we get a response this all stays the same uh now let me just get rid of the comments here to keep this a little bit more concise and now we're going to say okay if we get get stock price then we're going to map this to get stock price and then we're going to have function name function call this is very generalized here so we don't really need to change much here but here we need to change of course uh the ticker to function arguments get ticker and that is basically it then we get a response and we feed it back into chat gbt to get an output so that is already all of the magic this is what we had to change here so again we just changed the name of the function we change the description the parameters of course we also had the user message here now which is a parameter we asked chatgpt to process the user prompt to get a response uh then we call the function if necessary and then we feed the result of the function back into chat GPT to give us a response and of course here we're now going to call run conversation 2 and I'm going to ask for a simple uh query we're going to start simple I'm going to say give me the current stock price of Microsoft and that is basically what we want here I'm running this already and what I get here is the current stock price of Microsoft msft is 334.97 now I think we should also be able to see the actual response message here so I can actually print a response message because that's maybe interesting I'm going to run this again what we get here is you can see that the function the chatgpt decides to call the function get stock price with a ticker msft it recognizes that Microsoft has the ticker symbol msft and that given my prompt it should call this function to get the stock price of msft and then it knows that the current stock price is what we get here now I can make this even more difficult for chat gbt and at least when I tested it it worked let's see if it still works give me the current stock price of the company founded by Bill Gates so I'm not even mentioning Microsoft here I'm running this and you can see hopefully there you go ticker msft this actually even works for not so obvious examples so give me the latest stock price of the leading GPU company and this should hopefully give me Nvidia there you go Nvidia is being asked here the latest stock price of the leading GPU company NVIDIA Corporation is and so on please note that the stock price uh that stock prices are constantly changing and may vary at the time you check and you can see the power of this you can already see why this is mind-blowing I have a function that that has access to the internet I just have to give this interface to chat gbt and it can use it however it likes it can decide okay to answer this question I don't have this information but this function that I know of can give me that information and then I can answer the question with the help of that function and you can just give it I don't know 100 different functions to do 100 different things and then chat GPT all of a sudden has access to real-time information from the internet this is mind-blowing this is a game changer you can do so many different things with that and I think you will see a lot of different examples on how this can be used on my channel here in future videos but I don't know this just blows my mind you can just use this intelligent large language model and it now has access to real-time data from the internet and also in addition to that not just necessarily to the internet it can also have access to your private sources so it doesn't have to query the Yahoo finance API it can query your own CSV files your own databases your own local files that are maybe not even part of the internet this is crazy so let's maybe just play around with it a little bit longer let's see what we can if we can go to limits one thing I wanted to see here is what happens if I didn't test this yet what happens if I just ask something that cannot be answered with the function so for example what are stocks maybe this should not result in a function call ideally and I think it doesn't right yeah there you go we get just a description of what stocks are we don't get a function call but I can say for example now is the actually I don't know if this is possible this would be very interesting I think this is not possible but I'm going to try it is the stock price of Apple higher than that of Microsoft now I'm I don't think that this works right because it would have to call the function twice no because now it just gives me yeah yeah okay so so I would have to do some stuff uh some additional stuff around this to make it to to to give it a possibility to run two functions um or to to run the same function twice with different parameters but it would be possible it just doesn't work out of the box but maybe one last example let's try to actually I think this should be even able to get crypto prices so give me the current price of ethereum let's see if that works because that is not a stock in my function doesn't say that it can handle crypto so I think the API can okay no it cannot I think it's just a different ticker right okay for Bitcoin it seems to work but BTC is maybe a company right not sure I mean maybe if I if I add keep in mind that the Yahoo finance tickers for ETC and eth are not just PTC and ath maybe this will help now it doesn't even seem to call my function now so maybe that's too much I don't know but it works very well if I say that I'm actually looking for a company and then I'm actually looking for a company or now maybe it crashes I don't know what it does it doesn't seem like it can answer the question looks like it's stuck but yeah nevertheless you can see that this is very impressive if you use it the way it should be used now what do we get here we get a timeout service overload it we're not ready yet maybe let me run this a second time there you go oh oh there you go it actually works when I give it the information that in Yahoo finance actually the tickers are not just that simple it realizes okay the actual ticker is BTC Dash USD and it then gives me the current bitcoin price this is mind-blowing again okay very nice this is how you do function calling with openai and GPT and python so that's it for today's video I hope you enjoyed it and hope you learned something if so let me know by hitting the like button and leaving a comment in the comment section down below and of course don't forget to subscribe to this Channel and hit the notification Bell to not miss a single future video for free other than that thank you much for watching see you next video and bye
Info
Channel: NeuralNine
Views: 10,537
Rating: undefined out of 5
Keywords: python, gpt, gpt-4, gpt-3, gpt-3.5, chatgpt, gpt function calling, openai, python gpt, python function calling, python gpt function calling
Id: qNISzZoqpI0
Channel Id: undefined
Length: 24min 16sec (1456 seconds)
Published: Tue Jun 27 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.