How to Use The GPT-4 API With Function Calling | Your Own ChatGPT Plugins | TypeScript

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

Hey /u/friuns, if your post is a ChatGPT conversation screenshot, please reply with the conversation link or prompt. Thanks!

We have a public discord server. There's a free Chatgpt bot, Open Assistant bot (Open-source model), AI image generator bot, Perplexity AI bot, 🤖 GPT-4 bot (Now with Visual capabilities (cloud vision)!) and channel for latest prompts.

New Addition: Adobe Firefly bot and Eleven Labs cloning bot! So why not join us?

PSA: For any Chatgpt-related issues email support@openai.com

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

👍︎︎ 1 👤︎︎ u/AutoModerator 📅︎︎ Jun 14 2023 🗫︎ replies
Captions
all right so like an hour ago open air announced a new version of the GPT 3.5 and gpd4 models that include the ability to call functions so we're going to look at that today honestly this has been like my favorite way of using GPT 3.5 as what we call an agent where you not only let it respond to your requests but you also let it execute code and of course it cannot directly execute code with the way that you do it is you tell the chat model like hey please tell me which code to run and then in your program as you're developing you parse the response from chat jpt over from GPT 3.5 and then you use that to call a method now it used to be kind of cumbersome and difficult let me show you how it used to be before so I have this agents so this is just what I was working on and I like just literally a few days ago I built this and so what I did here is so you have the chat conversation right so there's a messages from you and the message from the user and the messages from the assistant and so for to make this work what I had to do was I had to create a agent instructions prompt so before we had the actual conversation I needed to give this extra information so I needed to tell this model like hey you should respond accurately sure you have access to the following tools and there's a list of tools and then I say use a Json blob to invoke a single tool and its parameters this is how it works you cannot do more than one at a time follow this response format in every response so thought commands Json here the system will respond with an observation then you respond again when you no longer need to use any tools then you say final response so we need this whole explanation to give it to the GPT API and then still at the end I needed to give it hey please in your next response follow the response form because it tended to forget so you needed all this stuff to add that to the prompt to make it respond in the structured Json type of way and it was pretty cumbersome and honestly it like didn't it didn't work like let's say I'd say 20 of the time or ten percent of the time it just didn't work so what open AI has done now is they've trained GPD 3.5 to actually give you in some cases structured information back now it'll still they say it won't be perfect so there might still be mistakes in there but it'll be much much better than the whole hodgepodge that we were doing here so honestly this whole file with these problems I can I can just I can delete it there we go it's gone so now I need to of course fix my imports this is gone add agent prompt this method this is completely gone I don't need it anymore what I also needed to do was to check the response from the model often it would say something and then they would include the Json and I need to split it up and then parse the Json so that's this whole deal which is also calm now up there we go let's organize my imports Okay so how does this work let's let's have a look now I structured my thing because I had this and I want this to be extensible so it's not going to be like the plain way to use it but I'll show you how how it works so we have here this openai language model this is the basic way this works we call openai create chat completion methods as before we have our messages so this is exactly the same as before I won't go into this this is an array of messages right that's who sent it what did they say and then the new thing is that this one functions and this one function call so we have to say function call Auto and that will tell the API it's all the model like hey you can either call a function or no it's up to you you can also say I don't want to do any function calls you can also say I wanted to call a specific function only so that's very useful if you're if you're working very with a very specific tool that you're building like I want to check the weather and I only want to check the weather then you can add it that way into the function call so okay this functions thing what what does it what does it look like so it's an array right of type check completion functions what's in here there's a name so you give your function a name you optionally give it a description and then you give it parameters now parameters this needs to be formatted according to Json spec so if you don't know Json spec so this is what Jason schema looks like so you say well you know this is type objects properties these are my properties they have a string and they have a description optionally and you can have some extra stuff like this is an integer with a minimum of zero that's kind of what it looks like it's pretty simple you just need to kind of know how it works so for this example I have created a brave search so I internally I call it tool I will I'm going to change the name now to function so it's more aligned but so what it looks like basically we have a name we have a description right and now we have this parameters thing so we have type objects properties query string description is the query to search for right and so this is what we give here in this function so that's just the list with this Brave search function in there and that's it right that's it so you give this what it will return is let me just let me just run this let me open the so the message I'm sending is what's the Apple Vision Pro which of course is not part of the training data because it's very recent and I send this okay okay okay let's let's scroll up and see what's happening here so this is the response from the open AI API so it says we got a message with the role assistant which is you know the normal messages but now there's no content but there is a part here that's called function call in my function call we have name so that's the name that I passed in at the beginning and there's arguments which is a string of a Json so here's where you need to be a little bit careful where openai say well it can still hallucinate parameters it can still give you incorrect Json so that's something that you will need to make sure that you catch it in your function or in your in your own code so but it's completely correct so that's perfect right this is the same thing so now my code says okay I recognize that this is a function call the function name I parse these arguments as Json and so I go to the brave search API I throw in all these responses which is just you know API response voila and then I go back to the open AI API and I tell it hey okay let me tell you show you how that works we go so I have this run tool and run tool gives me a string back so that's the whole this stuff that you saw right so this result we will add a new message with the role of function so we used to have only the roles of user assistance system and now we have function so we say well there's a response from the function the function name is the function name that it called and then this is the result sometimes m is something I'm just using that's not part of the API now one interesting thing I figured out here is that normally you give the entire conversation back to the open AI API right so because every time you're adding a new message to it in this case it gave me an error when I was doing it because the message that it sent before had no content and now it's complaining well what you're sending doesn't have any content so that's a bit weird I mean it works but it doesn't know in its own history that what it actually decided to search so if you look at the complete history of this conversation that we're sending to the API it's like it it will have forgotten what the actual query was to the search so that's a bit weird maybe they'll update it maybe this is just a bug that's here because one hour after it's being released so we'll see we'll see so yeah that's it right it's it's actually remarkably simple so to recap when you are creating a call to open AI you have to add this thing function called Auto and you have to add your functions your functions need to be give it a name you give it a description you give it parameters parameters is formatted according to Json schema then when you get the results you check if the result has its property called function call which we saw here and if there's this function call you check the name based on the name you go look in your code which function it is and you go parse these arguments which is a Json you just parse it and then you run your own function and then you give it back to the AI with a roll of function you pass it the name you need to pass it the name of the function I called otherwise it'll it'll error out and you give actually the content of your function which is just should be a string that's it this is like all the people who have building have been building things with Lang chain it's just so much code that's gonna be thrown away so that's kind of crazy um but yeah this is cool thank you for watching if there are still questions that you have leave them here I know I went over this quite quickly uh it's it's bedtime for me but I just wanted to try this out and get this out to you um it was a little bit difficult to get through the API documentation I think they're just not finished yet uh it's not super clear and there's not it's not completely right so there's still a few mistakes in there but yeah there you go you can start building and making amazing agents with a techipity so like this and subscribe if you like it bye
Info
Channel: Marcel Samyn
Views: 9,834
Rating: undefined out of 5
Keywords:
Id: Td647EeqKek
Channel Id: undefined
Length: 10min 48sec (648 seconds)
Published: Tue Jun 13 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.