How to Use ChatGPT API Create Your Own AI Chatbot Google Collab Python Code Tutorial OpenAI

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
this is ritesh srinivasan and welcome to my channel in this video let's look at the chat GPT API so yesterday there was this announcement from open AI that chat GPT and Whisper models are now available on their API so our developers can get access and they can develop applications so I thought let us try it out so here is the chat completion API documentation okay so the model used over here is GPT 3.5 Turbo and it is as simple as this code where we call this particular method open AP openai dot chat completion dot create and you have a model which is GPT 3.5 Turbo right and then you have messages so if you look at the message there is a role okay and here it is system and then there is content so you are instructing chat GPT or this particular model to be a helpful assistant over here that is the content then there is the role which is user and there is content so this is the user query okay so this messages object uh is it must be an array of message objects where each object has either a role system or user or assistant okay so the first system is nothing but instruction assistant is what is the response produced by the chat GPT uh or chatbot over here okay so let's go to the demo it's very simple I I'm using Google collab over here you need to install openai okay you need to store your open API key in a Json file or you can directly specify over here so what I've done is I have stored it in a Json file okay so let me start from here right so then I set the open API key over here right and then all I do is this okay so for the first query which I want to ask is that I do response is equal to open AI dot chat completion.create model is GPT 3.5 turbo and in the messages I have role system Sam's asking the model to be helpful assistant and the query I'm asking is what is svm okay so let me run this is very fast actually now let's see the response so if you see this is the response object which has been generated okay so if you see over here within the first choices object you have this content so this is the response which is coming from chat CPT okay or the chatbot model and here the role is assistant and then our details are there about tokens and other things so it is as simple as this particular call now what can you do with it okay so what you can do is you can extract this message right which is what I'm displaying over here okay all right and then what I'm doing is I want this to continue right so I am creating a messages object okay and I've already asked what is svm and I've got this response so I take this response object okay I just take this particular from the response this message object okay I take this message this particular entry right dictionary entry and what I do is that I just append it to the three existing messages over here okay so that is what I'm doing over here so here I've appended it right and I'm adding an additional query over here saying that you know what is the math behind svm so this is my next query okay so I'm again appending it over here right now if you were to look at uh you know messages right see this is how my messages looks like so the initial what is svm I got the response for that okay then I say role assistant right and then I uh this is from whatever the response which I got from chargpt right from the model then I add the following follow-up query role user content what is the math behind SVN okay this is my messages array of objects now okay so this one now I again call openair.chatcompletion Dot create I call this model GPT 3.5 turbo message is equal to messages okay now let's look at the response so this is the response which has been generated the math behind svm can be quite complex but here is a brief overview of how we assume works and it gives the text okay so if you have a front end you can make this API calls uh by appending the messages from the previous response and then you have this new response coming up from the chat CPT so you can actually simulate the chat GPT uh basically a chatbot not simulate basically create a chatbot okay so if you want to create a chatbot like this all you can do is that while true basically I'm creating a loop over here text is equal to input messages roll okay this is from user right so the role is user and content is this particular input text okay then I can say Messengers dot append this message okay so what is assumption over here I have already I have created this particular messages okay object okay where I have added these things role system role user content okay assuming that this is not created then the first step is to create a messages object with this role system content you are a helpful assistant okay and then you create this particular role user and you take the input text and you append that message to the messages object but since we have already had messages object over here I have not added that over here then send that messages object to openai.chatcompletion.create basically you are what you call Api call and print the response outside append this response continue in the loop let's see how it looks like right so here is the thing so it is asking for the input so because I was asking about svm I'll ask like what are some applications of svm in image processing okay actually running see it has retrieved basically it has generated this output and it says that for image classification we can use this this okay so let me change the question but what kind of features can be extracted from images some images for image classification okay so yeah so here is a question which I asked and here are the outputs now I'll ask like how can these features these features we fed into a SVN let's see what comes up okay it is running this response and then it says that choice of features to be extracted depends on the specific applications type of images classifier features extractor from images can be used as input to the algorithm so it is kind of continuing this right so can you show me code for image classification using SN let's see what's it's generated yeah so it basically shows the code over here right for a particular data set so you can actually continue like this basically you can create your chat words like this right and if you have access to say Google search API or you can say if you can pull out scrape results from websites you can add that as part of your response over here right and you can send it to the system to simulate something like a web chat GPT so lot of options are available now so here is a cost for chat GPT API basically so that it is dollar 0.002 for every 1K tokens so this is the cost for GPT 3.5 turbo so this was a short video on chat GPT API basically the chat API from open AI I hope you like this video if you like the video please like share subscribe to the channel I'll be sharing this particular collab notebook in the description of the video do check out see you in another video
Info
Channel: Rithesh Sreenivasan
Views: 5,310
Rating: undefined out of 5
Keywords:
Id: 3kVWye29DdU
Channel Id: undefined
Length: 9min 17sec (557 seconds)
Published: Thu Mar 02 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.