First, check the OpenAI NodeJS SDK version:
- OPTION 1 - using the terminal:
npm list openai
or
- OPTION 2 - by looking at the
package.json
file.
What do you get/see? You probably have the OpenAI NodeJS SDK v4
.
Problem
You have v4
, but you want to use the code that works with v3
. SDK v4
was released on August 16, 2023, and is a complete rewrite of the SDK. Among other things, there are changes in the initialization logic. See the v3
to v4
migration guide.
Solution
Change this...
// Old (i.e., OpenAI NodeJS SDK v3)
import { Configuration, OpenAIApi } from "openai";
const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);
...to this.
// New (i.e., OpenAI NodeJS SDK v4)
import OpenAI from 'openai';
const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
});