How to Access OpenAI, ChatGPT, GPT - 4, GPT - 3.5 Models for Free | OpenAI API in Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hi welcome to my channel learn code everyday in this video I'm going to show you how to use open AI apis so here in this video we're going to use the open Ai apis and using which we're going to generate the text from the prompts we're going to see how we can able to achieve this first and foremost first thing you need to go to the open.com and here you need to log to your website that is openi if at all you're not having the account in the openi then you have to create an account in openi with that you can able to log into your account and get the API case in my case I'm already having the account with me I'm going to log in with my Google all right now I have successfully logged into the openi and as soon as as if I log into this it is giving me two options chat GPT and API I hope you already know some knowledge about the chat GPT and let's see the some kind of a prompt that we can able to generate with the chat GPT for example in the chat GT uh by default I'm using the chat G 3.5 and you can as a chat GB generate an email to my boss for cly and let me mention some details my boss name is John and my name you can give something like um Mike okay that's what some kind of a prompt and uh using which my chat gbt will generate this let's see let me give the most specific I just want to generate a very short email to my boss for cly let me send a message and see the what the request that you're getting all right this is the really cool small email from the chat GPD that we got and again you can see which we actually use a CH GPT and using which we generate this now the idea of using the API is we're going to send the prompt and we're going to receive this message using the apis and with that you can able to use to your own websites in order to go in in order to get those things what you have to do is you need to go to the openi and uh the main website of the open.com and here you can see there is API and click on this and here you can go to the docs of this okay since you have already log to the open aai and your details will also get log to the open AI platform particularly for the docks and the API reference and on the left hand side you can see some pain and inside that we have some tabs are there like playground assistant fine tuning API Keys files usage and settings and what you can do is that first click on the usage the moment if you click on the usage it will give you some monthly analysis and some kind of analytics that you have for the monthly usage that is how much money that you're using for the open by default every new account that you have signed up for the open will get the $5 credit for free means you are in the free Trail for example example in my case you can see that I got the $5 credit for free and I'm using the free Trail service or free tril subscription and here you can see out of $5 I have used $2.4 means out of that you can see approximately $3 is already left out for me and you can see your plan details and everything in the settings on the left hand side below usage you can see the settings is there you click on this and under this there is a billing is there if you click on this billing and here you can see the subscription type the default or by default you'll get the free Trail or if at all you want to get the more payment methods or some kind of a premium pricing Etc you can also select those things but as of now you will get the free Trail if at all you are not seeing this kind of messages probably your free trial might be expired and if at all you want to get the free TR limit and create a new account in the open a and you can get the free trial particularly for the text Generation all right make sure you have some credit limit if your credit limit is zero your API case will not work cool so once you have the credit limit and everything is fine and then on the left hand side about settings you can see there is API Keys is there right click on this and here you can see that I have already created one of the API key and uh let me create the new API key just click on the new API key and write the name of the key let's say it is my key and that's the name I just want to give and in the permissions you give the all the permissions don't select the restricted or read only let's select all and click on create secret key all right so once youve done this you can see that you can get the some secret key let's copy opy it and please do remember that you can't able to view this again so as soon as if you click on done you cannot able to view this API key again so be make sure and just copy it and save it in some kind of a text document where you're going to use that API key so let's do one thing let me open my Notepad and paste the API key here and this is the API key I'm going to use to connect to my GPD models Okay so let's paste it in somewhere and uh you also you save this in some safe location wherever you can find this for example I'll just put it in my desktop and let's say this is my open AI API key and let me save it okay so that's an API key which is encrypted and once you click on done and that that's it you can see that you can't able to view your API key never you cannot use it and if at all you forgot to copy it then you delete this and create a new secret key again because you cannot view this API key again and again all right so done now we have ready and we have created the API key and the next step is on the left hand side here you can see there is a documentation is there just click on the documentation the documentation from open AI is excellent you can see simply use this documentation and connect with the help of apis and you can able to create your own application and the documentation also contains a code which is actually ready to use now I'm going to use a documentation from the openi and connect to the GPT models using apis using the documentation okay if we click on this quick start and we have some kind of a developers quick start is there and this documentation will give you the good understanding of how we can use the open AI API keys and uh using which you can connect to the GPT models all right if you look into the code or if you look into the steps to connect to the open a API is the first step is we need to set up the python so make sure that you have already the python that is running up with you if not just install the python from the python.org website that is official website set of this and here it is giving you the the minimum version that you have to use the openi is 3.7.1 and or newer versions anything more than 3.7.1 at least as of now which means that make sure just look into the documentation today I see like know 3.7.1 maybe tomorrow you might get the different python versions all right so you make sure you install python and then set up the virtual environment this is an optional step which is not actually required and if you want to create the virtual environment that is also good and next we need to install the open AI Library so in order to install Library we can use the pep install hyphen I upgrade and open AI okay so let's see the step number one in my case I have already installed the python with me so I'll just proceed with the setting up the virtual envirment and uh installing the open AI python Library let me minimize this and now you can open your any text editor that you're using in my case I'm going to use the pyam the most widely used uh text editor if at all you are using a Jupiter notebook or spyer or Visual Studio code it's happy to use whatever the python versions that you want okay so let me go to file and uh let me create the new project and uh let me create one folder let me set up this and uh by default let me also create the virtual environment here and uh the python version that I'm using is 3.11 if at all you having the new python version it's happy to use but make sure the python version that we using is greater than 3.7 once you're done just click on the virtual environment and uh create from existing SCE this window and next all right now I have successfully created one project and also install the virtual environment here okay cool now the next step is we need to install the open Library let's quickly install the open Library the let me use the pyam itself and let me open the terminal and by default you can see that I already connected to the virtual environment and U in the virtual environment of the Python let me install the open using pip install hyphen iph upgrade so obviously this command is not required let me use just spip install open Ai and press enter which will install the open package for me all right now I have successfully installed the open and uh let me create the new main file let me remove everything let's create some empty file and here let's test whether we have successfully installed open a or Not by just importing import open Ai and once you're done let's print open AI imported successfully okay there are two ways that we can run is just type the python main. time it's the one of the way that we can able to run and which means that I have successfully imported the library so which means my open AI is working and installed successfully as I said there are two ways that we can run this one is with the in the ter terminal the other is I'm going to show you the python console which I will use in the next part of the lesson okay so this is the python console anyway let's go to my documentation and we are ready and we have successfully installed the API open app now the next step is we need to set up the API key let's see the documentation for the Mac or Os you can use uh some basic steps that we have the first is we need to open the terminal and you need to edit the bash or proc file and you can in that you need to execute this following command and you do all these things but simply what I can do is even in the windows we have some kind of uh multiple commands that was there but of course this is actually recommendable instead let me follow the second step is setting up the API key for a single project so what it actually means is that it's asking me to create a variable name naming as open aior API undor key and provide the API key to it and you can call using the client open AI so by this you can simply able to connect to your code to the open AI okay let's go to my code and what I'll do is uh let me import this so let me import from open AI let me import open AI now let's create some variable named client equal to open Ai and here we need to provide the API key for it so before this what we will do let me create a variable open AI underscore API underscore key now you remember that we have already copied and pasted in one of the text document our API key let's carefully copy this and um open the strings or codes and paste the open key here let's save it and now in the open a method let's provide the API key equal to open AI API key now let's save it the next step is we need to Simply send the prompts to this client so let's see the steps that what we can do here is the step number three is sending your first API request in order to make the API request what you have to do is uh we need to create the chat completions and then we need to select the GP models that you want to select and followed by the message that you want to send to here and if you carefully look into the message here we have the system content and uh in the content it's mentioned something and also the role in the user content this is actually the promt that we are asking the chat GPT or the GPT model what I'll do is let me simply copy this piece of code and go to your text editor or code editor and paste the code here and if you debug the code or if you look into the code very carefully what we have is a client in this client we have the chart completions and create and here we are passing the argument as model and the model that we have provided is GPD 3.5 turbo so how we can know this model so what is the models and what are the different types of models that we have let's quickly look into this if we go back to the documentation page and here you can see in the next section of the quick start there is a models is there if you click on this models you can see the different models that which we can use for the API key request H GPT 4 gp4 Turbo GPT 3.5 d e DTS fiser embeddings moderation GPT Bas Etc and as I said like we can also use the gp4 models you can also request the apis for the gp4 models but however there is some pricing of course for every API request that we are trying to request has some pricing isself for pricing details of these models what we can do is go to the website that is open a.com and in the official website there you can see that the navigation bar above there is a API link is there just click on this API and below that you can see the pricing is there just click on the pricing and once you are in the pricing page then you can scroll down and there you can see the pricing for the language model it says that the multiple models each and every model has a different capabilities and the price point and the prices are per thousand tokens means for every thousand tokens the prices has been made generally you can think the token is each and every word but more or less like you know it's a canot a word it could be like you know more than one word let me give you an example of the tokens and so that it will be clear for you as I said token can can be referred as each and every word let's look an example with a promp consider the below example here we have generate an email to my boss requesting for cly which is my prompt and this prompt you can split that into tokens which have the 11 tokens are them and you can understand this 11 tokens is something like this generate is one token an is one token email is one token and so on you can see that generate an email to my boss request you can see this is a requesting even though requesting is a single word but actually you can split this reques in you can split this requesting in two words because this is a continuous tense request and ing so so that the chat gbt can understand this is the continuous tense so the token for this it be the request ing which is a tu which is a two two tokens for cly all right so that's the reason like know generally you might have the more tokens than more number of words and in this case you can see let's say we have 11 tokens for this and let's say the response that I'm getting from the chat GPT or GPD model say something like this High boss remember like even a single punctuation mark like Karma Ken can also be represents a single token and something like this once you have all the response let's say we have 989 tokens are there okay which means the context tokens can be defined as input tokens plus output token in our case let's say it is 11 and uh 989 which is combined together for the th000 tokens means for a single prompt means for the for a single API equest that we made we have the 11 input tokens and 989 output tokens Al together we have the th000 tokens of there so that is how we can Define the context tokens and the pricing is based on this th000 tokens cool now let's go back to the website and here I'm in the website and you can see the prices are based upon the th000 tokens sometimes you might also get more than th000 tokens or sometimes you might get the less than to th000 tokens it's all about the proportion of the values or the proportion of the tokens that we have made is based the pricing is purely based on this for example the GPT 4 you can see the gp4 turbo the pricing for the gp4 turbo again they can have the we have three different models that is gp4 one to five preview and so on for that you can see the pricing for the input tokens is the 0.01 is for the input token whereas for the output token for this model the pricing is 0.03 per th000 tokens and so on and similarly you can see the pricing for the GPT 3.5 will talk about uh the the input token for that is 0.01 is for the 1K token and the for the output token the prices is 0.020 for the 1K token so which is less when compared to the GPT 4 is almost 10 times lesser than the GPT 4 models okay so this is how the pricings are actually made So based on this you can select the model whatever you want all right when it comes to the modeling of the GPT for there are two different types of models are there one one is GPT 4 and GPT turbo gp4 is a large multimodel model which means that it will accept the text or image as a input and it will return the output as a text it is also used to solve the many difficult problems which the GPT 3.5 doesn't solve and if you look into the models and there so descriptions of this there are different models or many models we can able to use with the API request starting from gp4 0125 gp4 Turbo preview GPT 41106 and so on and uh you can see the description of the D model for example this is the description is gp4 Turbo and which is the new and latest model and also you can see there's a context window which will opens or which will take up to 128 0 0 which means 128k tokens and also you can see the training date of this it is up to train up to April 2023 and also you can see the different models of the different gbt models and also the context window for the different models is also there here and if you scroll down you can see the the important thing that we have the difference between the GPT 4 and GPT 3.5 is not significant particularly for many basic task however for the complex reasoning situations only for the reasoning and also some kind of a math solving problems the gp4 is much capable than any of the previous models and when it comes to the GPT 3.5 again we have the different models or there whereas the GPT 3.5 turbo 1106 is the latest GPT 3.5 model and where we have the context window of 16, 385 tokens and the training data is up to September 2021 so what are the questions or what are the things you ask it has a training data till September 2021 and like this we have the different GPT 3.5 models and at the same time you can see some Legacy which means those are outdated models and probably will remove soon for example here you can see the GPD 3.5 turbo 0613 which is will be deprecated in July 13 2024 okay so wherever you find some Legacy and if at all you want to use this Legacy model please be careful because those models will be deprecated very soon okay so like this we have some the different models this are what are the models that we have used or what have discussed is the GPT model also known as a text generated models and next we have some di which is the image generation TDs is the text to speech wishper is speech to text embeddings moderations GPT Bas Etc cool so like this we have the different models out there and you cannot be able to connect to the different models now let's go quickly go back to the where we have Stand Out is this let's go back to my code and here we can see the model that we have used is GPT 3.5 turo okay by default it is asking gbt 3.5 turo and if at all you want to connect to the different models just select the model that you want to use for example let me go select the different model of the GPT 3.5 turbo if I scroll down and here the model I'm using is this let me use a GPD 3.5 turbo what I can use simply let me copy this text and go to the code and paste this here done this is how we can select the model next when it comes to the messages we have the two different types of roles that have set one is the role for the system which means for the chap chart GPT or GPT 3.5 model and uh here you can set like you know what are the assistant you want from the model it's saying that like uh by default it giving some kind of a poetic assistance skill in explaining the complex programming concept with the creative flare instead let me spe give some generic things specifying that you are a helpful assistant is general purpose and need know about the all the topics and not into the specific thing suppose you want to know some details about the specific thing then just give the context as the specific thing about the questions that you have okay now this is the prompt that I want to use or the user is requesting for which will there you need to provide here in the prompt here now what I'll do is let me ask right or let's do the same thing what we have did in the previous lesson or the previous prompt that we have is write an email to my boss for C and and uh mention something like my boss name is John and my name is Mike okay that's it so you can simply execute this so for that what I will do is let me first um execute step by step let me execute this first two lines let's select this and I'll execute the line by line and I can execute the selection in Python console simply the shortcut keys Al shift e and let me connect to the openi first and with this you can see that I got the success message which means I have successfully connected to the openi now let me send the prompt to this thing or basically this is the API this is how we're going to send to the open air service and from there we're going to receive the message okay now let's select this and execute the selection and python it will take a while please be patient while you getting the request from the open a all right so we have successfully got the message and we got successfully received the information from the openi and let's see how we can retrieve the information how we can retrieve the message from the open eye for that let's go to the documentation you can scroll down and U there you can see that it is asking in order to view the message it will there in the choices of zero and then you need to see the message what it means is the completion is actually the variable name where we got the request information and uh here this is my completion let me expand this and in the completion you can see we have the choices is there which is a data type of list and the number of values that we have or the number of elements that we have in the list a one let me expand this and inside this uh let's select the zero and inside this we can see there is a message that is what we are actually interested here inside the message we have the content and this is what the message that we are actually interested okay so what we can do is uh let me inspect this or let's do one thing let me print this we can simply print this using first let me take the completion tot and from the completion we need to go to the from the completion we have to go to the choices and uh in the choices which is actually the list let's do one thing let's uh select this and uh custom view in the data view we can see the data of the how it is getting or let's see inspect huh yeah this is cool so inside that we have the choices and inside the choices because we are in the choices in that we need to select the element zero and uh inside the element we have the messages in the messages the content which is actually the information is there and I just want to see this information the information is this let me view this and it means that this is the subject is a request for C Dear John Dear John I hope this message finds you well and blah blah blah and you can see the okay let's do the same thing and we need to select the zero and after that we have to in the zero we have to go for message and Dot content that's it so this is what the message from the chat GPT or the request that we got from chat GP and you can print the message let's close this now let's select this and execute the selection and here you go this is what the output from the openi API request from the model GPT 3.4 turbo 1106 and this is the output that we have you can see that the subject is request for the CLE and Dear John and I hope this message forms you well or something like this and finally we got the output cool right so this is how we can able to uh create what the prompt you want let's do one thing let me uh replace let's say this is my prompt with the variable prompt and here let's write the prompt here and let's say the triple codes and now see how we are getting this and let's say try with the different U prompt let's say write a shot article about football okay so I'm just trying to make it more generalized now let's select this and you need to execute the selection let's wait for a few seconds until you get the message from the openi API cool you can see that we got the output and this is the very short article about the football the football is often referred as a saucer in some countries is one of the most popular sport and so on and beautiful right this is how we can able to get the request from the open hope you enjoy this tutorial and please subscribe to our Channel thank you very much
Info
Channel: datascience Anywhere
Views: 4,397
Rating: undefined out of 5
Keywords: OpenAI API, Connect to ChatGPT with API, How to access OpenAI GPT model with API, ChatGPT Integration, GPT 4, GPT 3.5, how to use openai api for free, chatgpt api login, chatgpt openai, How do I get access to OpenAI API, How do I get API access to GPT-4, How to access ChatGPT API for free, How do I train my own OpenAI model
Id: se8NgW7voz0
Channel Id: undefined
Length: 34min 58sec (2098 seconds)
Published: Sun Jan 28 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.