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.
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:
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:
Then, you can initialize a new TypeScript project by running the following command:
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:
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:
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:
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:
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:
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:
That will generate a response using ChatGPT and return it in the response body:
Given that we implemented the controller to use the conversation's history, we can make another question to ChatGPT based on the previous one.
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.
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.