ChatGPT Functions - Full Tutorial for using OpenAI Functions

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
chat GPT functions are very interesting because they allow developers to directly interface with different models to create functions that they can call through system prompts in this video I'll do a crash course on exactly how to use them this will be beginner friendly and it'll cover everything you need to know if you're starting out with GPD functions and how to create them how to interface with them and then how to customize them to whatever your needs are let's begin as an example of what I'll cover it'll be three main parts the first will be how to connect up GPD functions then create a few simple functions like Hello World then a more complex example where I'll connect a gpt4 up to the web so it can scrape information analyze it and return it back to users before I jump into actually building a function let me showcase the open AI documentation for function calling now this documentation basically describes how the models for using function calling is only really available for chat GPT version of 3.5 turbo and GPT 4 these two have been finely tuned to produce a special Json object with the arguments to be able to call functions this actually means that functions uncalled directly by openai and its models instead it passes the information across to be able to call those functions as a little bit of information before function calling was created people were already trying to do something similar by fine-tuning models and have them produce strict outputs that you could plug into a function function calling just makes this much easier and it's essentially four steps that opening eye recommends the first is to do a normal prompt second is to essentially get a response back from that prompt the third is to code your function and the fourth is to append that function to a new prompt and if this is taken into a more descriptive example let me give you this one here imagine you're trying to get the weather here in Australia so you type into chat GPT what is the weather the only difference now is that you've also defined a weather function as part of that very first prompt so this is step one step two will be the response back from open AI so now that it knows it has a function to call the weather it can decide whether it just wants to produce a normal output for the user or if it wants to call this function and then append the results to a later request and in this case if you've described the function well it'll try to call that function by giving you a Json object requesting that function to pick hold as well as the parameters of say Australia being passed in and stringified as part of that object in step 3 you get that response back you see that rather than a regular response from openai you're instead getting a function call and the function call is a Json object you're going to unstringify you're going to pause it and you're going to see that the attribute Australia is there you can now plug that straight into your own function inside of your code to execute you get a result back from your own code and then you can append the results of that into the next call that you make from openai and this continues to Loop depending on the type of conversation you're having with the model here is the example from openai and as you can see it's very similar to the example I was talking about this is inside of a development environment and it was essentially step one two three and four we're going to take a look at now this is inside of python and here is our example function now this is just a dummy function it's not a real function it's not calling a real API it's just kind of used as an example and it's called get current weather we pass in the location as well as the unit and we get a response back but let's actually have a look at what's happening for step one two and three so step one is to call the API from openai and to pass in two things we're going to pass in a message as well as the functions available now the message is just a conversation like you would normally have with chat GPT so we're passing in the role as user as well as the content in this case it's just saying what's the weather like in Boston but what we're also doing here is passing in these functions now you'll notice that the functions are actually an array because you can pass in more than one function inside of this array we just have one object of a single function and it is that get current weather function we've got a description of it which is to get the current weather given a location as well as the parameters in this case we have two we have the location as well as the unit for the location we've got a description the city State or San Francisco and for the unit we do have two options here for Celsius or Fahrenheit there is one more field here called required and this basically means that if we're going to call this function we definitely need the location now as part of step one we actually have to call the API from openai so here you can see we're calling GPT 3.5 turbo we're passing in the messages as well as the functions and we're setting function calling to Odo which means that openai can kind of decide finally we get a response back and we save this in response underscore message so that's all of step one now let's take a look at step two step two is really simple we just check if the response is calling a function so here we're checking if message underscore so if response underscore message dot get has the option there function call we move straight to step three so step three is basically calling that function this step is a little bit tricky so let me get through it the main aspects is essentially getting the function name that we're calling we're also pausing the Json of the arguments and the final thing is to pass in these two arguments over here to call the function and this a function responses just saved under function response now later we need to append this when we go to step four and step four is calling the API once more now that we have the function response we're going to essentially append the response message and append the function with its name and its response as well so this allows us now to call GPT 3.5 turbo once more with the message and get our final response and this will be printed out as a message this might all seem kind of complicated but from a user point of view they're not going to see any of this they'll just see chat GPT basically being prompted for the weather and then getting a response back but what's actually happening in the background is these four steps and the output is essentially what we would expect which is the temperature or the weather that you essentially are looking for now all of this still might seem a little bit complicated and it'll make a lot more sense when we jump in and do an actual demo of an actual example so let's do that right now quick pause if you're looking to learn more about openai I've put together a small handbook which basically covers a lot of these things including things like actually doing custom fine tuning as well as understanding how chat GPT Works under the hood with things like open Ai and their apis and I've got a few starter templates and I've linked to everything in the description below if you guys want to check that out or just support the channel then definitely have a look I'll start here on the open AI website the first thing you'll need is an account I'm going to select from the menu to login and I already have an account so I'll be taken to this screen over here I'm going to select the API section here because we're going to develop an API that uses functions this is the dashboard with documentation API references and examples but I'll head to the top right here and select to view API keys this is a secret key that you can generate and you'll want to keep this private in this case I'm just going to label mine chat GPT functions for this project so it's nice and clear if you have your own key make sure you don't share this on the internet and if it does leak make sure that your evoke or delete access to it for this one here I'll be deleting it after the end of this video next inside of vs code I'm going to have a new folder and a new file I'm going to call this file index. dot JS and here I'm going to put in the Syntax for chat GPT functions but first I need to install the open AI Library so here in the console I'm going to npm install open Ai and I also have node.js installed which is what I'll be using for this demo all right so let me import this Library into our file I'm going to pass in import and the two items I'll import is the configuration as well as the open AI API and this obviously I'll import from open AI next I'll need to set the configuration I'll create a new const value here cold configuration and I'll run a new configuration passing in an object with two values the two values will be the organization which is an ID that you get inside of your account and the other being the API key which we retrieved earlier let me actually jump into the open AI website right now to grab the organization as well so that you guys can see where it is here here on the openai website I'm going to head over to my account and then to settings and here the organization ID can be found I'm going to copy this piece of text and then paste it here in the organization section and I'm going to do the same for the API key there's one last thing we need to do to initialize all this to create a value called openai we're going to call open AI API passing in the configuration and once that's done we can use this new value to create a request and receive them from openai now it's time to create the very first function I'm just going to create a hello world function here I'll pass in function hello world and I'll also pass in an argument called pen string which I can use later this will simply console log out hello world but it'll also console log out the appended string that I want to pass next I want to call open AI so I'll create an async function to be able to do this this async function Shall be Called cold chatgpt with functions and I'll also pass in the append string which I can define a little bit later here I'm going to set the value for chat to equal the awaited response from openai when I do a create chat completion and this chat completion is going to pass in a few different values first I want to define the model which I'll be using in this case we'll be using chat GPT 3.5 turbo and more specifically the dash zero six one three version now we're also going to pass in the messages and for these messages I'm really just going to start off with an array of two the first will be the role for the system so this kind of defines what you want chat GPT to do and in this case I just wanted to perform function calls and in this case I've just got the one hello world function the next message will be the message from the user themselves so for this message I'll do the role as a user and I'll set this to essentially be to call the hello world function and pass in the following argument that as a user I'd like to call the hello world function passing in the string it's about time now the fun part to create the functions themselves I'm going to pass this in as an array with a single object it'll just be the one function that we have here so I'll pass in the name to be hello world making sure that the Caps is properly identified with the uppercase W I'll have the description here that it prints a hello world with a string pass to it the next is quite important it is the parameters which is an object the first main value here is the type which is object as well and then the properties which are the different types of arguments you want it to utilize and pass back as a stringified object now in this case I just have the one which is a pen string and this is a string that you append to the hello world message finally I'll pass in required having an array of different properties that definitely have to be passed in when calling this function and since I just have the one I'll pass in a pinstring there as well one thing to finalize this call to open AI is to pass in the following which is a functions call and set it to Auto and this will just allow chat GPT to identify that functions are enabled as part of this request it's time to call the request let me head over here within this function and what I'm going to do is essentially set a value to identify if a function is needing to be cold what I'll do is set the value let once to use function equal and what I'm going to do is pull this out of chat.choices index 0 and check for the value finish underscore reason now this value in the start I'm just going to console log this out the main thing here is to check if chat GPT is actually requesting to run a function so to do this I'm going to call this function I'm going to call chat GPT with functions passing in it's about time and I'm going to open up the console and run this using node.js I'll pass in node dot forward slash index.js and here our console log comes out with a function underscore call this is the text within this value dot finish underscore reason so this finished reason I can now check for this string whether this string actually says function underscore call and use this as a kind of Boolean to run an ifscript and this if script can then do the next step in our process step three which is to identify if a function needs to be called in this case if it has this string it will continue inside of this if script and here we can essentially console log out the results from chat.data dot choices index 0 Dot message and ideally this has all the details for us to be able to run the function itself here they are it says here that essentially a function call has been requested the name of the function is for the hello world and it has the arguments down here as well which is for the append string now let me head back into the code I'm going to run an ifscript here checking the message as well as the function call itself and finding out the name for the function that's being called what I want to do here is to make sure that it's the same function that I've defined there's a few different ways to do this but this is one of the ways that it's a bit more strict just to ensure it is the same thing now that I've got that defined I'm going to pull out the arguments I'm going to do this as an argument object here I'm going to get the arguments from the function call Dot arguments but because this is stringified I need to run json.parse so that I can get this out as an object that I can use then I'm passing this to hello world so hello world with my argument dot append string being passed through and here we can now call it that's pretty much it so I'm going to open up node.js I'm going to run the index file and here success hello world with it's about time this is a pretty standard function right now it's just a console.log so how about I get this to return to chat GPT as part of step four I'm gonna make a few changes to the code first we'll need a return for the hello world function because right now it's just a console log I'm going to cut it out and create a new value here called hello this will be the string which will also have the append string value great so I can now return this value hello which means I can get rid of console.log I'll be passing this into the next function here for call chat GPT with functions and this means that a pen string can essentially be pulled out as a value later on now I need to make another change I'm going to remove the messages here and set it to have its own value and this is so that I can append it later with the function call and its return so here I'm just going to set it as its own a little array and since it's got the same value here messages it'll just be passed into the very first call to create a chat completion I can now scroll down here to where we call the hello world function I want to save the return of that value under content and for this I actually might define this before we call the ifscript so it'll come up as an empty string and here I'll add it to be set once we call that function so here as a summary we've got content empty string content will be defined here from hello world where and the hello world function gets cold and it should say Hello World Plus a pen string now that we've got this defined we're ready to move on to the final part of this step what I'm going to do is have our messages which we've Now set as an array to push another value there which is the value of our original response from chatgpt this is the one where the function is being requested to be called but this is just as a bit of History we need to push one more item in which is the actual response from the function after it's been called as well here I'll create an object and for this object I'm going to Define this as the following I'll set the role here to function I'll set the name here to hello world being the function name and I'll send the content to the content we received from that function with all this defined it always is useful to do a double check to make sure everything is correct and I'll do this by setting console log and console logging out the messages that we have so far let me run this in node.js to have a look at the output here in the terminal I'll just run node index.js and the response is essentially the history of the chat history that we have so far which includes the system message the user message the call to perform a function as well as the response that we have from the function once it's been called from our terminal the very last part now is to pass this old back to chat gbt which means that we need to do one more call passing all the messages across to a chat completion I'm going to call this a step 4 response and I'm going to call a weight open AI create chat completion once more here I'm going to use the same model as well as the messages and really that's it we don't need to pass any functions across this time we want chat GPT to look at the response of our function after it's been called and give some detail as to what it decided to essentially populate for the user now that it knows the response from that function so I'm going to console log out of that response here called step 4.data.choices.0 and let's call it now inside of the terminal looks like that worked chat GPT has said that the hello world function was called and it had a string output which says it's about time and it printed the following which is hello world it's about time let me show you the different steps that we took as part of doing this just before we jump into the next demo so the first is we defined the function just up here this was the hello world function which then we were going to use inside of our call where we defined an interaction with chat gbt let's call this the chat GPT function here we did step one step one was calling chat GPT but also defining the function inside of that call so that chat GPT could use it the next step step two was to check whether chat GPT wants to call a function or not in the event that chatgpt does want to call a function then we move straight to step three which is actually getting all the arguments already and calling the function itself and that brings us to step four the very last step where we compile all the information append it to the messages and send it back to chatgpt so that it can finally decide how it wants to handle the response of the function and its data from a user point of view they only really get to see step one and step 4 where they call a request and they get a response back from chat GPT step two and three are sort of hidden in the background now to be a bit more creative I'm going to add another function here and this time I want to be able to get the current time of day and this is something that chat GPT just can't do but here I'm going to create my own function called get time of day and what it'll do is get the current date and time pull out the hours minutes seconds time of day whether it's AM or PM and then what I can do is compile all this into a return this is what we'll be sending back to chat GPT which is this string over here hours minutes seconds time of day but this is part of the function that will allow chat GPT to Now call the time of day and hopefully provide it to a user now let's test this out here in our message to chatgpt instead of just saying I'd like to run the hello world function instead I'm going to ask it to to tell me their time of day so this is a simple chat log essentially and for this chat log now we'll actually add in the additional function that Chad GPT can call under here so this being an array we'll just copy out hello world and I'm going to remove the require statement because we're not passing any properties here so I'll remove that too I'll update the description of this function that it is getting the time of day and I'll update the name here to be get time of day this is just the first part of updating the functions available we do still need to update what happens after chat GPT requests to use this function so I'll need to clean this bit of code up a little bit I'm going to cut all this content for the actual hello world section and put it inside of the IPS script that we created so it only gets called if it's a hello world function but I can copy this if script now and I can make another one and this one will be more specific for the time of day so here I'm listening if chat GPT is calling that function I'm not passing any arguments so I can remove that line I can also remove this item here which actually passes it to the hello world function and replace this function with time of day the rest of the content is more or less the same I'll just update the message here to be called with a return of time of day so now chat GPT can call either hello world or the time of day I can now run this function let me get rid of this passing statement and open up the console I'll just run a node once more calling index.js and here we have unfortunately an error now the error is the fact that chat GPT didn't give us the time and there is a reason for that the function was not actually cold and the reason for that as well is because I didn't label it properly I called it time of day but the actual function name should in fact be called get time of day so make sure that you're specific with the names that you're using because one mistake or One Missed typo can cause the function not to work here I still haven't updated this value here get time of day but now that I have I can call it successfully and chat GPT now actually gives us the current time based on my own terminal and this is the essence of adding multiple functions where chatgpt can choose which one it wants to run and then give a proper outputs to users now let's build a more complex example I'm going to create a function which will do web scraping this will be an advanced function and show you how you can create functions in other files and how you can call them through chat GPT my plan is quite simple I want to collect up and scrape all the information from Goodreads for different types of books and then recommend certain ones using chat GPT now to do this I'm going to use the right data they're a place that allows you to connect it to proxy networks as well as use data sets or scrape the web the first thing I'll need is an account and I already have one so I'll log in and then I'm going to select their scraping browser just over here luckily there there's already boilerplate ready for me to go so I'm simply going to select the option here on the bottom right and that says checkout code and integration examples now I'm running on node.js and I'm going to try and build an API so the first thing I need is Puppeteer core so in node.js I'll open up the terminal and run npm install Puppeteer Dash core it basically emulates a web browser and makes scraping a lot easier then there's a sample code here this isn't specific to any website I could go through it and try and rewrite it specifically for a website like Goodreads but instead I'll select the lazy option of copying it into chat GPT and getting chatgpt to essentially write out that code for me the simple prompt I'll use is that this is some sample code from Bright data and I needed to search up Goodreads for the keyword of programming for any books available and what comes out is this piece of code which is more or less the same but just with more specific CSS selectors so that I can identify the information from that website accurately now let me copy this into vs code and see if it actually works I've pasted this into my scraper.js file but I'm going to update the syntax here because instead of require statements I'm using then I need to run the main function so let me actually do this by passing it here at the very bottom and executing it and then opening up the terminal to see what comes out on the console now here I'll run nodescriper.js this uses Puppeteer to create a browser it connects up to the Goodreads website searching through the results then it collects all the information from each one of the book titles and finally spits this out into the console looks like this example is working so now I can take this example of a function and see if I can customize it to turn it into something that chat GPT could use to communicate directly to the web and collect information and pass it back to users now let me prepare this scraping function to be actually used first I'll need a keyword which I can pass into it so I'll Define it here at the very top so that I can reference it throughout the rest of the code I'm going to update the actual query for essentially checking Goodreads and looking up the keyword then I'll also update the console log so that I know what is being searched for and the final thing I'll do is create a new attribute here which I'm going to call return statement this will Loop through for each one of the books and add it as a string and then I'm going to return this string here as a return statement now I'll export this function so I can use it in the main file so I'll run export default main then heading back to the index.js file I'm going to import it I'll run import scraper from scraper.js and now I should be able to use this within my code but first I'll need to Define chat GPT to actually start using it I'll update the message log here to say that I'm looking to get the top 5 books that come with the keyword chatgpt next I'll Define the function I'm just going to copy the hello world function I had earlier since it's more or less ready to go and I can reuse the syntax I'll update the name of the function now to be called as a scraper the description that it scrapes for books from a website called Goodreads and finally the parameter which will be the keyword which is what it's searching for with this done I just have to update a one last thing which is the calling of the actual function so here I'm going to once again copy the hello world function that I had earlier instead of looking for the function name of hello world we'll look for the function name scraper if chat GPT functions is trying to call this I'll call it and I'll pass in the keyword which will be probably the book name or the title I'll update the message and the only other thing that I need to do is this function here is a async function so I'm going to await it since it does take a little bit of time for it to call the URL and that's everything prepared let me open up the terminal and run this I'll run node index.js here it looks like chat GPT is actually trying to run the scraper it's navigating to Goodreads performing the search results grabbing the book titles and then presenting them back to chat PPT which now presents back a response to the user and this looks all correct I've only got five recommendations here even though I know that the scraping basically provided a lot more and these recommendations are the ones that Chad gbt decided so this is essentially all working it means that chat GPT is now connected to the web scraping information and then compiling that and giving it back to users and you can imagine the business potential for something like this you could get GPT functions to analyze different types of products and then compare them and their prices and delivery dates and send that information back to a user to best give them recommendations they might need I'm going to upload all the code that I've done in this example here to my repository chat GPT functions I'll link this in the description below so if you want to replicate it or use it yourself you should be able to do so easily I'd also like to thank a bright data for sponsoring this video they make it and others possible on this channel and if you want to check them out they're actually kind of cool they've got a lot of other features not just a scraping information and proxy networks but even data sets or you can pull out data from existing websites and they integrate with a lots and lots of different Frameworks and here you can see quite a few different creators have actually made content on them if you want to learn more about scraping as well as web proxies
Info
Channel: Adrian Twarog
Views: 57,597
Rating: undefined out of 5
Keywords: chatgpt functions, chatgpt function, chat gpt functions, chat gpt function, chatgpt, functions, javascript, openai, open, ai, tutorial, chatgpt functions tutorial, openai functions tutorial, how to chatgpt functions, chatgpt functions guide, chatgpt openai, functions chatgpt, functions chat gpt, gpt4 functions, chatgpt3.5, gpt functions, nodejs, open ai, open ai functions, learn chatgpt functions, how to use chatgpt functions, chatgpt functions crash course, guide, crash course
Id: WpQ0TqS3_68
Channel Id: undefined
Length: 29min 14sec (1754 seconds)
Published: Fri Aug 25 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.