How to use OpenAI API with Node JS Javascript tutorial | OpenAI Latest version | ChatGPT response

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone in this video we will be using openAI  API with nodejs and also sending request from our   Postman so let's get started so first let me  go and create the nodejs code and later I will   be showing you how you can access the open API  with creating a key so first thing just go to   your favorite editor I am using visual studio code  over here and just create a new folder and inside   that folder we will be having our main.js alright  and after that just go to terminal click on new   terminal here we will be requiring two packages  one is going to be Express because we are going   to have a get call over here that is obvious and  another one is going to be the openai package   itself so it's going to be npmi space Express  space openai hit enter yeah so both the packages   are installed so if I go to package.json I'll be  having Express and open here this is good so now   let's just import both the packages inside our  file so it's going to be const Express equal to require Express and here you can see  this is the latest version of openai   I think if you're using older code if you  watch some older tutorials they may have   openai 3 point something so the code is  completely changed in openi version 4 so   you need to like use the latest code over here  and next is going to be const openAI equal to require openai and next is going  to be the app thing const app equal to express the standard stuff that we use app.  use express.json because I'm going to send data   from Postman in the form of Json so we need to  use for here express. Json also yeah so now we   are good with this let's go and add the code  where we will be including our open AI key so   it's going to be const open AI equal to new this  particular variable that we have used it's going   to be open Ai and within circular bracket and  a curly bracket here it's going to be API key   and inside this we need to specify our open API  key and make sure that you specify this in the   right way it's camel case K should be Capital  so now let's see how we can get our open AI API   key so first thing for that just go to Google and  type open AI so you will be needing a chat GPT or   openai account already created so click this just  click on login I have already created an account   if you don't have an account you to just create  one so you'll be getting three options over here   chat GPT dall E and API we need API just click  that and if you go at the top personal here you'll   be having view API Keys just select that and  here you will be having option to create a new   secret API key and this is super important never  expose this key anywhere and never use this key   in the front end always have a separate server  nodejs code and use it there and don't give to   anyone I'll just click on create secret key and  it will ask you to verify if you are a human or   not just complete this puzzle yeah so once we done  solving that puzzle we'll be having verification   successful and this is going to be our secret  key so I'll just copy this and we need to just   click on done and in case if you are getting any  errors when using this key you need to go to this   usage thing okay and here it will show you what  percentage of your openai API credit that you   have used so there are two ways one is the free  trial that open a API gives us so here you can   see right I'm having a $5 credit granted and it  expires by December 1 and make sure that you check   the expiry date for your open API account okay  so now we are good we have just copied the key   now let's go to our code and we are just going  to paste it over here so you need to be careful   with the key okay now we done now let's go and  create our get function so this is going to be app.get and it's going to be the path it takes two  parameters one is a path and another one is going   to be a call back function so it's going to be get  response I will have something like this and it's   going to be the call back request comma response  and we'll be having an arrow function over here   and first let me show you without getting the  prompt from the postman and later I'll be showing   you how to add a Json there okay so let's just  directly create a chat completion that's what   it's called in open technical term chat completion  which gives us a response from openAI API so for   that we need to have a variable it's going to  be const I'll have it as response equal to this   is going to be a because we are making an API  call it's going to be await open AI which is   this variable over here you need to be careful  with the variable this variable corresponds to   this one this variable corresponds to this one  dot chat dot we'll be having completions and Dot   create and we to open circular bracket and another  curly bracket okay so I'm continuing over here   and inside this we need to specify the model so  I'll just put model over here so by default the   good model many of them are using is GPT hyphen  3.5 hyphen turbo okay this is a good model that   you can use I'll just put a comma over here and  next one is going to be the message that we are   going to send to openAI API so it's going to be  message and here we will be specifying our prompt   and before the prompt we need to specify the role  okay we are a user so we need to specify the role   as user so it's going to be role as a key and  value pair it's going to be user and I'll just   put a comma next is going to be the content which  is going to be the text prompt that we are going   to send okay content and this is also going to be  a key and value pair and this is going to be our   text prompt that we are going to send to openai  API and generate an AI response so I'll just give   here Essay on global warming okay something like  that you can give whatever you want I will just   give that and next one is going to be the maximum  tokens I really like this parameter okay Max   tokens let me give it as 100 so what Max tokens  means is that whenever we use this openai API key   the usage is calculated based upon the tokens so  here you can see right one token is approximately   four characters or 0.75 words so in our case I  have specified 100 tokens so we will be getting   75 words in our response if we don't give this we  will be getting an entire response and if we get   an entire response the number of tokens that we  use increases and our usage also increases here   and it will cost us more okay so in case if you  are giving some trail purposes you need to just   restrict it and in case if you're going for a  production you can just remove this this is not   all needed in production code (depends on the  use case) only for testing purposes and if you   want you can also use that it's totally up to you  so now we are done with this and it's throwing an   error for await because we need to have a Async  function so just give here async yeah so now we   are good so now let's just go and console it  okay just go here and have a console dot log   I'll just have it as response because that is  the thing that we're getting from the API and   this is good and final thing we need to make our  server listen so it's going to be app dot listen   and we need to specify the port number I will go  with 3,000 and another call back function let's console.log server started yeah this is fine  and now if we hit this path we will be getting   an essay on global warming okay from the postman  so I think we are good now let me just now save   it and for starting the server we need to use the  node it's going to be our file name yeah server   started so we are good now and let me just go to  here and quickly get the Local Host path yeah just   copy this one so I'll just come to my Postman  and if you click on this plus icon you'll be   getting a new workspace just paste it so it's  going to be HTTP colon // localhost colon your   port number that you have specified over here  okay so that needs to be same since I'm using   3,000 over here I'm specifying 3,000 here also  and after this we need to specify our path which   is going to be slash get response and make sure  that you're using HTTP if you put https you may   get some errors and our path so now if I send it  we will be basically getting the console here as a   response so if I just send it and here we won't be  getting anything and okay we are getting messages   is a required property and this is not message  it is messages just change it inside the response   thing so I'll just change it and save it and  just rerun it again and this time I will send it again so here you can see I haven't got any errors  and this is the message okay this message is the   thing that we want okay this is where there  the response from our open AI is present so   we need to access this particular thing okay  so the whole Json is response so we need to go   inside choices and inside choices we need to  access the element of this list because this   is a list okay this is a list and this Json  present inside this list is present at the   zeroth index so it's going to be something  like this response is this whole thing and   we need to get choices at first so it's going  to be response dot choices and inside choices   since it is a list we need to access the  zeroth index which is this particular Json   so it's going to be choices of zero index and  inside the zeroth index we need to access this   message so it's going to be Zero Dot message  okay so this is how we need to like access   you know go deep and access that particular  key and its value so I will just now save it   and stop it you can use nodemon you know if you  want server started and if I just click on send again so here you can see this is the content  that I'm getting so here you can see right it's   saying role as assistant which means it's AI  which is generating this particular thing and   here you can see this is my prompt essay on  global warming and I'm getting the response   and if you want to access this particular  content directly you can just have here dot   content that's it so now we are good we are  good we are going good now let's see how we   can send this particular text from Postman okay  so for that it's going to be so simple we will   be just having here body and now by default  we will be having none and another important   thing you need to have here get okay don't put  here post or put something because here we are   using get function okay app.get it's a get call  so if you use some other operation over here we   will be obviously getting an error so now we need  to go to raw and here we'll be having text please   change that to Json and inside this put a curly  bracket double quotes let's just have a key name   as user prompt in camel case and here we will  be having our text so I'll just go with essay   on Earth something like that some other prompt so  in case if you're have going to have a front end   or something this is the place where you will be  like you know sending the data just imagine you   have a UI so this is how you need to send Json  okay so this is good so now we need to find a   way how to access this particular prompt so for  that just go here const I just have it as user   prompt and it's going to be request this variable  dot body do we have to specify the key name that   we have used over here so I have given it as  user prompt just copy this and I'll just paste   it okay and let's see if we are getting this  user prompt now so just have here a console console.log I'll just go with user prompt yeah so  I'll just save it and rerun it again and just send   it so here you can see I'm having essay on earth  so now let's see how we can integrate this so let   me just remove this console and we need to just  replace this particular text inside our messages   key and content key with our variable okay we need  to just replace this rather than hard coding it   we will be sending it from Postman or in case if  you have a front end you can send it from there   so I will just save it and we will be directly  getting the content why are we like doing this   we can just have here dot content yeah we are  now accessing this value using this key okay we   just save it and yeah clear this start it again  and I will just send it so this time we will be   directly getting the essay you see right because  we are directly accessing the content which is   present inside the response but in our post  man we will be still having sending request   because we aren't sending back anything right  we aren't acknowledging that this particular   call is complete to postman so for that what  we can do is we can just have here response.   send let me just now copy this I will send this  as a response paste it saving it stopping it and   rerun it again and this time I will go with some  other prompt Essay on openai itself yeah so I'll   just click on send and here you can see this is  what I get openai is an artificial intelligence   thing blah blah blah and if I go to my post  man and if I go to this response thing body   here you can see I'm having the response here  also and this is like not completed because we   are having this constraint Max tokens as 100  if you want to get the full response you can   just you know remove this or you can increase the  max tokens that's totally up to your choice and   we are getting the response in Postman because  of this line so this is basically how you use   openai's API and integrate with node js and get  a response in Postman so I hope you would have   found this video useful do check the playlist of  my channel I've done a lot of other programming   videos and CRUD operations also many other AI  videos check them out sub me thanks for watching
Info
Channel: United Top Tech
Views: 4,708
Rating: undefined out of 5
Keywords: openai api node js, openai api, openai tutorial, openai chatbot gpt, openai api tutorial, openai api key, openai api tutorial javascript, openai api project, openai api integration, openai api nodejs example, how to use openai api nodejs
Id: clwoDeSn48c
Channel Id: undefined
Length: 16min 30sec (990 seconds)
Published: Sat Sep 09 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.