← Home

Using ChatGPT OpenAI API in a Node.js App

January 14, 2023

ChatGPT is a powerful language model developed by OpenAI that can generate human-like text based on a given prompt. This makes it an ideal tool for creating chatbots, virtual assistants, and other applications that require natural language processing.

In this article, we will explore how to use the ChatGPT API in a Javascript Node.js application by using the openai package. We will cover the following topics:

  • Setting up a Node.js project
  • Installing the necessary dependencies
  • Authenticating with the OpenAI API
  • Using the openai package to generate text
  • Handling the response

Setting up a Node.js project

npm init

This will create a package.json file, which will contain all the necessary information about your project.

Installing the necessary dependencies

Next, we need to install the necessary dependencies for our project. We will be using the openai package to interact with the OpenAI API.

To install this package, run the following command:

npm install openai

Authenticating with the OpenAI API

To use the ChatGPT API, you will need to sign up for an OpenAI API key. Once you have your API key, you can set it in your application using the following code:

const openai = require("openai")
openai.apiKey = "YOUR_API_KEY"

Using the openai package to generate text

Now that we have set up our API key, we can use the openai package to generate text using the ChatGPT model. We will be using the completions.create method, which takes in several parameters, including the prompt, model, and temperature (used to control the level of randomness in the generated text).

const prompt = "What is the weather like today?"
const model = "text-davinci-003"
const temperature = 0.5

openai.completions.create(
  {
    prompt: `${prompt}`,
    model: `${model}`,
    temperature: temperature,
    max_tokens: 1024,
    top_p: 1,
  },
  (error, response) => {
    if (error) {
      console.log(error)
    } else {
      console.log(response.choices[0].text)
    }
  }
)

In this example, we are using the davinci model and setting the prompt to “What is the weather like today?” and the temperature to 0.5. The max_tokens parameter is used to specify the maximum number of tokens generated. The top_p parameter is used to specify the proportion of the mass of tokens generated.

Handling the response

After sending the request, the API will return a response in the form of a JSON object. The response will contain several properties, including the generated text.

{
  "id": "your_id",
  "choices": [
    {
      "text": "It looks like it's going to be sunny today.",
      "index": 0
    }
  ]
}

In this example, the generated text is “It looks like it’s going to be sunny today.”

You can then use this generated text in your application as you see fit. For example, you could use it to create a chatbot that can answer weather-related questions.

In this article, we have explored how to use the ChatGPT API in a Javascript Node.js application by using the openai package. We have seen how to set up a Node.js project, install the necessary dependencies, authenticate with the OpenAI API, use the package to generate text, and handle the response. With this knowledge, you can now start building your own applications that use the power of natural language processing.


Chafik Gharbi Full-stack web and mobile app developer with JavaScript.