How To Use ChatGPT With JavaScript (NodeJS & Express)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
yo what's up guys so today we're going to check out how we can use openai's API platform along with node.js and express to create our own gpt3 jet GPT powered rest API for our example we're going to create an API endpoint which takes in a piece of code on one side and then returns to the Big O time complexity of that code that said you'll see that you can use these same tools to build all kinds of amazing stuff it's really just up to your imagination so feel free to divert From the Path a bit and build whatever you think is cool and without any further Ado let's just jump right into the code to start we will need three things first you'll need a copy of node.js if you don't already have this head over to nodejs.org and simply download that onto your system secondly you'll need an account on beta.openai.com when you first create your account you should get 18 of free credits to use over your first month so you shouldn't need to add a credit card or anything like that once you've got an account go to the top right corner of your screen click on your account then view API Keys click on create new secret key then copy this key to be used later in your project finally only an API client for interacting with your server I'll use Postman which is free but feel free to use whichever one you would like now that we've got what we need I've open up my terminal and navigated to an empty folder where I want this project to live inside of this terminal I'll run npm and knit y to generate a blank package.json file we'll need to add three normal dependencies those being express.ed and openai and sewing those should take a couple of seconds but once you've done that we can also install nodemon as a Dev dependency using the Dash D flag just to make our lives a little bit easier I'll open up a project using vs code instead of which we should just have our package.json file looking something like this or replace the default script with a Dev script which I want to call nodemon index.js running our server with nodemon will allow us to watch for changes in our code and automatically restart our server if anything changes in my files I'll add a file for our API key called EnV inside of it we can paste our API key that we copied earlier I'll call mine openai key like this in all caps just make sure not to push this up to something like GitHub because this key is a private key back over in my files will add a file called index.js for this example we'll just do everything in this one file but feel free to break everything out and organize a little bit if you so wish up at the top of our file we'll start by importing our dependent first we'll import Express by requiring Express like this under this will import dot end remembering to call the dot config method on it so that our API key is loaded into the environment finally we'll need two imports from openai the first is configuration which is simply an object for defining things like our API key and the second is open AI API this object once instantiated will give us an easy interface for interacting with the openai API and generating images and text based on prompts once everything is imported we can start by making sure that we've got a server running or create our server object by making a variable called app and setting it equal to express then calling the function directly underneath I'll call app.use then inside of that call express.json this adds a piece of middleware essentially code that will run on every request which parses the body of the request into a Json format next I want to define a port for the server to run on if one is available in the environment we can use that also just use 5000. finally call app.listen to start our server passing it the port as the first argument then a function which console logs server started on Port X just so that we know it's working with all this out of the way we should now be able to run and PM run Dev in our terminal and see this message logged out confirming our setup is working as expected to get our endpoint up and running we'll first need to set up open AI create a variable called configuration set this equal to a new configuration and pass this an object with our API key remember that this object comes from openai and the environment variable will only be present if you set up dot end correctly under this we'll create another variable called openai and set this equal to the openai API object passing it our configuration to start saying this in action we'll use our add variable to say that we want to listen for a post request the first argument of this function is where we want to listen for the request and my case will say slash find complexity the second argument will pass is the function that we actually want to run when the endpoint is hit because we want to call openai we'll want async function taking in Arguments for request and response inside of our function We'll add a try catch block to make sure we catch any errors and we can test that this is working by returning a response with a status code of 200 and just some Json with the value of working or call our API from Postman sending a post request to localhost 5000 my complexity and we should see that we get a response with our message now that we know our server is working we can go ahead and start hitting openai or create a variable called response setting it equal to the open AI object we created earlier this object has several available functions but the one we're going to want is create completion since the function is asynchronous we're going to want to add in the weight to this function there's a number of parameters that we can pass but the ones that are most important are model and prompt model is a string of which open AI model we're actually going to want to use there's a number of these with different pros and cons and different costs which you can read about in their docs but for this example we're going to use text DaVinci 003 The Prompt is the actual text that we want to pass for the API response and we'll start by hard coding a JavaScript function for which we want to find the time complexity my example is essentially a basic for Loop over an array which should give us an O of n time complexity under this loss the model to give us the time complexity of this function I found from the docs that the results are more consistent when given the separator so I'll add something like this as well we'll need a couple more values for the number of tokens that we want to use at most as well as some others for how exactly we want the model to behave each of these is a bit out of scope of this video but you can check out the docs here as well as check out some examples like this one on open ai's website I'll go down to our response and switch this to pass back a value of success true as well as some data there's one phones from openai will live on response.data.choices for example we only expect one choice so choices at index 0 then dot text for the actual response we can also catch any errors returning the error response from open AI if one exists else a generic response message running this again in postband we should see that we get a successful response with open AI informing us that the time complexity is O of n for this function to wrap this up we'll come back up to the top of our try catch and destructure a value of prompt from our request we'll pass this value along with the request and it should contain the actual code that we want to respond for and our back text we'll replace the hard-coded value with the prompt then head back over to postman in Postman I'll click on body then raw Json we'll pass some Json with the key of prompt followed by the code we want to check while passing code which contains two nested loops and open AI should now tell us that the code is O of N squared time which is correct for this piece of code to wrap up I'll note again that there are many more examples like this one on open ai's website I'll have a link to those in the description to check out more if you're interested beyond that I hope you learned something smash the like button to help me help you prevent the AI overlords from taking our jobs until next time peace
Info
Channel: Tom Is Loading
Views: 28,451
Rating: undefined out of 5
Keywords:
Id: UQamG425RD0
Channel Id: undefined
Length: 5min 56sec (356 seconds)
Published: Fri Jan 06 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.