NEW OpenAI Function Calling API with Node.js (ChatGPT / GPT / GPT-4)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
open AI just came out with something new called function calling function calling is a way to call functions using chat GPT you can now use function calling in gpt4-0613 or GPT 3.5 turbo-0613 by the end of June these will be the default models so you'll be able to just say gpt-4 or gbt-3.5 Turbo and it'll work but if you're still in the month of June you'll have to add this Dash zero six one three to the end I'm going to show you how function calling Works in 10 minutes using a real example I honestly think this may be as revolutionary as chat CBT itself there are unlimited possibilities now that you can have chat gbt run functions for you you really can create anything you can imagine using the Chachi PT API to follow along you can go to nextchat.ai this is a chat CPT powered service with prompts libraries there's a prompt in this library that will help you understand how to use function calling faster it's free to sign up once you're logged in there's something called a global prompt library in the bottom right corner you can click that button search for open AI it's this one right here open AI function calling with node.js you can copy that prompt come back to your conversation you can go ahead and save your prompt in your prompt Library I'm going to call this open AI function calling I'll paste that there so that it's saved and now whenever I want to start a conversation about function calling I can just say slash go down to open AI function calling shift return I'm just going to say do you understand how this is working this is the new function calling that open AI just came out within the year 2023 so that it knows it's now the Year 2023. this is new information alright so now that I know that it understands how it works I'm going to just say rewrite the code with notes explaining how it's working and at the end make sure to say Do not rewrite any of the code keep the code the way it is and the example I gave you uh make sure to do that or it might try to change the code since it's stuck in 2021 you want to really make sure to let it know that this is the new way to do it once it's finished you can just come up here and click copy the code this is a basic node.js server I just created I'm going to copy the code in there I'm going to come back here and ask what npm packages do I need to install and it tells you which packages to install I'm going to copy this come back here paste that there go ahead and install that and I'll just go ahead and go through this and explain what's going on we're going to have chat gbt run this lookup time function that looks up the time of a specific location down here in our create chat completion function if you're familiar with the open AI API these two parts probably look familiar to you you need to give it a model name and again it's important with your model name make sure to put a dash 0613 at the end of the name or function calling won't work the second thing you're probably familiar with is the messages with messages there's roles of system user assistant and now if you look in their documentation there's something called functions the system is where we explain what it is so we just say you're a helpful assistant this prompt is where I'm going to ask it to give me the time in that specific location and I'm going to need to run a function because chat EBT is stuck in the year 2021 and it doesn't know the current time in Beijing China and so here's how the function works you'll notice it's an array within this array you can put as many objects as you want each object represents a function call what you'll want to do is first name the function my function up here that I created is called lookup time then this next part is really important you want to give it a description think of the description as a way to communicate directly with Chachi BT to let it know to run this function so I just say get the current time in a given location what this does is let chat GPT know that this function lookup time needs to run whenever the user is asking about a location or a time in a location so this part is very important you just need to be very specific let it know what this function does by letting it know what this function does chat CBT will just know automatically if it needs to run that function or not before responding the next part that's important is parameters and as the notes say this will Define the input parameters for the function in look up time and so in our function we just need a location but we do need it in a specific format and we can actually explain that format to chat gbt in the location object description and so in the parameters you should say type object and then within the properties this is an object with other objects inside of it now you could have more than one object like for example you could say name as a second parameter and then you could add a second name parameter up here in lookup time and I'll just go ahead and do that to show you how this works so the current time in I'll say name here is date time and you'll see how this works in a second the second thing you need to put after the name of the parameter is the type so both the location and the name should be a string and then this next part again is really important description the description let's chat CBT know what it should be returning so a big part of running these function calls is first you use this description to let Chachi BT know when the lookup time function should run the next really important thing that chat CPT needs to understand how to do is how to create the properties for that function we need chat CPT to create the properties the parameters that are going to go inside of this lookup time function and so this first one we say we need a location parameter and that location what that is the example is Beijing China but it should be written in a time zone name like Asia Shanghai and so that's very important it needs to be in that format or this API this world time API won't work and the cool thing with Chachi BT is it just automatically knows how to do that as you'll see in a second so that's the first thing we'll need this second thing I want the original name of Beijing China so I can use that as the name game within this lookup time function instead of it saying the current time in Asia Shanghai or something like that is whatever I wanted to actually say the current time in Beijing China is whatever and so I'll just say the location mentioned in the prompt example Beijing China and then it should return the name itself without changing the format unlike the location the location should change the format to something like this a time zone format the next part within the parameters is this required object you need to let Chachi PT know which parameters it has to create I'm going to say both I want both the location and the name because I need both for the function to work if one of these parameters were optional then you could just do the one like if I did not need the name parameter I could just take that out but I'm going to say I need both of these the last thing you'll want to pass into this create chat completion is function and call Auto this needs to be set to Auto so that it knows to automatically call a function call if chat CPT thinks it should based on the prompt information the other option would be none which means don't ever run a function call but we want it to run a function call so I'm going to say Auto and we'll go ahead and run this and I'll add some a console log here just so you can fully understand what you're looking at I'll say completion.data here and I'll rewrite it this way and I'll put all the code for this up on GitHub as well like I said you can find this on nextchat.ai and you could be having conversations asking it to add things to your function for you and stuff like that all right and let's go ahead and run the function and see how it does I'll say node index because that's the name of my server function okay and we'll go one by one it looks like it said the current time in undefined is 720 23 PM that is because I forgot to pass in the name value but let's just go ahead one by one I'll show you what the completion data console log said so this is an example of what it will return you'll notice in the completion.data dot choices it said the finished reason is a function call so since in my prompt I asked it a question about a location and a time and Chachi PT knows to run a lookup time function whenever you see a question or a prompt about a time or a location it just automatically knew this is a function call I need to run a function call before responding and so that's what's in the completion data object the next thing to be interested in is choices Dot message and that's what this completion response is so if you go down you'll notice there's no content the reason again is because it just automatically knows it needs to run a function and so it tells you what function it thinks you need to run before answering or responding it says it thinks it needs to run the lookup time function and it went ahead and created two arguments for you two parameters to use in your function the first is location and notice it changed it to Asia Shanghai but it also kept the original name for us and so the way this is working is I say if completion response content is falsy which it is go ahead and run the function within it and then underneath this you could have something like and you could say return completion content if there is content because that means there should not be a function called the next step down if there's no content is to find which function should be called so there can be an unlimited number of functions there could be more than just the lookup time function you could have additional functions here and so the next thing you need to look up up is what is the name of the function and that's where we look up completionresponse.functioncall.name that gives you this value here and then here I can say if the function call name is equal to look up time then I know I need to run this lookup time function this next part is important you'll notice the arguments are stringified they're not parsed so you need to parse this value so that you can access the values within this object and so for this completion arguments const you can say json.parse the completion response function call arguments and that gives you this value here which is location and name and you'll notice in my lookup time function I just have the location in it I need to also pass in the name I forgot to do that so I'll go ahead and pass in the completion arguments.name and now let's try to run this again and see if it responds correctly and it did it said the current time in Beijing China is 727 p.m and so as you can see up here the lookup time function ran correctly it got the right time and it returned this as the console log and just to see that if this works if I give it a different name let's say something like London England let's see if it works still I'll say node index and it did it said the current time in London England is 12 28 pm and that is how function calling works like I said I think this is going to be revolutionary there are so many new startups that are going to be created using this technology you really can automate any computer-based task you can send emails organize your calendar create a to-do list connect to social media apis to send out social media posts really anything you can imagine you could create a chat CPT powered service to automate any task if there's any kind of function calling video you'd like me to make please let me know in the comments below I'm planning on creating a video soon where I'll have EBT send an email for me using a function call if there's any video like that you'd like me to make please let me know in the comments below and also let us know what you think of function calling do you think this is revolutionary or do you think it's over hyped like the video if you'd like to see more content like this don't forget to subscribe and hit the Bell icon to be notified when new videos come out thanks for watching and I'll see you next time
Info
Channel: Native Notify
Views: 1,864
Rating: undefined out of 5
Keywords: openai, ai, chatgpt, gpt, gpt-4, function calling
Id: o7iC4JYHryI
Channel Id: undefined
Length: 13min 22sec (802 seconds)
Published: Thu Jun 22 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.