This article is a step-by-step guide to building a Node.js (TypeScript-based) REST API to interact with an advanced artificial intelligence language model developed by OpenAI (ChatGPT).
To use ChatGPT with Node.js (or any other language), we need to use OpenAI's ChatGPT API. For that purpose, we can use the openai package, which provides a simple way to interact with the OpenAI API and has TypeScript support.
Starting from scratch, we will set up our project, create OpenAI API keys, build our API controller, and make the API able to establish a historical conversation in the natural language. Whether you are a skilled developer or just started with Node, this article will provide a clear guide to building the whole thing.
Code hands-on
Step 1: Install Node.js and TypeScript
First, you will need to install Node.js on your computer if you need to. You can download and install Node.js from the official website.
Next, you need to install TypeScript globally using the following command:
CODE: https://gist.github.com/pablanco/04c4c883eeb978612e7af5557f58e14e.js?file=ts-install.sh
Step 2: Create a new TypeScript project
Once TypeScript is installed, you need to create a new directory for your project and navigate to it by:
CODE: https://gist.github.com/pablanco/da49b66919ac660a122e1b16f5d83615.js?file=create-folder.sh
Then, you can initialize a new TypeScript project by running the following command:
CODE: https://gist.github.com/pablanco/a6c3f66da91e2f5bc2e3f4d39f80a6fc.js?file=tsc-init.sh
This will create a new tsconfig.json file in your project directory, containing some default information about your project and its TypeScript settings.
Step 3: Install Dependencies
Next, we must install the dependencies for building a REST API with Node.js and TypeScript. We can do this by running the following command:
CODE: https://gist.github.com/pablanco/626ed64334513679053c13d82c0da9ad.js?file=npm-install.sh
This will install the following packages:
- express: a popular web framework for Node.js
- body-parser: middleware for parsing request bodies
- openai: a package for accessing OpenAI's GPT models
- dotenv: loads environment variables from a .env file
- @types/node: TypeScript type definitions for Node.js
- @types/express: TypeScript type definitions for express
- @types/body-parser: TypeScript type definitions for body-parser
Step 4: Get Up OpenAI API Credentials
Before using ChatGPT, you must set up API credentials for the OpenAI API. To do this, you must sign up for an OpenAI account and create an API key. You can do this on the OpenAI website.
Once you have your API key, create a .env file in your project directory and place these variables:
CODE: https://gist.github.com/pablanco/1205d99765e9ce6b67516b068bc1ecb7.js?file=.env
Step 5: Create an Express server and OpenAI interaction
Now, you can create an Express server to handle HTTP requests and responses. Create a new TypeScript file called app.ts in your project root directory, and add the following code:
CODE: https://gist.github.com/pablanco/d979b83b5dacc1451ba971eac2220053.js?file=app.ts
This code sets up an Express server with body-parser middleware. It also loads environment variables from the .env file using the dotenv library.
Next, we must create a new controller function to handle the generateResponse route declared in the above code. Create a new folder called controllers in the root directory of the project, and inside of that folder, a new file called index.ts, and add the following code to it:
CODE: https://gist.github.com/pablanco/9bfa84cb44934f550ca6a61048821578.js?file=index.ts
In this example, we are using the gpt-3.5-turbo model, which can take the history of the conversation into account, and you can ask subsequent questions based on the context of the conversation that has taken place in a previous prompt.
Step 6: Build and Run
Build your application and start the server using the following commands:
CODE: https://gist.github.com/pablanco/20cc85ba714547441e80044f54948f8b.js?file=build-run.sh
Step 7: Test the API using a client
You can test the REST API using any HTTP client like Postman or PostCode. Send a POST request to http://localhost:3000/generate with the following JSON payload:
CODE: https://gist.github.com/pablanco/884c3280da7ed4f630e7faa966c7622c.js
That will generate a response using ChatGPT and return it in the response body:
CODE: https://gist.github.com/pablanco/9e96186525c56c1c873fbcb75ce2e360.js
Given that we implemented the controller to use the conversation's history, we can make another question to ChatGPT based on the previous one.
CODE: https://gist.github.com/pablanco/cde0b550e62b817a1ac237cdcbc4080d.js?file=request-2.js
CODE: https://gist.github.com/pablanco/107530fd3ec9b63fd8c99a6026f058e6.js?file=response-2.js
That's it! You have successfully built a REST API with Node.js and TypeScript to interact with ChatGPT.
Code repository example
You can get the working code example from this repository.
Final Thoughts
With this guide, you can now interact with ChatGPT through your custom Node.js API and use it as a base for your projects.
You can extend this REST API we built in several ways to make it more useful and robust by adding new features like more routes to interact with OpenAI API, create input validation, error handling, authentication, caching, etc.
These are just a few ideas where you can get started. Depending on your specific requirements and use cases, you can extend the API in many other ways. The key is to start small and gradually add new features as needed.
Also, remember that OpenAI API has limitations depending on the model you want to use, the number of requests you make, and the number of tokens (input size) each model supports. To learn more, see here.