Chatbot with ChatGPT and NodeJS | ChatGPT NodeJS | OpenAI

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] [Music] thank you [Music] hello people this is selfie turds and let's learn that how we can create a chatbot using node.js and chat GPT so let's understand what we'll be learning in this video so there is a node.js script that we will be writing or you can say node.js application that will basically talk to the chat GPT server so we'll create a connection between the our node.js application and the chat GPT server and we will use the openai library so open AI provides library in different languages so we'll be using the npm module and through that NPR module we will be basically connecting to the chat GPT server and for that we'll need a open API key so basically this chat GPT server can be accessed by any developer using their apis and we need a open API key or you can say that this is the key that is used to basically connect between your applications and the charge GPT server first thing is what we need to create the API key in the open API platform so for that we need to open the website plan platform openai.com after that we need to use the open AI npm module and this is the open AI npm module and will basically use it to call some of the methods that has been exposed on this NVM module and basically we'll be giving some prompt We'll be asking some questions to our chat GPT server and it will give you response in your command line tool so let's see how we can create it so the first step is creating this key and for that we'll be opening the website platform Dot openai.com and there we'll go and create our key let's go to the website and we'll type platform openai.com and here you will be asked to do a login so I'll be I'll be using my Google authentication and I will use my account that is self-dets at the rate gmail.com and basically using it now I am inside this platform openai.com website so here if you see there is something called as API references so you can see that these are the different apis which can be used if I go to my account here here you can see this is view API keys so I have already created some of the apis API Keys like this one which was created on March 20 and then on March 24 so this is visible only for one time so once you create a new secret what happens that text is visible but after that you will not be able to see it if you are creating a new API key or if you want to create a new API key you need to click on this one create new secret key that will give you a long text and that you need not to show anyone otherwise your account can be hacked so basically you need to click on this button create new secret key and it will give you a new API key which you can use so this is the process how we create the API Keys now the second thing is what you need to use the open AI npm module let's go to the browser again and search for open Ai and BM and here is the open AI npm module so if you see here what does it say it says the open API node.js Library provide convenient access to the open a from node.js application so you can use this Library you can use npm install open Ai and this is basically a sample code that has been written here and this can be used to connect to the chat GPT server and basically get some response so let us create a new project in our webstorm and after that we'll be creating this particular code and connect to the chatgpt server I'll open my webstorm and here if you see I can create a new project so let's create a new project here I'll click on this new project and what I'm going to do I am going to you create a node.js project and I will choose my directory in this path which is here I'll be using the WSL which is installed on this window system and here I'll go to home and then finally I'll go to workspace here I've created a node.js folder and here I'll be creating a new directory and the new directory will be what chat bot using node.js and chat GPT so I'll hit enter and this is the project folder that I'll be using I'll click OK and this is the node interpreter so what I'm going to do I'm going to use my WSL interpreter so it is present here so this configuration is entirely based on your ID since I am using the WSL and windows that's why I need to do this node.js configuration so if you see I'm using the version 12.22 of node.js and finally I'll click on Create and this will open the application so let it come here I'll make it bigger and finally this is the project files which you can see so this is the terminal here and this is the package.json file so the first thing that we are going to install is the open AI module so I'll say npm install openai hyphen iPhone save so when I use this command hyphen iPhone save it will basically add the library into my package.json file so if you see that library has come here open AI 3.2.1 the second node module that I'll be using will be dot EnV so this dot EnV needed because we'll use this particular library to load our configurations which will be injected in the environment variable of our application so these are the two libraries that we are using let's go and create a new index.js file so I'll come here I'll create a index.js file so the sample code to connect to the open AI server or the charge GPT server is given here so we can do a copy paste from here and what I'm going to do I am going to just paste it so I'll do a control V or the shortcut that basically you are using so you can just copy and paste it here so what is the first line doing it is basically requiring the open AI module and it is using the configuration object and the open AI API then this is the configuration object and what we need to do we need to provide the API key which we have created in our previous step so it is reading from this processed on environment.open API variable and we need to load our environment variable that can be done how we need to use the command require and then dot EnV and then we need to use config so this is the process by which you can load the dot environment file and what we need to do is we need to create our DOT environment file so I'll go here new file and what we are going to say we are going to use dot EnV as the file name configuration key that we are using is this one so I'll just copy it come here I'll do a paste and I will paste my own API key so I'll be inserting my API key here you need to insert your own API key that you created in the last step this is the configuration object and finally in the new open API object or class you can say we are passing this configuration object and this is returning you a instance of the open a API open AI API Library so now you can call these methods so if we see more method so we can say open a i Dot and you can see these are the different methods that are available here so one of the method is create completion which basically talks to the openai server and basically processes The Prompt that you provide and returns you the response so let's delete this one and this create completion method takes some of the object the first is the model so as you know that all the artificial intelligence things are modeled based on some data so this is the name given to that model and there are multiple models that are available but we'll be using this text DaVinci 003 so in artificial intelligence field the more data you provide to the modeling tool the more better result you get so this is one of the model there can be n number of model which you can use so different company creates their own model they use supercomputer to read data read millions of book or you can say all the available articles which are present on the internet and they create their model so this is like text DaVinci z003 is the model next is what next is the prompt so prompt we can say that it will be in the text format prompt is like what you are asking to the artificial intelligence server so it is a prompt like you will be asking it in the text format and finally you will be getting response in the text format the third part is the max tokens so max tokens is used for getting the length means how much length your response will be so if it is of less token you are providing then you will not get the full response so don't go into the depth of these things let's understand how basically we can connect and get the response so for the max tokens what we can use we can use 3000 or any value whatever you want so I am giving you 3000 as the value and then finally this is completion Dot so if you print the completion text or the completion variable so you will get a response which will contain completion and then data and then choices will see the response will print the whole completion part and then finally the text so this is the code that we copied from our npm module but we want to create a method out of it so what I'll do I'll say async and then function and the name of the function will be call chat GPT and then this will take some text okay so we'll provide some text here inside the call GPT method we will do try and catch so basically we are doing this try and catch why let's say we call the chat GPT server and there is some exception so we need to write that exception so that we can see the error and then fix the issues we can just console and then log the exception and finally here what we are going to do we are going to move this part okay so let's move this whole part so I'm do I will cut it from here and then I'll move it here now what we can do we can just call this method so we can say call GPT and we can say what is redis let's say this is the question that we want to ask let us see the code again so everything is fine okay so we need to provide this as text and this also as a integer so this should not be a text so our code is completed let's call or let's run this file so it says node index.js and let's see what is the output so it is saying what is redis okay and this is giving us the answer redis is an open source in memory data structure stored so this is a very beautiful example that you have created your own node.js file you don't need to go to the chat GPT website and basically call or give some prompt and it will give you the response let's change it so let's say uh when India got independence okay let's see what is the output in this one so I'll again run the file node index.js and it is saying India gained independence from British rule on August 15 1947 the date is now celebrated as India's independence day so this is a perfect or this is the correct answer that has been given by chat GPT so you can see like we have used this open AI library and based on the configuration key or the open API key that we have provided and then calling the create completion thing using the different models we are using text DaVinci 003 model and we are giving some of some text and it is giving you the response so you can create get better examples like you can accept these texts from the command line itself and then call the chat GPT server so you can improve this code more whatever you want so this is how basically we use the openai library and connect it to the chat GPT server to get to get the response so hope you like this video hope you like my Channel Please Subscribe my channel and please share these videos with other people thank you
Info
Channel: CodeWithRajRanjan
Views: 5,243
Rating: undefined out of 5
Keywords: chatgpt, chatgpt nodejs, create app with chatgpt and nodejs, nodejs, nodejs chatgpt
Id: 18O2BTG7gnE
Channel Id: undefined
Length: 12min 16sec (736 seconds)
Published: Wed Apr 05 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.