ChatGPT API in Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what is going on everybody and welcome to a chat GPT API tutorial if you're looking to further customize chat GPT either by building much more advanced applications besides just purely a chatbot or if you're looking to further customize the behavior by injecting previous even assistant or chat GPT responses or using the sort of system capability then the API is something that you've probably been looking very much forward to as I have been this is a paid API it is a fifth of a penny per thousand tokens which for what it is is extremely cheap so anyways uh but keep that in mind you will have to set up an openai account as well as set up billing I most of the time it seems that they are giving away credits when you sign up for an account so that may vary whenever you're watching this uh there may or may not be free credits but either way you'll have to set up billing if you want to use it but uh anyways let's go ahead and dig in so to start I actually already have a key.txt this is just a text file with my key that's is all that's in the text file so once you have set up an account you'll go over to your account go to your keys create a key and just copy and paste it into the text file or you can copy and paste it into the program I'm just not looking to show it to all of you so beyond having that key the other thing that you are going to need is the open AI package so make sure you pip install open Ai and if you already had open AI I don't think I did on this machine but if you did you would want to actually pip install Dash upgrade open AI because the chat GPT aspect the sort of like chat aspect is completely new for the openai package so you'll want to go ahead and do that I think for this I'll make it a notebook I might regret this decision later but that's okay so let me go ahead and save as that's ready what do you mean it's already okay oh thank you [Applause] okay chat GPT API so I'm going to save that as chat gbt API notebook very good and let's get started first of all we can make this considerably larger so first things first is we can import open Ai and then to actually load the key it's just open AI dot API underscore key um and in this case I'm going to do equals open but you could just paste your key in there if you wanted key dot text with the intention to read dot read dot strip and just in case you edit a new line by mistake I actually don't think I did in mine but if you do there you go so we'll go ahead and do that now the way that this API is going to work is you're going to make a request and you're going to get a response back but one of the key features of a chat is that there are messages so you've got messages from like a user and then the assistant and you need to be able to pass that history of messages to the API every time you're trying to make a query so a very basic example of this might be something like this so we would say completion equals and then it's just open AI open AI Dot and this is a chat completion and then we're going to dot create and first we're going to specify the model that we're going to use in this case it's gpt-3.5-turbo there's also I think this is o301 version and at least at the time of my recording there's a beta0302 so you can just take note of that and check it out if you're interested my guess is eventually o302 will be the primary one but keep those things in mind uh as things goes on as things go on because everything is so finicky in terms of the types of prompts that you use so as a new model comes out you might find that things have changed so if you are doing something and you're actually putting this into production you probably would want to test it against the beta models as they're being developed and make any changes that you need to make going forward especially if they're going to deprecate the old ones so uh 3.5 turbo so that's the model that we're going to be using and that is the model that vax chat GPT I know it says GPT 3.5 but that is the model that is backing chat GPT so then we're going to pass messages and messages is going to be a list and in this list you're going to have a role and content so first of all it's going to be a dictionary and we will have a role and in this case we're going to say it is the user role now again though you can put anything you want in here this could be either user assistant which would be chat GPT or you can also have a system message at least in my testing I have not personally found a system to be of use you might think oh that sounds really good um I I'm not finding a use for it but hopefully in the future they will give system a more impactful role in everything but for now anyways I'm going to use user and then your will pass content and the content that we're going to pass is uh what is the circumference of the Moon in kilometers and that's actually everything that we're going to need so we can just print out the completion that we get we will run it and we will wait for a response is actually taking longer than it usually does uh and in this response you can see that we get a ton of information here we get um why did it stop so um I don't know all the options but for sure one is there you it actually reached a stop token uh which is the desirable one or you could literally run out of tokens uh so the maximum length here is 4096. so if you have enough tokens that exceeds that number it is going to stop for that reason as well so keep that in mind uh the other thing to keep in mind too is the actual usage here again it's um a fifth of a penny or a point let's see .002 dollars per thousand token so in this case it was whatever that is and we've used 32 total tokens so doing this you can kind of start to keep track of how many tokens are you using both for your actual billing and usage although open AI does a pretty good job of outlining all of the tokens that you're using and how much you're being billed for so you don't really need to do it for that reason but as you continue to use more and more history you do want to pay attention to the tokens because again you can't exceed 4096 so as you do you either need to truncate the older stuff or you could do something a lot more fancy like maybe summarize the previous text up to that point and again you could use chat gbt to do that summary so anyways maybe more on that later but this is a very basic completion and at the end of the day the probably the main thing that you're looking for is going to be reply underscore content equals uh and it looks like GitHub copilot is a wizard actually I don't think it's choices was that even oh okay choices so okay they're doing it a different way though and then is there really a DOT text yeah there isn't a DOT text so interesting chat uh all these open are all these AI models and in this one I don't know why it would say that maybe that's like a pre maybe from like a previous model anyway actually what we're looking for is gonna be completion choices and then message and then content so I'm actually just gonna make it like this so I'm going to say completion.choices dot uh and actually it's going to be the zeroth choices Dot message dot content cool so we'll do that and actually let me just uh we'll just print it right here print reply content and nine times out of ten this is what you're going to be actually interested in seeing so continuing on um probably the more likely scenario when you're using something like this is to rather than submitting necessarily you know a completion just like this and then you know hand typing in here you're gonna have some sort of input that's either coming from a user or maybe some actions that the user took something like that this is going to be a much more Dynamic thing and it's probably going to be a list that ends up building over time so I'm going to come down here and kind of build on what we've been doing so first I'm going to have message history and that's going to be an empty list and then we're going to just do a really basic input here so we're going to say user input equals input and we're just going to we could keep it the way it was but I'm just going to do that and then what we're going to say is print users input was and then whatever user input hey man whatever user input was okay so let's go ahead and in fact I'm just going to copy what is the circumference of the Moon in kilometers so copy that come down here we'll run it I will paste it in there and we'll see user's input was that so once we have some sort of input you're most likely going to be doing something more like message history dot append and then again in this case it would be a dictionary role I really want to see what the it had like started to make a suggestion content user input that is what we want so it's probably going to look something more like that so you would append to the history and then from here we would say completion equals open AI whoops open AI chat completion dot create again it's going to be that GPT 3.5 turbo model and then this time the messages will just be message history whatever that happened to be and then we'll go ahead and close that off yes and then reply content same thing as what was up above uh and then we can go ahead and print reply content although we should see that it's basically the same exact thing now again in a sort of application context you're going to you need to manage your message history the API is not managing it is literally just a one single query response it's not keeping it won't keep track of your message history and this is uh annoying because you have to message it uh manage it rather this is annoying because you have to manage it but it's actually a huge bonus because this again allows you to completely make up assistant responses if you want and again you can use that to kind of hack your way into behaviors that just have not been possible before so anyway what we're going to do now is since we have this reply content once you have that you're going to want to go ahead and message history.append and you're going to say roll uh it was almost there but in this case it's not thought it's going to be assistant and then the content is going to be reply content so now you have that message history beginning to build so I will go ahead and just do that and then what what if as things go on you're going to want to like utilize that history and so now we can just kind of take everything we've done up to this point and at least show this this use of History so what I'm going to say now is going to be user input equals and hopefully you'll just finish that for me thank you sir and then we will go ahead and print users input was yes just like that line before if you don't have a copilot by the way I'm trying to I'll do my best to not use copilot except for cases where I would have copied and pasted essentially so these would have all been copy and paste so then we're going to message history append and again in this case we're appending user yes user input nice copy paste save there and then so now we've got that message history now again we're going to query that model so it's just going to be completion equals open AI I thought you would do it for me chat completion create very good that model that we've been using this whole time again message history and then we're done and so as you can see now going forward every time we make a completion we're actually just it's this exact same block of code you could just make it a function or and just make it a function call every single time now because message history is the very is the only thing that's going to be variable at this point and then again reply content completion choices zero message content I believe that is the correct sequence and we can uh we'll print reply content now open to this point we asked what is the circumference of the Moon and it says okay it's almost 11 000 kilometers but what if we are like okay well what what moon is that in reference to right there's lots of moons so you might say something like that so as well I don't remember my previous response to the Corpus of Earth's moon so that is kind of weird actually that it did not what moon was that I don't remember that's a crazy response to be honest actually um I'm gonna leave that in this video because that's that's that's not even accurate they do it it definitely knows its previous response and in fact like let me just make sure I didn't screw something up print because that would only make sense if that was like the first message uh what is the circumference assistant replies the circumference of the Moon is that boom user that's really curious why would it uh I don't even know what to think about that that that's like a bug it's not even a bug it's just that's a that's a mistake I'm actually I'm gonna run that one more time because I'm super curious if I've screwed something up in here or if that was just a fluke of a response uh and in fact let me think here so so here we're gonna reset username what is was what is okay so we'll rerun this that resets the message history for us we're going to append that we're going to go ahead and paste the or rerun that same response that's good uh we are going to append that response which moon is that in reference to sorry this is so weird um wow I don't which moon is that in reference to what if we I'm now at this point I'm just super curious sorry to waste your time what is the circumference in the moon in chronometers append run what if we say which moon is that that would be a tough one to without the history to know uh this okay yeah that's weird that's weird Behavior like I said I think things are like constantly changing or something because again this is not that was not behavior that I was experiencing literally like yesterday so interesting uh I can't believe it would chat GPT would respond in such a way so as to say um I can't access the history of our messages because like that's literally what it does [Music] I mean we you could literally see we we've been passing the whole history it can totally access those oh man that's funny anyway moving along uh what we'll do now is kind of combine everything into much uh a more condensed way so you'll start off you'll have a message history uh then we might have a function called chat it will take an input and we'll also just uh specify a default role as a user so um we probably should uh are you really gonna do that to me I'm not going to cheat uh so what instead we're going to say is first we're going to say message history dot um append and in this case we do want to append um essentially the input that comes in with the role of whatever the role happens to be most of the time it will be user in our case and then what we're going to say is completion we'll make the actual completion again these are this is literally copy and paste of what it was I'm guessing what it suggested in the beginning was good enough but that's okay uh reply content uh yes indeed then we're going to we can print it out if we want but then we also need to append it to the message history with the role of assistant whatever it did reply and then finally uh we'll just return reply content great so then uh we can do something like this so 4i in range and let's just do two uh and then we'll say user input user's input was and then chat user input I'm happy with all of that actually so cool so then we can go ahead and run that and we can say is it safe to drink water from a dehumidifier it's going to say as a large language model I can't you know it's going to say something like that yes I don't have personal preferences it's generally not safe to drink water from a dehumidifier the water collected by most dehumidifiers will contain impurities blah blah blah blah um okay so then we can continue how might we make it so again we can we'll test this history by how might we make it safe so we'll ask okay how what are things that we might be able to do to make that water safe I'm guessing it'll say something about boiling and blah blah blah use a water filter high quality blah blah blah boil it it does say however boiling will not remove impurities or chemicals that can leach into the water from the dehumidifier okay you get the point so okay so at least in this case didn't try to tell us that it can't reference history I'm still a little Blown Away by that response but that's okay we'll move on um it is kind of funny it's always been funny to me when like these AI models like they're they're boasted as like oh they can use chat contacts whoa It's like it there's nothing impressive once you realize like you're just like it can reference it because you're passing all of it so it's actually not impressive at all but whatever anyways um moving on so now uh that's a real basic example of using it like a chat and then the next thing I would do is um let's try to implement this into a grandio application again uh if you don't have I'm not even sure if I have it on this machine so pip install dash dash upgrade just in case let's install grandio now you could do this in a million different ways if you want to build your own kind of chat UI have at it it's just gradio has a chat element already for us it's super easy and quick to use so that's what I'm going to do instead so uh this was a kind of part of me wants to just do this in a separate uh script I think that's what I'm gonna do okay to start we're going to import gradio as gr and then we're going to import open AI as well we're going to open AI dot API key equals open [Music] I'm just going to copy and paste what we had above here this okay so once we've got that we're going to make a chatbot but instead I think what I'd like to do is make it a joke chat bot so some sort of customized chatbot we're going to start off with the following I just went ahead and copy and pasted this and again you can make this whatever you want you don't have to make a joke bot and also if you don't want to type all this out that's also okay I'm going to put everything up on GitHub I'll put a link in the description uh to to both of these things so this notebook and then the chat app so um so you don't need to worry about typing all this out if you don't want but uh roll user content so basically I'm just saying you're a joke bot I will specify the subject matter in my messages and you will reply with a joke that includes the subjects I message mentioned in my messages reply only with jokes to further input if you understand say okay and again in this case I have gone ahead and injected an okay we are forcing the assistant to agree to our demands and the only reason I'm doing that in most cases I bet it would say okay the problem is sometimes there is variability to this sort of agreement and if you don't get the agreement at least at through my playing sometimes it will go ahead and break character so to speak so um anyways we're just we're hoping to to have it stay strong and steadfast in its commitment to what we asked it to do next what we're going to do is Define a predict function and this function will take input and from here what we're going to do is um oops we will global message history that's fine uh we will append uh to message history role user content input yes uh we are not going to do that though and then what we're going to say is completion equals and in fact that's going to be chat uh no no we don't want that completion here I'm going to come down here my guess why it's suggesting this is I think that check that chat completion is one of the new things from the open AI package roll user messages that is not actually the message uh messages equals and in here it will be message history message history good okay so once we have that we will say reply content and in fact I'm just going to copy and paste that as well let's go down down up up up up up up up this and we'll take the uh we'll take all this uh [Music] there reply content we will print the reply content we will add it to the message history and we will say um go down here we're going to say response equals and this is going to be a modified version of what again was in the radio app so we're going to say message history and then we're gonna go with the ith element content and then so it's going to be these like pairs basically so it will be I uh it's actually just gonna be this I plus one I plus one and then it we're going to do that uh and actually so it'll be these like tuples 4 I in range and then wherever you want to start from so in this case we have a pre-prompt and that pre-prompt contained two messages so we're going to start basically after that so 2 to the Len of the message history message history minus one and then two so that'll give us pairs in this like Tuple form and again the reason we're doing that is just to satisfy this like Radio Chat thing so once we have our actual response that is what we will return the response cool then what we can do is actually build the gradio app so we're going to say with gradio DOT blocks or gr dot blocks as whoops as demo so this is just going to begin to build the gradio app for us and essentially assign it to variable demos so that we can run it with demo dot what is it launch yeah so with gradio blocks demo we're going to say chatbot equals gradio chatbot so this creates our gradio chatbot instance and assigns it to the variable chatbot with gr dot row so we're going to make this like row component essentially and that will contain all of our other like elements and stuff we're going to now specify text field gr text box we're going to say show label is false and then placeholder type your message here is actually fine and then I'm actually not positive this is going to be needed but this was in the uh the demo code so I'm going to say style and then we're going to say container equals false feel free to play with that and find out if that's actually a requirement or not okay so once we have that that's our text box then what we're going to do come down here and we are going to do text Dot submit and we are going to say predict text chatbot a bond what this is going to do is run basically predict pass in that text and this is the for the chat bot uh oops this I just chat bog surprised I didn't show me a error anyways and then pass that to our chat bot instance here and then what we're going to want to do is we can clear that out so we're going to say text Dot submit so also when there is a submission we're going to say none none text and then we're going to specify the JavaScript that we want to run and uh we will I actually got this from one of the I believe garadio developers that works at hugging face this is not my whipped up code at all so it's going to be some JavaScript that will just simply empty out the um the text box and I'll show you why we're doing that here in a second there is another option that is a pythonic way which would be text Dot submit and then we will run a Lambda function that instead empties out none and text so oops we need a comma there and none needs to be capitalized these two things do the exact same thing it's just this uh ends up doing it slightly faster so after all of that we can actually demo dot launch we'll save that and python3 chat app.pi once that's ready we will open it come down here and now what we should have is a joke uh chatbot so we should be able to say uh Pro programmers and Librarians let's see if we get an error why do programmers prefer dark mode because light attracts bugs okay so those are not actually related let's try uh Python and JavaScript why did Python and JavaScript break up they couldn't seem to integrate well how does python like its coffee with class of I didn't say the jokes would be good uh just that it would attempt to make jokes um programmers and cats this is a terrible joke it just doesn't even make sense um part of me is wondering if if this history context here is kind of screwing us up so I'm just gonna restart this real quick and I'm gonna refresh here um so let's do programmers uh and boats these jokes these jokes are terrible oh they're funny because how bad they are um how about python Java and C plus plus I swear as I was like playing with this the other day it was like so much better but now it just seems bad why did the python developer refuse to change jobs because they couldn't function without their Anaconda meanwhile the Java developer was stuck in a loop and couldn't break free but the C plus developer said they love their job because it had a lot of classes that's maybe the best one but couldn't function without their Anaconda oh man good stuff good stuff let's just try a Lego why did the Lego stop singing it lost its voice oh my gosh but it didn't matter because it still had plenty of building blocks to play with I think it would have been a better job a better joke to just stop there but that was maybe the best one so far anyway you get the idea so I mean this was just meant to be a really basic example of utilizing the chat gbt API obviously you don't have to make it about jokes at all so you could come back over here and we could just go up to chat app we could actually remove the entire message history uh and then here down here we could rather than two uh we could start at zero uh let's try this again we'll come up here to our granio app you can launch it I'm just going to close this one hopefully we don't hit an error hello how can I assist you today um and then now obviously you could ask anything you want what are the steps to becoming a doctor so as you can see that message every time I hit enter it just like clears out that text box if you don't have that stuff in there it just won't clear out that text box also I think the colors here are probably because I have dark mode on Maybe I don't know yeah looks a little better if you're not uh messing with the default colors but anyways okay that's quite a bit um what if I just want without a license not everyone can become a doctor perhaps you against the doctor without a license is illegal in almost all countries wow well which which countries is it not illegal to practice without a license let's figure out where we got to move to oh it actually says in every country there's got to be some country where you can practice without a license but okay okay uh in what states in the USA can I practice law without having gone to law school oh that's incorrect interesting are you sure can't I practice law in California for example without having gone to law school can you practice law in California without law school degree I'm pretty sure there's a handful of states where you can practice law so can you take the bar without going only four states California Virginia Vermont Washington so you can take the bar exam but then I don't know I want to say you have to like practice for some number of hours or something like with you know somebody that is a lawyer um I forget all the details but yeah so anyway it's not exactly news that Chan gbt gets things wrong but but anyways so as you can see uh we you can either make it write terrible jokes or you can have it um just be it's basic chatbot you also could issue your uh pre-prompts if you wanted in this sort of chat bot context if you wanted there are like so many cool applications that can be made with uh technology like this even though factually there are things that it gets wrong uh there are um just objectively so many very impressive things that we actually can do when we aren't too worried about factualness so um what I'm gonna do is I think we're gonna I'm gonna stop it here and there are a few projects or small projects that I kind of have in mind that I've been just waiting for an API from uh open AI for chat GPT to work on so at this point I'm going to stop it here like I said uh there are I will put everything up on GitHub I'll put a link to the GitHub in the description uh so you can check that out if you're interested also I'll put a link to the neural networks from scratch book in the description if you're looking to learn more about neural networks from scratch in Python just using python raw python so no other third-party libraries uh and then later we use numpy just simply to make it easier to understand but every step of the way is taught in raw python so if you really want to learn about neural networks and how they work that's the best way to do it in my opinion um totally unbiased and otherwise I will see you guys in another video
Info
Channel: sentdex
Views: 114,684
Rating: undefined out of 5
Keywords: python, programming
Id: c-g6epk3fFE
Channel Id: undefined
Length: 35min 18sec (2118 seconds)
Published: Fri Mar 10 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.