Getting Started With OpenAI ChatGPT (GPT-3.5) API In Python | Tutorial For Beginners

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey housing guys right so open AI finally released check gbt model today called gbt 3.5 turbo and I'm super excited to be honest so unlike the regular DaVinci model that was like a search engine you provide input and the model respond with an output the charge gbt model was more like a composition and which I'll show you in a second for this video I'm going to show you how to get started with working with open air GPT API in Python now before I dive into the tutorial I want to cover the pricing first and that's going to be one of the important aspect when it comes to using a service so the chat gbt model is going to be like I said before it's going to be this gbt 3.5 turbo model and this model causes 0.002 cents per thousand tokens which is a lot cheaper than the DaVinci model from the 3.0 model that causes two cents per thousand tokens so unlike the 3.0 gbt models in which you can set a token limit how much tokens per API can consume with the gbt 3.5 turbo model basically this API doesn't have a total limit so ever the uh API returns is going to be whether the tokens that is going to use so for example let's say if I provide a prompt and based on the problem the API returns let's say 10 000 characters and based on that API code it's going to cost you the 10 000 tokens usage and cannot set a limit cap unfortunately and I think that's one of the reason why open AI decided to make the model a lot cheaper than the 3.0 gbt models now the next I want to go over is the model itself right so if we go into the documentation in which I'll link the link in the description below and here's the newly edited the gbt 3.5 model um this is a model list if you look at the description so the gbt 3.5 model is a cell models that improve on gbt3 and can understand as well as generate natural language or code if I click into the gbt 3.5 model now foundational gbt 3.5 table right here now listen right here we have this gbt 3.5 turbo-0301 so this model right here uses the training data up to March 1st 2023 so if you're interested in getting the latest update then case distance 0301 gbt 3.5 turbo model and just keep in mind that this model is going to expire on June 1st 2023. and for demonstration purpose I'll be using just a regular gbt 3.5 turbo model now with that being said let's get started alright so first I want to sign for an open AI account by going to openair.com then click on menu then click on the sign on so make sure that your sample account and as a new user you get 18 dollars of credit to use but the credit is going to expire after three months so just keep that in mind account let me log into my account first now once it's time for an account you want to click on your username then click on view API keys now if you want to create a new set of SQL key and that's basically your API key now Michigan you are save the API key somewhere safe but for demonstration purpose I'm going to save the API key in my python script all right so here my uh demo.pi file I'm going to create a variable called API key and I'll paste the open AI API key here now we need to install the open AI python package before we can connect to the endpoint or use the API all right so here we launch my terminal and to install the open AI python package it's really easy you simply type pip install open AI enter and once you install the Open Eye python package in your python skill want to import the open AI python package now the first one to do here is want to attach the API key to the open air module so here I'm going to type open AI dot API key and I'll attach the API key next I'm going to create a variable to specify the model ID and it's going to be gbt Dash 3.5 Dash Turbo so as I mentioned before chair gbt API Works a little bit differently you found the regular gbt 3.0 models and the reason being is because when it comes to working with a check gbt API well kind of like having a conversation basically here I'm going to insert a function and now name the function chat gbt to underscore composition you know name the parameter composition now if we look at the python code make a decision gbt composition function so basically we'll reference the open AI Library then we'll use the chat completion model nkl supplied the model ID which is the gvt 3.5 turbo model then we have to send messages printer so basically we have a conversation log and we're going to pass the composition log to this function to keep adding the composition records and from this API code I'm going to name the outputs response for now actually let me do this I'm going to copy this code block here foreign then you can see how the API works right so here I'm going to create list and I'll name the list conversation so when you're working with chair gbt API you are going to provide a role in the accountant and content is basically your initial question or initial prompt and release is going to be used to determine uh if the input is coming from a user or from the system itself and found the API we have three different rows system and user so user Road will be yourself basically the person who is typing the input and system row is going to trigger the composition so whenever you start a new session you always starts with the system row and when we are going to pass the message we always need to pass the message as a list that's like an example so here I'm going to run these two lines to create the composition list and we input the open air module in our initial the function swap all right so let's go back to this create method now if we look at the composition record right now I said this inside this list I have one record in this record which is additionally we have a row in the content and that's basically my prompt now if I go in around the query method to make the API call all right so here you think that the model 26 is take a look now go back to the create method now here if I run this uh create function now if I print the response option right so here we have the API created date the model ID and I guess right now uh gbt 3.5 is going to use the 0301 model by default because I remember I set the mod ID as the regular gbt 3.5 turbo model and usage is going to be the total tokens that you have consumed so if you're using gbt 3.0 model the API is going to set a token element to 16 tokens from the completion token usage but right now with the gbt 3.5 turbo model there's no uh token usage limit actually there is so the maximum usage limit is going to be 4096 tokens so if your request exceed the 4096 tokens usage in that case it's going to tell you that found the finished reason it's going to return uh continue for the Finish reason and that means that this additional content that needs to be retrieved long to focus on the most important part from this response option and which is this Choice key I mean let me print the choice key from the print statement and it's going to be choices not choice now if we look at the response in suggestless we have the message index the message content which is going to be the response found your prompt in here the rule updated to assistant meaning that you are in a conversation with Dai and just one very important concept when you run this query method you are only going to get one message record back and this is very important thing to remember so I like the regular gbt 3.0 models the API can return multiple responses with the chat gbt model you can only return one message or one response at a time now to retrieve the row and the content so from the choices response we can always reference the last element and which is going to be a minus one and we can reference the message attribute the rule and this will be Mrs star content and I'll go ahead and runs two lines and here's the assistant role followed by the message itself so to check the composition so basically uh this is what I did all right so first we're going to clear this and it's going to be the compensation tracker then we're going to initialize the composition by setting the role as system followed by the question that I want to ask check gbt now to continue the composition I'm going to code the chat gbt composition function and I'll pass the composition list now inside the church gbt compensation function I'm printing the message index bench reason and the total token usage I don't want to append on those things to the composition this I want to keep the computation list as clean as possible so this line here is going to a pin the message director to the composition argument and here I can update the composition list to make sure that I have the last record opinion to the conversation list all right so let me take a look right so the composition looks right now is empty I'm going to run these two lines to create my initial pump then I'm going to run the chair gbt composition function so this prompt used 195 tokens and stop means that the API has successfully written all the content into this API call now if I print this print statement which is going to print the row and the last compensation record in history content returned from the API code which is going to be the content response now to follow on this composition so basically we want to appear another question to this conversation list so he'll need some Michael so let's say I want to follow on the second item from the response so basically a pin the role as user meaning that this problem is coming from a user and for the content I'm going to say can you be more specific on item number two I'm going to append this message to the conversation list and I'll call the chat gbt composition function again and if I want to retrieve the response so answer the same print statement so that's going to retrieve the last record from the conversation list and I want to achieve the row and the counting response now if we look at the response so basically my follow-up question was can you be more specific on item number two and found the output here so we can see the chair gbt API is able to provide additional information based on the follow-up question that I ask if I want to read to the composition reactors so basically we know it's a list so we can insert a loop so we can say for let's do for record in conversation and I'll simply print uh row and content announce the empty line between each message record okay so if I print this Loop she uh that's not good because some of the response have empty lines if we look at the Loop response so basically I'm printing the entire composition into a single output right here so once I forgot to cover words how should we modify the code to keep the composition running like on chat gbt and to modify the code so here I'm going to insert my code snippet to replace everything all right so basically I'm going to uh start the session with the system row and for the content it's going to be how may I help you basically it is going to start engaging the composition from the beginning then we're going to insert the while to Loop to keep the process or the conversation running Non-Stop an input is going to be the input pump so basically going to type your questions or your pumps into this input statement then it's going to save the string into this problem variable then we're going to appear in the prompt to the content key and this is going to be your message and we set the road as user then we run the charge gbt compensation function to send our requests and once we get those ones back we're going to append the message to the composition list I'm going to terminate this session and I'm going to press F5 to run the script all right so here I forgot to update the API key all right so let me try again I'm going to pass F5 now here's going to be the Python program that you are going to work with let me take out the print statement because when you are building a program uh this should be go to the bottom but I'm going to take out the print statements for now all right let me try again all right so here we go now assuming that this is a command line tool and with this program we're going to start with I am an AI language model and I can help you with a variety of tasks such as answering questions providing information and generating text making recommendations playing games and and so on I want to know uh who won the NBA final in 1992. and it's going to return Chicago Bulls then we can continue with the prompts and I can ask a follow-up question what was the final score in game six and because all conversations are saving a list so check gbt API is able to go back to the previous records and reference my previous question right so this is going to be if I'm going to share in this video and feel free to post a question or feedback in the comment section below and if you find this video useful please don't forget to give this video a like and click on the Subscribe button and I'll see you guys next time bye
Info
Channel: Jie Jenn
Views: 18,011
Rating: undefined out of 5
Keywords: chatgpt, openai, gpt, python, ai, gpt 3.5, gpt-3.5-turbo
Id: I4T--ycOpi0
Channel Id: undefined
Length: 18min 9sec (1089 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.