OpenAI API Requests in JavaScript

Making Requests to OpenAI's GPT APIs Using JavaScript

In this guide you'll learn how to make requests using JavaScript to OpenAI's GPT APIs.

Most of the documentation on OpenAI's website shows Python examples, but there is a Node client from OpenAI called openai-node on GitHub.

Make sure you have an API key for OpenAI, as it is required for the client.

Installing openai-node

First, install the openai-node package using npm:


npm install openai

Basic Query in JavaScript

Create an app.js file.

In this example we're using an environment variable for our API key using the dotenv package. You can also hardcode the key, but it's not recommended.


require("dotenv").config();

Next we'll bring in the OpenAI package, then create a new configuration instance with the API key variable:


const { Configuration, OpenAIApi } = require('openai');
const configuration = new Configuration({ apiKey: process.env.OPENAI_API_KEY });

Then we can create a new instance of OpenAIApi with the configuration:


const openai = new OpenAIApi(configuration);

Everything is now in place to make requests!

Making a Request

To make a request we'll be using the createChatCompletion method from openai.

Here's an example of an async function called getPoem. The createChatCompletion method returns a promise, so we use await to handle it.

The modelName will be either gpt-3.5-turbo or gpt-4. Then we have an array of messages that will contain at least one object with a role and content.

In this case, the message will be a user prompting for a rhyming poem:


async function getPoem() {
const response = await openai.createChatCompletion({
modelName: 'gpt-4',
messages: [
{
role: 'user',
content: 'generate a short rhyming poem',
},
],
});
console.log(response.data.choices[0].message.content)
}

The content of the response message is accessed with:


response.data.choices[0].message.content

Now every time you run getPoem(), a new rhyming poem will be displayed in the console:

gptrequests

That's it! Now you know how to make requests to OpenAI's GPT APIs using JavaScript and the openai-node client.

Transcript

00:00 In this quick video, I'm going to show you how to make requests using JavaScript to OpenAI's GPT APIs. So most of the documentation on OpenAI's website shows Python examples, but there is a Node client put out by OpenAI called OpenAI-Node on GitHub that we can use.

00:19 The first step, of course, is to install it. npm install openai. And if you haven't already signed up for an API key, you'll need to do that because the client needs the API key. So this is my app.js file. I'm just going to do a very basic query in here.

00:34 And the first step is, of course, to require OpenAI, which we just installed. Specifically, though, we're going to require configuration and OpenAI API. The next step is to provide our API key to a new configuration instance.

00:51 So I'm using an environment variable. Of course, you could just hard code your key in here, but that's not really good practice. But if you just want to get up and running, you can do that.

00:59 So I have mine in a.env file. I'm using the.env package to load my environment variables and then grabbing one of them called OpenAI API key and assigning it here.

01:12 Once we have this configuration, the second step is to create a new instance of OpenAI API where we pass in our configuration. So if you get to that point and you have a valid API key, we now should be able to make requests. So how do we make requests?

01:28 If you look at the documentation for this package, it's a little bit out of date, and it shows the create completion method, which still works. But this does not support the chat endpoint, meaning it does not support GPT 3.5 Turbo or GPT 4.

01:43 It only supports Text DaVinci 003 as the most recent model. So instead, what we need to do is use a method that exists, it's just not on the readme called create chat completion. And this returns a promise. So we're going to want to await this and put it in some sort of async function.

02:02 I'll call my async function get poem. We will await this. We'll call this response or something that we get back. And then we have to provide an object in here that has a couple of pieces. The first thing is the model name that we want to use. I'm going to use GPT 4.

02:20 And then we have to provide a list of messages or an array of messages. Now, I have a separate video talking about the messages format, but the bare minimum here is a single message in an array. And each message has a role. We'll set this first one to user. And I'm going to ask it to generate me a poem.

02:38 So the content will be generate a short rhyming poem. I'm not even going to give a direction as to what the poem should be about. So we await this. This will make our API request, assuming our key is configured properly.

02:54 Then we need to look at the response.data. And maybe I'll just start by printing that out. Response.data. Let's execute get poem just one time down at the bottom here. And let's run our file, app.js. All right. So we get some data back.

03:10 The stuff we care about for the most part is inside of choices, which is an array. We want the first choice, the message. It's a little bit annoying. It looks like this. Response.data.choices. We want the zero with element. And then we want.message.content. And that will give us the content of the response message.

03:29 So let's try running this. And maybe we'll do it twice to get two different poems. Okay. So we'll wait. And here's our first poem. There's no divider between the two, but this is the first poem, and this is the second poem. Not the most useful query, but that's beside the point.

03:44 What matters most here is seeing how to make a request in JavaScript using the OpenAI client.

More Tips