Getting Started With OpenAI GPT-4 API (ChatGPT Official API) In Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey house and guys in this video we're going to learn how to use check GT4 API in Python all right so to take out the invitation to access your gpt4 API and that's how we cover how to see API real quick in Python all right so let's look at the documentation real quick so for the third GPT API it's based on the gbt 4 model now if we look at the models here so for the church GPT for API which is going to be the gbt4 model we have four different versions we have the gbt4 model which is going to be identical to the gbt 3.5 model except that we'll be able to do a lot more complex testes using the gbt4 model now beside the regular standard gbt4 model we have gpt403 14 model which is a snapshot of gpt4 model from March 14 2026 and with this model the model is not going to get updated it is going to be purely on just a snapshot then we have the 32k and the city 2K 0314 model so for the gbt432k model and the gbt4 model the only difference is the token limit so the regular gbt4 model can only accept up to 8192 tokens first the city 2K model which can accept up to 32 768 tokens which is almost four times more than the regular gbt4 model now in terms of the usage and it's going to be identical as I mentioned before it's going to be identical to the gbt 3.5 model now let's try out the gbt4 model in Python alright so first we need to generate an API key now if you don't have an account simply go to platform.openai.com and simply send for account and filed instruction all right so here I'm going to basically reopen the page now on the home page you want to click on your username then click on view API keys now here I'm going to create a new set of API key alright so I need to delete one of the keys now for demonstration purpose I'm going to copy the API key to my python script all right so I'll name the variable API key next we need to install the open AI python package now open your terminal all right so here I'm going to create a new session by terminal and to install the open AI python package we'll type the command pip install open AI enter all right so going back to the python script here I'm going to import the open AI module they need to attach the API key so here I'm going to type open AI dot API key then we're going to assign the API key now going back to the documentation all right so here I'm going to use the gbt for 32k model so I'm going to create a variable code model ID and I'll send the model ID all right so there's going to be everything we need for now all right so here I'm going to create function and I'm name the function check gbt conversation and this function will have a parameter called conversation now inside the function I'm going to make API code to open ai's endpoint all right so here I'm going to type open AI to reference the open air module then we want to use the check completion model dot create now for the output I'm going to name this response now inside the create method we need to specify the model that I want to use to the model parameter right so it's going to be model ID next we need to pass this that contains the composition log so the parameters called messages and here I haven't created a composition log list here but I know the list is going to be named as compensation from the composition argument then we're going to return the response option all right so here I'm going to create this I'll name the list conversations let me rename this to composition log all right so the way how gbt API is able to recode the compositions by accessing the compositions list to look at a previous conversation logs now only send a command to check gbt API endpoint we need to provide two things row and content so the row ID is being used to identify either conversation belongs to the AI or belongs to the user and there are three different rows system user or assistance so when you make an API call so most likely you will either use system or the user row but the system rules are optional I'm just going to be used to trigger the first conversation pump all right so for example so here I'm going to make API code to uh gbt for API so I'm going to assign the user row to uh this uh dictionary object and this basically means that me as a user I want to send a requests to check gbd4 API now for the content which is going to be the pump I can basically ask anything so I can say tell me a joke you know it pinned the dictionary option to the composition list all right so if I print the compositions list let me go ahead and run this code block now if you look at the list that belongs to compositions so basically uh this is going to be log number one so I'll go and choose send this list and check gbt API is going to look at the last item from the list to determine what the intention is going to be and here we go in the uh run this code block to attach the API key and also to create the chat gbt composition function all right so I'm going to make an API code using the chat gbt conversation function so basically when we make an API call by passing the conversations list the check completion.create function is going to appear in the composition reactor inside the response option and which I'll show you in a second now here if I run line 17. also here forgot to update my here let's do this I know why I'm getting real I've got to uh upgrade my open AI python package all right so I'm going to type purple stuff Dash uppercase U open AI so that's the shortcut to update the python Library all right so let me uh starve again so go ahead and create a function and now it has the API key now here I'm going to create the conversations list developing the first compensation record now this time if I go in the run the function now it's giving me uh here let's see me change the model ID all right so let me try again so this time I'm able to make the API code successfully all right so let me look at the documentation I may have to look into it after this video now if we look at the conversations list and here's the output so we have this open air option that contains the response so Creator is going to be the timestamp that you make the API code and the research the how much token this API code is going to cost you now if you want to access the response the gpt4 response is going to be inside this choices key and here I see I made a mistake so I don't think this is going to append the conversation log through the composition log argument right so what we need to do here is we can here so we can reference the composition lock which is contains the latest composition records all right so I'll basically append the record from the response option to the composition log argument all right so here if I just do this so this should be composition log all right so anyway um I know this compositions uh this is going to contains this open-air object option now here I can reference the choices key and that'll give us a list now inside the list the response is always going to be the first record so I can reference the first element and I know the response is going to come in from this message key second type message followed by Counting and that'll give us the response now here I'm going to do here is I'm going to take the response option that choices and it's going to be the first element that message that comes in I removed all the empty spaces on both in all right so here let me try again so I'm going to run this code block if I print the composition list and as you expect to rackers right so here we got one cent so this should be additionary all right so let me do this let me come out this line here I want to make the API call again oh so this should be kill me all right so let me recreate the compositions list and actually I forgot to recreate the function all right so I apologize for the mistake so let's take a look all right so going back to the uh choices key inside the message we have content and the row all right so we have got to append the road to the inside the gbt composition function all right so this is going to be rope and this will be coming from messages da bro then we have the accountant right so that looks pretty good to me now to convert this code snipper into a workable code to look like uh chair gbt all right so here we need to insert while loop now inside the while loop let's say I want to create a command line tool the first thing I want to provide is being able to allow users to provide their inputs all right so I'll name the inputs pump and it's going to be user and once the user provides their input so we can append the input to the compositions list and here I'm going to change this to how may I help you now change the road to system so basically this is going to service a trigger to start the composition now I'll say you have a very specific use case now let's say you're creating a resume or cover letter assistance then you will modify the pump to be something like I'm a resume helper how may I help you and based on this pump here charge gbt API will be able to identify the purpose that you're trying to do but for now I'm going to make this a little bit more General so I'm going to say how may I help you now going back to the while loop so here I'm going to append the record based on the pump here so the row is going to be user followed by content then can code the function and you can simply update the compositions list and this would be compositions now for Simplicity I'm going to basically uh print the composition list all right so here let me save the python script and I'm going to rerun the script all right so here this will be a system all right so let's take a look and you have a this print statement Shadow me all right so let me try again so here we can provide our requests so I can say to me a joke now if we look at this output right here so the first record is going to be the initial uh pump Ed by the response from the church gbt API they were sending our request to the GPT API followed by the response from the API so that's going to be able so I'm going to share on how to use chat gbt for API and hope you guys find this video useful and if you enjoyed this video 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: 3,912
Rating: undefined out of 5
Keywords: chatgpt, openai, gpt, python, ai, gpt-4
Id: fzbOejjIisg
Channel Id: undefined
Length: 16min 43sec (1003 seconds)
Published: Wed Mar 29 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.