Introduction to the ChatGPT API with Postman

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
since jgpt was launched at the end of 2022 nothing is the same anymore now open AI the company behind chair GPT has released an API that allows anyone to integrate the AI model behind chegbt in their own applications if you have been using chat GPT so far you probably have used this interface which is fine I guess but the API enables you to do is to use the technology behind jgbt in a programmatic way in this tutorial we will cover three important aspects about working with the chat GPT API we will Begin by creating an API key which is needed to use the chat gbt API next we'll make a simple request against the API to get a list of all available open AI models and last but not least we will build a request against the chat GPT API at the end we'll also go briefly into the pricing model but let's not get carried away about pricing rack now currently when you sign up for an account with openai you get five dollars in credit valid for three months so that is just enough to get started throughout this tutorial I'll be using Postman as it makes creating requests and debugging them much easier I highly recommend using Postman or a similar tool to build this request before you actually start writing code you will find in the video description useful links as well as a document with step-by-step instructions for everything I'm showing in this tutorial if this sounds like what you're looking for let's dig in the first thing we need to do is to go to the address platform.openai.com and log in with your account after you have logged in you should see a page that is very similar to this one and here on the top corner you will see your name initial and you'll see personal if you click on this one you can go here to view API keys and this is where we can generate the API key which we will need to access the API initially you don't have any API keys so simply go ahead and click on create new secret key and keep this window open for a while you may need this in case you're losing the API key so I'm going to go ahead and copy this and the next step is to jump directly into Postman and create a new collection where I'm gonna store everything that I'm doing with the openai API let's call this collection open AI and inside the collection I'm gonna go here to the variables tab and what I want to do is to save this API key here so in the current value I'm going to put this API key in the initial value I'm going to write fix me and I'm going to name this API key don't forget to save this and the next step is to do a simple request against this API for that we're going to open up the API documentation and if we scroll further down we're gonna see one point in the API reference which says models so this endpoint will give us a list of all available models and this is a simple request that we can do and should do to ensure that our API key is correctly configured in Postman so I'm going to go ahead and copy this address and paste it here and if we go ahead and try this request what we'll see here is that we're getting this error for one unauthorized and there's also this additional information in the response body telling us what we need to do in order to get this key so essentially what we need to do is to provide an authorization header now because we have a collection all we have to do is go inside this collection and we'll see here under off the type is currently selected to no off and what we want to do actually is select the Builder token and here in the token value what we want to write is Curly braces gonna be two of them and as soon as we start typing this we should see here API key we hover over this we're gonna see the current value of our API key let's go ahead and save this inside a collection The Next Step would be to also save this request inside the collection so all we have to do is click here on Save I'm going to select here the collection open AI and the request name would be get models so now this is our collection this is our request let's give it another try and what we're getting back is actually 200 okay and we want to make sure that the model that we want to use is actually available in this list so I'm going to go ahead and search for it and the name of the model should be something with GPT and there is this first one which is a specific model version but this can change in the future actually I want to get a stable model version so let's search for this entire string and this is the model that we actually want to use this is also the model that's powering check gbt when you're using the regular web interface all right so now we have this initial request that is working so we're making incredible progress we need to jump back at the API documentation and figure out how we can use the chat GPT API and this is a bit hidden here it's available inside chat and this is the end point that we actually need to call so let's do a few things I'm going to go ahead and configure this manually so first of all I'm going to copy this endpoint address because this is where we need to send our requests and jumping back in Postman we're going to open up a new tab and let's select here post because this is a post request it's super important that you don't forget this and then I'm going to go ahead and paste the endpoint and let's go ahead and save this directly inside the collection so we're going to call this chat GPT API as it is right now this will not work so if we try to submit it we're going to get back an error we must provide the model parameter so in order to do that we actually need to go here to the body then we need to select Raw super important that we also select instead of text Json and what we need here to provide is a valid Json so I'm going to start here valid Json so we obviously need the model property and we're going to specify this based on the model that we want to use so the name of the model is this one the one we have searched here previously so I'm going to go ahead and copy that and paste it inside here so let's go ahead and send this we're no longer getting the previous error about the model we're getting an error about the messages so in order to understand what we need to do we're going to jump back to the API documentation we're going to see here that there is an example for the messages so I'm going to Simply copy this string that we see here it's actually an array go back to postman comma and as a new property we're gonna write here messages and we are going to provide this currently it's not so well formatted so we're going to click on this beautify just to make this a bit easier to read so this should be enough to get our initial request going so what we'll see here is status 200 okay indicates that everything is working well and apart from this we're gonna see exactly where this response is so it is actually in this response object has a property choices which is an array and then we're gonna get here a message we'll see the role assistant and then the content so this is essentially the first call that you can make to this API it's super basic but if you manage to do this so far you're doing incredible progress if you're getting value from this tutorial help me create more like this one by giving this video a thumbs up and subscribing to my channel now let's go back to this and figure out how we can make this actually useful so in order to make this a bit more useful we actually need to configure our requests better currently the way it is is not super useful so for example let's imagine that we have a vehicle repair shop and we want to provide some useful information if someone is actually using our chat bot for example so in order to do that we need to configure a few messages and there's actually a bit more information in the user documentation so if you go here under guides to chat completion we'll understand that in order to make this work there are actually some roles and some messages that we need to Define before we actually are getting started with this so let me explain a bit what is the idea with these roles and how we can use this in our example essentially whenever we're starting we should start with a system so this is giving a bit of context to the AI in terms of what we are trying to do so we can write something a bit more useful for example your name is Val you are an assistant for a vehicle repair shop when answering try to be funny and relaxed so in the system we're providing a bit more context so for example we can also go and now Define essentially what the input will be so this will be again with the user role so we have to copy this entire object that we have here and provide it on the next line so the role will be user and we can ask a simple question like I what is your name so let's go ahead and try this out and what we're getting back is actually what we expected hello there my name is Val like vavav room but without the room so essentially it's also trying to be funny it understands that they are an agent for this vehicle repair shop and asking for follow-up questions like how can you assist us so for example maybe the user wants to know how to reach the service department but this information is something that the model does not have so to provide a form of training we need to provide essentially already existing conversations that we need to imagine so let's say for example the conversation that we are going to generate here is related to the phone numbers so I'm gonna write here a question like what phone numbers can I reach you and then what we'll do next is actually to provide a response from the assistant so this will be the assistant and the assistant will provide here an answer for example it can go a bit more into details and provide here some training information like what is our main phone number what is the phone number of the service department of the parts department and so on so you can have here additional information about this so then when a user comes and we're actually trying to use this on a real application and ask something related to phone numbers for example let's say how can I reach the service department this is something that we can put inside here as a question and we expect the AI to be able to understand which is the phone number for the service department if you go and take a look here in the response what you will see here okay we should see here this is the number that we expect and of course the assistant is again trying to be funny because this is what is in the original system prompt so essentially you're providing some training data with your request or if you have a conversation history that also needs to be part of this entire request so it works a bit differently because there's no State between this request when you're sending a request to the chat GPT API the chatgpt API has no knowledge in terms of what you have sent previously so it essentially starts from scratch this is why you need to use the request to provide the entire message history if that message history is important or of course you can make it a bit shorter and create only the specific messages that are important but as a minimum you should specify here the system this gives the general context and after that you're essentially training check GPT in terms of what kind of information it should give if there's something that's related to your application and not something that chatgpt already knows before we conclude this let's also talk about pricing for a second so obviously using this API is not free and when you have registered for this account you are getting essentially a free trial for a limited period of time with limited budget if you have a bit an older account you probably have like 18 if you have just registered for open AI right now you're probably getting like five dollars but this is still enough because chat GPT is really really cheap so it's currently priced at essentially 0.2 cents per 1000 tokens and just to give you an idea like what are these tokens before going back to one of our requests and we're taking a look here at the response we're gonna see how many tokens we have used so in order to generate this response we have used 169 Tools in total so it is essentially very very cheap it's a very affordable model and for that reason probably it's adoption it's gonna be absolutely huge but once this trial period is done of course you will need to sign up for a paid account you need to provide your credit card details but my understanding is that you will be charged only based on usage which is actually a very fair pricing model all right so this is all for today I hope you got value from this tutorial and that you've managed to get started with the chat GPT API in Postman if you have any questions please leave them in the comment box below I would love to hear back from you if you have any ideas for tutorials and what are you trying to build with chat GPT apart from this I would really really appreciate if you would give this video a thumbs up subscribe to my channel it will help us create more tutorials like this one I'll see you next time until then take care and bye bye
Info
Channel: Valentin Despa
Views: 7,670
Rating: undefined out of 5
Keywords: chatgpt, openai, chatgpt api, chatgptapi, postman, openai api, chatgpt api examples
Id: rlLwSr-wIAg
Channel Id: undefined
Length: 14min 6sec (846 seconds)
Published: Thu Mar 09 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.