How to use ChatGPT API Python for Beginners - Full ChatBOT Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what's up welcome to one little coder in this video we're going to learn about open AI chart GPT API in Python there are two different methods to access the API at this point and I'm going to show you both the methods one using openai Library second one using the open a endpoint so we're going to cover end to end starting from how to create your openai token to actually building a python code on Google collab where you can chat and get a response so this entire thing is probably going to take less than 20 minutes so I would strongly suggest you to stick around till the end also a few people had asked me it how do you up you know change the parameters update the parameters of open a chargpt open hrgpt APA so I'm going to also cover that in this video where how you can update certain parameters like temperature and other apps let's get started with the video first thing that you have to do is you have to register for an open EA account and then you need to get a token so if you register for an open a account you need an email ID you need a phone number because they don't want you to use multiple accounts and after you do that this is the screen that you would reach so you would uh okay this like you would have the account you could have logged in after you have logged in you would reach a place like this where I'm going to show you how you can get your API key when you go to the account Tab and inside the account tab you can go to API keys I can link this directly in the YouTube description it's easier for you to get started so go to API keys and here you can create a new secret key so when you click new secret key it's going to actually create a key and you need to copy this key and paste it somewhere else because you cannot view this key after it has been created so I've created the key I'm going to delete it and I'm going to show you in this video APA key but that is also something that I'm going to delete after this video never ever show your API keys to anybody all you have to do is add it as part of an environment variable there are a lot of different ways to store any APA key like a Json secret value so use any of these values if you're going to build an application do not share your API key please do not I've seen a lot of people coming to my GitHub repository and pasting opening APA key please never do that never share it anywhere at all now having said that at this point you have an open aircon and you have an API key now let's jump into the Google collab where I'm going to build this code so this code first thing that you need is you need to install openai application open AI python Library so it's quite simple pip install queue is for a silent quiet installation and then open a once you have installed then you can import the open AI Library so this is for the first method so like I said there are two different methods to access open a chat GPT I'm going to quickly show you that let me show here so first method is this method so if you see this code you can use open AI library and you can use the API key and then you can use open a chat chat completion this one open a chat completion and then chat with open chat GPT so to use chat GPT the first method you use charge GPT in Python the first method is to use open AI library and use open a chat completion the second method is to actually use this endpoint and make a post request so I'm going to start with the first method and then we'll jump into the second method what is the first method the first method like I said you need to import open AI Library so import open AI library after you import open AI Library it is very important sometimes to set a context to this charge gbt API so you need to tell the chat bot here that what does this chatbot supposed to do so for example you can say you are a kind helpful assistant so you can say this you are a kind helpful assistant so this is the context that you are setting with the chat GPT APA so that the API knows how to prevent how to behave how to do how not to do certain things for example you don't want the API to be using let's say followers or something when you are going to deploy this kind of system with children so there are certain nuances that you need to keep in mind so that is where you set this context about what this chatbot is ideally supposed to do like what is the behavior or what this chatbot represents and what is the duty of the chatbot and I'm going to show you a different aspect as well but for now let's stick to you are a kind helpful assistant that's done the next thing is it's it's um it's this it's a Json in the Json you can see role is a system and the content is uh what the setup that you did and the next line if you want to send a message now you have got two different rules one is the system role as the second one is the user rule so system in this case is stat GPT and user in this case is like I like whoever is going to chat with like if you're going to deploy this in public then whoever chats with the chat GPT APA is the user so at this point we have set up the message again you don't have to take notes while you are watching the video the entire Google collab notebook I'll link it in the GitHub repository so you can directly copy the code from there and paste it in your own vs code Visual Studio core or Google collab Wherever You Are so the next thing is now at this point we have successfully set up the context the next thing we want to do is we want to create an infinite Loop so that we can get have a chat mechanism so create an infinite loop with while true and then get the user message from input this is a naive way I'll have a different video probably like tomorrow or day after tomorrow where we will build a gradio application to have a chat bot in and itself but for now this is a very simple chat mechanism where you collect the user input it prints out a response then you collect user input so it's going to be very simple now I'm going to quickly show you how it looks so that you know it's easier for you to comprehend when I explain the code so I'm the user I'm going to ask a question I'm going to ask who is the famous most famous chess player okay so you can see it says Gary crash Pro of Russia is widely considered to be one of the most the the most famous chess player in history he was ranked number one so now one of the things that when you build a chat bot you would have seen a lot of my tutorials is that the context or the memory is the problem a lot of times chat Bots do not know what you are talking about because you sent the message it's going to go look for the information or answer and then it's going to come back and give the answer but it doesn't link so to show you that chat GPT in this particular place has context I'm going to say first I ask who is the famous most famous chess player says Gary Cross Pro blah blah Now by definition if I use a pronoun uh human beings would know that that pronoun is referring to Gary kaspro so I'm going to say has he lost any match before so I'm just trying to ask has Gary kasproof lost any match before so I'm going to send this message and it says yes Gary gaspro has lost matches before so it says even though he is considered one of the greatest yes players all time it he is not unbeatable so now the point is one you can ask question second it can also hold memory so it can have like two things that's what you can see like I can even see how does he compare with Magnus Carlsen also I mean spelling correctly yeah so it's going to make a call and then it has given me a huge response but the point here is that this works absolutely fine and I'm going to show you the code now so what is the code like I said we are going to create an infinite Loop that's why it is continuously running the message is we are getting the message from the user using a simple python function like probably the day you started learning python you would have learned input as a function but that's what we are using and if the message is present we are going to append this message with existing message and what are the existing messages that we have got we have got a message that says the role system content and you are a kind helpful person so now why does it have a context it's because we keep on appending the messages as well and then the next thing is now now if you know your messages that's why this does not come inside your infinite Loop it stays outside because you don't want to override your message so that's that's again another information so now how do you send a message it's a simple like dictionary uh like a Json you have a role who is a user what is a content content is the message that you got from the user now you're going to invoke open AI dot chat completion and that is the chat object and you can say create and what is the model that you're going to use in this case GPT 3.5 turbo you don't have to necessarily stick to only this model you can also use there is a latest version of Turbo that we discussed yesterday in the video the zero three five one if I remember correctly you can use that as well if you want right now we're going to use GPT 3.5 turbo that is what is powering the actual charge GPT interface if you go to chat.openia.com send the messages and again get the response from chat dot choices the first object get the message message content and then reply when you reply then you can say this is an assistant responding like this is and what is a assistant name in this case is chargpt and what is the response so that message also gets appended to the messages list so this way you keep the context as well so this is the entire um if you see this is the entire code like it's not even like five lines of python code probably like 10 lines of python code you have successfully built a chart GPT clone or chargpt um actual chargpt chatbot using python using open a package it's quite interesting you can chat like I can ask any questions and we can do all these things with the first method so the first method we used open AI package python package to create a chat interface or a kind of a chart interface within less than 10 lines of python cool now as as I told you we have already added the key as a variable here but uh I would strongly encourage you to add it as an environment variable not not like a variable that is visible now let's get into the second method now what is the second method now you can ask me first why do you need the second method the first method doesn't give you a lot of flexibility in how you want to do but the second method is an APA endpoint which means you have got a node.js application you've got a JavaScript application you've got a let's say laravel application a PHP application you can use this endpoint anywhere that you want you are not restricted by a python Library you can literally use this endpoint anywhere you want and that is the advantage of having a HTTP endpoint so that's one reason why you want to use the second reason is you can do streaming data what does it mean um if you if you know chart chart GPT when you type something chargpt gives you a very nice animation um when the response comes that is called streaming so you can stream with the API endpoint second the third thing at least I feel the most important is at least at this point it's very easy and straightforward for you to update or change parameters of charge GPT API using the API endpoint than using the python Library so for these three reasons I would if you are going to build a production level application I would strongly encourage you to use the APA endpoint so now what is the APA endpoint the API endpoint is simple it's apa.open a.com V1 slash chat completions so this APA endpoint uh when you are going to send the payload okay if you are not very familiar with apis API is like a system that helps you talk to another system it's like a gate now when you're going to talk to this API you need to send a payload and the a is going to respond back with the response so a play payload goes and the response comes back response now when you send the payload there are certain things that you need to send not mandatory but certain things first what is the model and what is the message like you can see that I've asked the same question who is the most successful chess player so if you're going to build a chat interface now this is an F string ideally you can put um like a python object there that will use that so you have got the role you have good user you've got content and this is the question that a lot of people have been asking like how do you change the temperature how do you change you know top P so these are certain important things because sometimes you want chatgpt to be very creative let's say you are building an application where um you're building an application where it has to write creative exercise there you might have to keep your temperature very high where sometimes you want the hallucination to be very less so you are going to do the trade-off with a temperature I heard that open Microsoft Bing recently also lets user control the temperature so it's a good thing if you can do it and it's always good thing for you to have this control when you are building a production level application next is all the simple things the most important thing is if you want streaming data you need to enable stream true but right now I don't want streaming because I I'm not processing streaming data per se but when we build a radio chatbot we'll do streaming so at this point we have streaming false and then this is our payload so this is the payload that we are going to go send it to the chat GPT APA endpoint that is a completions endpoint and what is the header so somebody has to authenticate it is actually us so the header says like it's an application Json type the content type and the bearer token the token is exactly the same token that we were using before so you can literally use the same token and this is an F string so I am calling the same python object which I defined above so the next thing is I'm going to make a post request in HTTP request a popular ones get and post so we're going to make a post request with the APA endpoint what is the header for authentication and the payload as the Json and stream is equal to false because we don't want it to be stream get the response back and once you get the response back this is what you get to make it easier for all of us to read I have put it inside a Json viewer because it's a Json object so you can see first it says what is the chat ID that is going to be very helpful what is the object that we use chat completion when is it created Unix timestamp what is a model that has been used here we are not using 0351 we are using zero three zero one which is the default charge GPT model what number of prompt tokens this is quite important if you are going to calculate cost for example you could be building an application where you need to charge the user so prompt token and prompt completion token so total how much tokens that we have got so total 95 tokens prompt had 16 token when we got completion 79 tokens total 95 tokens and what is the message that we sent the message of the assistant what the assistant replied back it says the most successful chess player in S3 is widely considered to be Gary Castro blah blah blah and all the information that you have got now this is the second method that in which you can access chat GPT APA using python so I'm going to go ask another question here so I'm going to say um what is the first computer in the world and send the message I'm going to get the Json object once I get the Json object I mean the response object the response object I'm going to say response dot content I will copy this Json and go to the Json viewer paste it here see it here so you have got the Json in the first Json you have got the ID chat completion the time the model the usage how many tokens we send how many tokens we received total number of tokens and the message the assistant says the first computer in the world was an electronic numerical integrator and computer called eniac created in 1945 at the University of Pennsylvania nice so overall if you see in this video we learned first how to create an open a token which is quite important for this then we learned the first method to use chat GPT API in Python using open AI Library the second method we learned how to use open AI AP chat GPT endpoint which is the completions endpoint to use charge EBT in Python so now the second method could be applicable for any programming language like I said anywhere where you can make an HTTP request like PHP JavaScript any framework collaborable node.js anywhere you could use it so overall we have learned how to build a chart GPT on how to use chat GPT API inside python using these two methods in the next video probably I'll try to build actually like a chat interface where you can chat and communicate I think that's going to be fun but meanwhile if you have any questions let me know in the comment section the code will be linked the the I'll upload the collab and the GitHub repository and Link the GitHub repository GitHub repo in the YouTube description so it's easy for you to get started with make sure that you are not burning your cost by you know sharing your open AP open a APA with somebody so make sure that that API key secret is secret and if you have any other question let me know in the comment section otherwise I hope this was helpful video in learning how to build a chat bot using chatgpt APA in Python so if you want to use if your friends want to use chat GPD API in Python please share this video with them I'll see you in another video Happy prompting peace
Info
Channel: 1littlecoder
Views: 28,944
Rating: undefined out of 5
Keywords:
Id: Vurdg6yrPL8
Channel Id: undefined
Length: 17min 37sec (1057 seconds)
Published: Wed Mar 01 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.