OpenAI API With Python Tuorial 1-ChatCompletion API and Completion API

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello all my name is krishnaik and welcome to my YouTube channel so guys from past many days many people are requesting for the open AI tutorials so finally here I am I'm going to now create a lot of videos related to open AI apis itself and if you don't know about it open AI provides a lot of API to access various llm models to perform various tasks um the prerequisite to start this series you know is just Python programming language so if you know how to use Python programming language if you know how to probably implement a lot of functionalities if you know about different data structures I think that will be more than sufficient to start with along with that one thing you really need to understand is that with respect to open AI the documentation is given in a very much Clear way so whenever we are doing the programming always make sure that you refer to the documentation also so let's go without wasting any time let's go towards this tutorial and I feel right now I am seeing like many of my students are directly using this open AI API itself to create to in their specific end-to-end projects because open AI API definitely gives you state of art models you know which can actually perform some amazing tasks specifically with respect to NLP right so without wasting any time let's go ahead and I will start sharing my screen now what we will do is that uh I will start completely from scratch so please do make sure that you follow me with respect to this and you can also practice along with me uh I'm not going to uh you know Skip any part if there is some errors that are coming I'm going to make sure that that error is resolved then and there so I'm not going to edit anything as such as we go ahead now the first thing first uh what I'll do I'm inside my open AI folder so this is the folder location that I am probably going to create I will just go and write open with code sorry click on open with code so this will open my vs code okay so I'm trying to show you at least from like completely from scratch it'll be amazing you will be able to learn a lot of things itself right so once you have opened your Visual Studio code what I'm actually going to do now is that first of all as usual we will go ahead and create a new environment okay so here is my new environment so for creating the new environment in the command prompt in the terminal what I can do I will just open command prompt you can also do it in Powershell but I feel I work really well in Windows command prompt itself I've also worked in git bash and Powershell it is up to you whatever technique you specifically want to use or whatever tools that is up to you so here I'm going to create contact create minus p and the type of environment that I am probably going to create is that I will make sure that I will create a folder over here itself and all the packages will be installed over here and the importance of creating an environment because I just want to work with open AI apis itself right so whatever libraries is specifically required for this I will only be installing that so conduct create minus P my environment name let's say V and V and then I will give my python version is equal to 3.9 okay so I'm going to go with python 3.9 and along with this uh I will also give minus y right dash y so that if it is asking for me any prompt like yes or no it will automatically be taken right so here uh is where your installation is basically happening now you will be able to see over here your V and V environment will be created right now after that I will activate this environment and we'll start it till then the environment is getting installed or created what I will do I will go ahead and create my requirement.txt requirements.txt okay and for this I will be using open AI Library let's say I want to use pandas numpy these are some good Library is that needs to be used over here and one more library that I am specifically going to use let's see first of all whether this has got activated yes it has got activated uh the next library that I am basically going to use but before this let's go ahead and activate this so conda activate V and B slash okay so once I am inside this specific environment uh uh that basically means I'm accessing this environment itself right now the next thing what I'm actually going to do over here is that I will also go ahead and uh yeah install all these libraries so here what I'm going to do I'll go and save this and let me go ahead and do the PIP installation pip install minus r requirement requirement.txt so let's wait this particular requirement dot txt will get installed okay and the reason why I'm installing only this because I'm going to use only this okay okay one more Library I've actually missed let me write it down so that this will be python dot EnV this specifically whenever you create an environment variable you will be able to load those variables that basically means this Library will be responsible in finding which all environment variables are available over here and it will be able to use it okay so that is the reason I also need to install this specific Library which is called as python.anv okay so this is there so let me do one main thing let me just go ahead and create get my one e and B file I'll tell you why I'm actually creating dot EnV file because there is a reason right whenever we probably start with open AI you know you have to probably create an API right so if you probably go once you log in right so let's say you are in open AI right so open AI over here once you log in inside this right once you log in inside this you will be taken to this page and from here what you can do you will be able to see view API keys and here are a lot of apis queries that are created because you're exploring multiple things over here what you need to do is that just go ahead and create a new key go ahead and write the name over here and just click on create secret key and you have to copy the key the key will basically start from SK right so I have already created a key which I'm actually going to use okay and that is the reason why I have actually created this dot EnV okay and here you can see the installation has happened let me just go ahead and install it once again because in requirement.txt you have python dot EnV okay I will give you each and every of these things uh specifically in the GitHub but I'll not give you the key okay key will not be possible you have to create your own key now the main question Rises that whether you will be able to use the free account yes obviously you'll be able to use the free accounts I will talk about the model pricing how the pricing happens pricing usually happens based on token size and for the free account it just provides you 18 okay so let me just go ahead and explain everything about that first of all what I will do is that I will go ahead and create one key value pair on the name of or open underscore sorry open AI underscore API underscore key okay now this key will probably start with SK and the entire kit itself okay so what I'm actually going to do is that I will just go ahead and set this environment key and then we will probably start the programming part okay so let me pause the video just for this instance so that I can just paste this and then we will go ahead and continue okay so guys I have updated the environment key so I'll again not open it so that you see those keys itself anyhow if you see also I'll delete it probably after recording this particular video now let's go ahead and go through the documentation of opening act understand guys the real content okay I will be able to explain you out many of the things but again if you really want to see the documentation you need to be dependent on this website of open AI so if I probably go ahead and click on the documentation what all things specifically we are going to learn with respect to open AI now um first of all you need to understand open AI API itself they provides what kind of models okay so here are mostly all the models that are probably given to you like GPT 4 GPT 3.5 you know daily specifically for images whisper is basically for audio embeddings moderation moderation is specifically to understand uh to detect whether a text is safe or unsafe right GPD 3 and initial even charge GPT came it came with GPT 3 then it went to gbt 3.5 now it is also having gpt4 so if you already have a paid account you'll also be able to access gpt4 so all these things are there and if I probably deep dive like what all models are specifically there here you'll be able to see in Jeopardy 3.5 you have this models this models in gpt4 all this information is basically there we'll also understand there's a concept of something called as token because the entire GPD model specifically Works based on tokens okay we will go step by step we'll try to understand now whenever we try to use GPT 4 or GPT 3.5 we need to use something called as chat completion API now what exactly is this API don't worry about it I will just try to explain you with examples chat completion API will be super important when you are probably trying to implement some chat Bots kind of stuff you know where you have conversational uh happening from both agents and the end user itself right so in gpt4 you have all these specific models in GPD 3.5 you have GPT 3.5 turbo you have this you have this all these models are there now what is the exact difference between all these models um if I probably try to show you with respect to this particular playground okay and if you probably select any of the model let's say in GPT 3 I'm selecting text DaVinci z003 so in gpt3 this is the most capable model in GPT 3 Series can perform any task other than gpt3 models can so all those information is basically there and if you really want to understand about each energy model you can just probably go ahead and see from this documentation itself but don't worry with respect to coding I will be explaining you you don't have to even worry about anything right so here also you'll be able to see GPT 3.5 when we say GPT 3.5 that basically the most capable GPT 3.5 model is basically this one GPD 3.5 turbo right so here you can see most capable GPT three point model and optimized for chat at one tenth of the price of text davinc c003 now where does this text around say 0 0 3 comes over here right again it is a kind of GPT itself and all these models you'll be able to see what is the max tokens right so this basically says that GPT 3.5 I can give you a better accuracy the speed may be little bit less but yes it will give you some kind of accuracy so if you probably use this text DaVinci and probably see any kind of results better results will be given by GPT 3.5 turbo right and based on the number of inputs and the outputs that you are probably getting that is how the charges happen and that is basically computed with the help of tokens I will explain you each and everything guys don't worry about it but I just want to show you all these things before we go ahead uh this was about chat completion API whenever we want to use GPT 4 GPT 3.5 but if I say GPT 3 see GPT 3 is another uh gptc can understand and generate natural language so whenever I see this kind of models right this will be a completion API not chat completion API completion API okay so let's I think I have spoken many more things as we went ahead now let me do one thing let me just go ahead and say test open AI open AI dot i p y and B okay so this is basically my Jupiter notebook here I will try to write down all the coding and requirement.txt uh I'll not use requirement.txt I will just go ahead and open my terminal okay and here what I'm actually going to do I'm going to just install one more Library which is called as pip install IPI kernel the reason why I'm doing this is that because I will be able to use the Jupiter notebook itself right over there and we specifically need to have this library because it will be able to select the kernel okay so once this is done then we will go ahead and start writing our code now once we start writing a code it would be quite amazing because now is the time that you will be able to understand everything whatever actually saying okay uh I've just given you ideas about some of the other things and whenever the documentation is specifically required I will help you out with that because understanding your documentation is important it probably took me one week to get comfortable with the document then I was able to map each and every points over there so this installation is done now what I will do I will go ahead and select my kernel so my kernel is python 3.97 let's see whether it is working fine or not one plus one done right now the first thing first uh what should we import Okay so first of all I will go ahead and import open AI because this is the library that I'm probably going to use it along with this let's see whether this is working fine or not open AI yes it's working fine absolutely working fine along with this uh if you remember I have also imported dot EnV and I will go ahead and import load underscore dot EnV right and why we specifically required this because uh with respect to load dot EnV it will be able to understand that what environment values we have actually put it'll be able to load that okay along with this I will also go ahead and import OS let's see import OS okay now what I will do I will write openai dot API underscore key first of all I need to set this specific key okay open AI dot API underscore key so for that I will write OS dot get OS Dot get EnV with my open AI key so what was the key name that I had actually mentioned open AI underscore API underscore key right so once we do this that basically means our API key will get loaded now okay now let me just go ahead and make some sales that basically means uh I've set up my key and this key is important if this is not set up your open AI will not be able to work okay now the first thing first as suggested right like how many different types of models specifically open AI has so what I will actually do I will just go ahead and import pandas okay pandas and I will write over here open AI Dot model dot list if I basically go ahead and write like this okay and if I execute it you will be able to see a lot of things that will be shown away okay so like this you will be able to see a list of data so if I probably pick up this specific data as a key I will be able to see all my models see first the model name is whisper then you have another model like this you have something like query and all and there are many models which has been hidden right now if you really want to see in a detailed uh manner what I can specifically do is that I can basically write PD dot data frame and let me give this entire thing over here so once I do this so this is my entire model so over here you'll be able to see right so DaVinci this this this this is there but anytime you want to refer what models to be used I will be explaining you with respect to that okay now let me show you the magic and what all things you can achieve from this okay two things we are going to discuss one is the chart completion API okay API and another one is the completion API so let me just first of all start with the completion API because this actually starts with gpt3 right gpd3 models right so completion API so here what I will do I will say open AI Dot completion dot create sorry completion dot create okay and this is how we specifically use the completion API okay here the first thing that we specifically give is my model name okay the second thing uh that we uh give in this is that after we provide the model name you know we also need to make sure that we provide the prompt okay prompt basically means what kind of input I want to probably give like this right so let's say I will go ahead and write over here what is the capital of India right something like this so let's say this is my message that I really want to give over here now from this model how you will be able to select which model over here again you go to the documentation which model is superb for this gpt3 let's go ahead and say I'll set this because this is very capable this is fast and lower cost than Diamond C so I will copy this and I will go ahead and open this right and I will just go ahead and write my model name right the order that is basically given over there you can specifically use that okay now this is the just this much okay now what I will do I will store this in a variable which is called as response okay or if I don't want to use the response let me just see the output okay now see I asked the question what is the capital of India and the answer is the capital of India is New Delhi okay I'm getting the output it's amazing inside the choices right so what I will do first of all I will just go ahead and write my responses so this is my response and let me just go ahead and print with the response so this will be my response okay now inside the responses I I have got the output also capital of India's New Delhi will understand each and everything in the finished region what this is everything what is this prompt token everything I will make you understand okay so first of all let's go with this uh total tokens okay so total tokens over here was basically as 17 prompt token is seven okay now what how exactly this seven came see what is the capital of India was the question okay and probably if I copy this and go to and search just for open AI tokens okay whenever we give any sentence right whenever we give any sentence with respect to gpt3 let's say I'm pasting this you can see over here all this entire sentence is basically divided into tokens and it can show that okay what is basically over here is is from this page to this is you have the second word then from this page to this the you have the third word from this page to this you have the fourth word then you have the fifth word then you have the sixth word right so these are specifically your tokens and the total number of tokens has been seven now thumb rule right so here also you can see a helpful rule of thumb is that one token generally corresponds to four characters of text okay four characters it is approximate four characters see over here you have six characters I guess seven characters it need not be always four okay as the word becomes bigger let's say I will say capitals okay or I will use some more words now here you can see automatically it is doing the tokenization right so it depends on this specific word and the word that we are specifically using okay uh let's say I like to eat burgers and let's say so here you can see I will just write that as Burger now for in the case of burgers and see and has been taken as a separate token and this entire word has been taken as a separate token right so this token Nigerian is basically done by the open AIP itself isn't it amazing so over here it has basically taken the input uh sentence or prompt as seven tokens let us divided this into seven tokens and it is returning right the completion token basically means the output it is returning so if you probably go ahead and copy this over here and paste it over here right like this here eight are coming right so if you probably add this two sentence two line sentences so here you can basically see somewhere around 10 or 12 tokens will be coming up okay so that is the reason why I am saying you when it is probably taken this entire sentence you will be able to see that it is being the completion token the output token is number of it's It's considering as 10 right that basically means the output is considering of 10 tokens which is form which is basically representing this entire sentence right so the total number of tokens is nothing but input plus this is equal to 17 okay now let me do one thing okay here there is also one parameter which is called as Max token okay Max token is equal to 25. okay uh let me change the question explain machine learning to a five years old okay and Max token is equal to 25 now see oh Max token okay Max token is Max tokens it should be I guess let's see okay the the parameter was Max tokens over here now what it what I am saying right over here so let us say this is my question explain machine learning to a five years old so this becomes my input prompt right now my output prompt it is saying that I ha the maximum number of uh tokens that can come in my output sentence is 25 so that is the reason you can see machine learning is a process of teaching computers to do on their own this is there till 22 tokens you can see over here right how I am saying 22 C 12 22 and total is 34. let's say that if I make it to 10 right default is 15 I guess right 15 or 16. so here you will be able to see machine learning is a way to make computers that's it the maximum number of tokens that I'd actually put in the outside uh in the output is 10 so that is the reason it is just showing in this many words right so this is an example of the completion API here specifically you can see that um uh Max tokens is there right with respect to this and here I can also say that okay act as a act as an AI assistant right and explain machine learning to a five years old right so here you will be able to see machine learning is this this this uh finished region is length now see before the Finish region was stop but now it is saying length because the maximum number of tokens has got reached specifically that limit has been reached okay so this was just an example with respect to this along with this you know I will just copy this and show you some more examples okay act as an AI system explain machine learning to a five years old the max tokens let's say I am going to give it to 25 now along with this I will be using a temperature parameter now temperature parameter usually is set between 0 to 1. if it is zero right the model is not going to take any risk and it is going to give the same kind of answer which will be the best answer right in the case as we go towards one now the model will just not be biased towards the agent it will definitely see multiple reasons it will probably try to see multiple outputs and whichever is the best output will keep on giving that right and in the case of that you know you you will be able to see that always will not be getting the biased results right so that is the reason we set up this specific temperature let's say I'm setting up to 0.6 okay and now I will also give one more parameter n right so here what I am doing I am saying that act as an AI system explain machine learning to a five year old the max token is this this okay so I will go ahead and execute this so here you will be able to see now this temperature values so and so the N is equal to 5 basically says that I am going to get 5 outputs so this is my first output this is my second output this is my third output this is my fourth output and like this one more output is over here okay so here you will be able to see all the outputs itself right and this is what I am specifically getting now why we are using this n parameter see I can probably say act as an AI assistant and explain machine learning to a five years old in three different ways if I write like this that basically means I will be getting three different outputs right over here machine learning is the process of this this this is done by using algorithms so three different ways three ways I will probably be able to give or let me just change this question a little bit okay um top four famous cities in the world ok so if I probably execute this and let me see the output how it will come okay so here you will be able to see New York London Paris Tokyo right so top four famous cities in the world right so if I probably remove this top cities in the world and if I probably try to keep n is equal to 4. now what is the problem with the previous output everything is in one place just in one text so I have to probably write more python code to split all these values and probably display it in separate field now if I probably go and write in this way so here you'll be able to see all the information Tokyo Japan New York this then again with the most billionaire some information is there some information is here in different different orders you know based on different different reasons a reason okay so here you will be able to see the top cities in the world okay so like this you will be able to get different different kind of answers so I can say act as an AI assistant and tell me a poem okay or explain me a poem explain about Ai and Shakespeare word I'm just trying something okay so once this is executed here you will be able to see artificial intelligence is the ability to this this this these are your outputs right now if you really want to get this choices or let's say I will just take n is equal to 1 to make a little bit simpler so if you want to read this text how will you read it so I will take the response I will go ahead and probably talk about the choices so here are my choices right and here you will be able to see 0 Let's see I'm able to get this and then I will go ahead and pick up my text right and once you probably print this all the special characters will also go off okay so this is my entire sentence that I'm absolutely getting the output as okay now let me just try some more amazing things for you so that you will be able to uh see different different queries and all and what capabilities specifically this completion API has right whenever I say completion API this is just a text API okay so here is one more example and this time I will use some different model okay so here you will be able to see response open AI completion create text DaVinci zero zero three I've used act as an AI model to detect positive or negative tweets the person is bad and is biased towards negative sentiments so I have written this right now see this is what is prompt engineering okay so I have told the model to act as a AI model to detect positive or negative tweets so this person is bad in his bias towards negative sentiments the number of Max tokens that I've used is 10 temperatures one I want just one response let's see whether it is positive or negative okay that's it I was just one I've just told the AI model to detect positive or negative to it so that's it so let's see so here you can see it is nothing but negative sentiment the output is negative sentiment right isn't it amazing like this you can probably do a lot of things okay I will probably try to copy and paste it over here uh act as an AI model to Translate language to translate text to French okay so let's say I am writing over here hello how are you okay something like this hello how are you let's see whether it works fine or not so automatically you'll be able to see bonjour comment all zones I think it is fine I guess right act as an AI model to detect or to translate text to French okay I am able to get the output right so even just by using this kind of models now just imagine guys before we using machine learning to train this model to take the data how much effort it used to take we probably if you're using deep learning models we have to probably use it and uh you know based on that only all the expenses usually comes now the expenses becomes very negligible because the token see over here open AI API charges based on tokens right based on token solid charges like how much tokens is specifically use and if it is point zero zero zero two dollars per token that imagine like if you probably have thousand tokens also hardly one or two rupee it is right so it is very much feasible to specifically use this and that is what is the importance of all these things right because it is not going to take a job it is not like everything is going to get automated but it is also going to help you it is going to help you in performing most of your tasks in an easy way right so and only thing that you specifically require in this is about obviously what do you require in this just Python programming language and you should definitely know how to use the API itself so this was about the completion API you know perform different different types of type of task uh it is up to you just be much more creative with respect to this kind of prompts and use it okay now let's talk about the chart completion API itself uh because chat completion API is also important from the day uh that you have specifically chat through your GPT 3.5 and GPT 4. these models that you will be able to see this is only used in chat completion API right so I'll just give you an idea about how to specifically use chat completion API so here I'm just going to write chart completion API and here I will say openai dot chat completion dot create and here I'm basically going to provide what is first thing that I'm going to provide is obviously my model name and now after I provide my model and let's say I will be using this one which is very good and you can see Max tokens you can allow only this much so here I'm basically going to use this now the next thing that we specifically provide is messages now messages will be in the form of key value pairs the first message that you specifically specify over here is um a kind of role first of all you specify a role let's say if the role is assistant or is the role is system because there are three kinds of role which I will discuss and one more role which has been recently added which is called as function calling I will also talk about it and the next field that I specifically specifies content okay so let's say I say act as an AI assistant okay act as an AI assistant okay so this is my now as soon as I probably write this role is equal to system and content as at is an AI assistant whenever we see a roll as system that basically means I'm telling the agent to probably act as an AI assistant right the next role that you can probably provide over here is that whenever you specifically Define any kind of role I can copy this I can paste it over here right the next role that we specifically say is something called as user and in the user let's say I am asking a question hello how are you right right now once I use this and let's say what is the output that I get so here it is using the gbt 3.5 Turbo and whenever we say chart completion API it is a conversational API that basically means you will be able to append this number of messages many number of time very much suitable for chat Bots itself so here the output you will be able to see the role is assistant so whenever you get any output right from a agent from a model that is specifically in the form of role right role is equal to assistant and then you have content hello I am an AI assistant so I don't have any personal feelings I am here to help you how can I assist you today so all this information is basically given from the uh from the from this specific model that is GPT 3.5 turbo okay now here you can understand that what is the similar difference what is the differences between this completion API here we just used to give one prompt here you can give n number of messages and you can append the specific messages as you like okay and N number of so after this also you can probably write one more question so that context will be remembered from this uh for for this API right that context will be entirely remembered okay so let's say I will give you an idea about it okay I will just say I'll ask you this question see I'll say role as a user my age is 33 and uh I am a data scientist let's say okay so this is my question so this is my next input that I am probably giving and after this what I will do I will write one more role and I'll ask a question what is my age let's say what is my age right so now it is going to give me this specific answer as an assistant okay I've I've put that information my age is 33 and I am a data scientist just to give some info to this chat CPT this is sorry GPD 3.5 turbo okay so here you will be able to see based on the information you provided your age is 33 now see it is being able to remember this context and N number of messages I can probably append because at the end of the day this part model is making sure that all the information is basically utilized in terms of context right so that is why this chart completion API is quite amazing and you should definitely use it right so this is what is about this one and probably I can also do one thing is that whenever you are trying to use this always make sure that because this this this message is that you're appending this will play a very important role when we are probably discussing about function calling and that I will probably take in my next video but just to give you an idea how things happen this is the most two important apis that we specifically use completion and uh chat completion the models that you can probably play with you can probably use any of the specific models and use it right so uh try to use it try to try practice it you know because as we go ahead more and more different examples will definitely come up with and I really want uh I will probably give you every tutorial about this how things will be how things are not you know how you can probably convert this into an end-to-end model how you can probably do fine tuning but we will go step by step we'll try to understand it okay a couple of questions always remember that that you have charge APD 3.5 sorry gbt 3.5 gbt 4.0 that is only used for chart completion API right so yes this was it from my side I hope you like this particular video more tutorials are going to come as we go ahead um I don't want to make it a very long tutorial so that you will be with me as we discuss as we keep on discussing in the next video we are going to discuss about function calling with the chat completion API right now if let's say if you are having a third party API right and from the third party API also if you just give that Json right automatically the open AI API itself will be able to read that particular Json and will be able to give you the information so yes this was it for my side I will see you all in the next video have a great day thank you take care bye
Info
Channel: Krish Naik
Views: 43,443
Rating: undefined out of 5
Keywords: yt:cc=on, openai tutorials, code generation using openai, chatcompletion api, completion api, machine learning using openai, data science tutorials
Id: CbpsDMwFG2g
Channel Id: undefined
Length: 36min 37sec (2197 seconds)
Published: Fri Jun 30 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.