最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - TypeError: OpenAIApi is not a constructor - Stack Overflow

programmeradmin3浏览0评论

i got error:

const openai = new OpenAIApi({ key: apiKey });
               ^

TypeError: OpenAIApi is not a constructor

when I'm trying to create an openai api using nodejs (v16.7.0). I've followed the code in documentation and installed the openai api (v4.3.1)

code I used:

const { OpenAIApi } = require('openai');

const openai = new OpenAIApi({ key: apiKey });

How can I resolve this?

i got error:

const openai = new OpenAIApi({ key: apiKey });
               ^

TypeError: OpenAIApi is not a constructor

when I'm trying to create an openai api using nodejs (v16.7.0). I've followed the code in documentation and installed the openai api (v4.3.1)

code I used:

const { OpenAIApi } = require('openai');

const openai = new OpenAIApi({ key: apiKey });

How can I resolve this?

Share Improve this question edited Aug 31, 2023 at 0:53 desertnaut 60.4k32 gold badges152 silver badges178 bronze badges asked Aug 30, 2023 at 13:52 Justine GwapoJustine Gwapo 811 gold badge1 silver badge3 bronze badges 2
  • 1 maybe try doing npm install openai again just incase and check to see if the versions are patible after npm show openai version – Nikster Commented Aug 30, 2023 at 13:57
  • This worked for me, you can post it as an answer – Muhammad Amir Commented Jun 16, 2024 at 18:58
Add a ment  | 

5 Answers 5

Reset to default 4

Accordingly to documentation you don't have to use destructure operator on imported object.

If you ommit it, everything should run fine.

const OpenAIApi = require('openai');

const openai = new OpenAIApi({ key: apiKey });

UPD. Actually required object contains needed class, it's OpenAI.
So you have to specify the correct name:

const { OpenAI } = require('openai');

const openai = new OpenAI({ key: apiKey });

UPD2. As Mr. Polywhirl mentioned in ments, there exists third approach. It's to use class that lies inside imported library object.

const OpenAIApi = require('openai');

const openai = new OpenAIApi.OpenAI({ key: apiKey });

The thing is that you used version 3 code, but probably have installed the newest version (like myself), which is 4. Check the latest docs, they changed a lot of stuff: https://www.npmjs./package/openai/v/4.12.4

All should need to do is remove the curly braces (destructuring).

const OpenAIApi = require('openai');

const openai = new OpenAIApi({ key: apiKey });

Note that the official documentation advises you name the import OpenAI per the example usage, but you should be able to name it anything you like.

If you open up node_modules/openai/src/index.js, you will see exports.default = OpenAI; as the bottom. This is exporting class OpenAI extends Core.APIClient as the default export. This means you can name it anything you want, when you import the library.

const FooBar = require('openai');

const openai = new FooBar({ key: apiKey });

What may seem confusing is that index.ts has the following exports. The class and the namespace have the same name.

export class OpenAI extends Core.APIClient {
  // ...
}

export namespace OpenAI {
  // Helper functions
  export import toFile = Uploads.toFile;
  // ...
}

export default OpenAI; // Add namespace functions as static methods to the class

This piles to the following JS, because esModuleInterop is set to true in the openai-node libraries tsconfig.json

exports.OpenAI = OpenAI;
exports.toFile = Uploads.toFile;
(function (OpenAI) {
  // Helper functions
  OpenAI.toFile = Uploads.toFile;
  // ...
})((OpenAI = exports.OpenAI || (exports.OpenAI = {})));
exports = module.exports = OpenAI;
exports.default = OpenAI;

A better approach

Store your API key appropriately

  1. Install dotenv as a dependency
npm install dotenv
  1. Add an API_KEY to your .env file at the root of your project
echo "API_KEY=" >> .env
  1. Update the server script
const dotenv = require('dotenv');
const OpenAI = require('openai');

dotenv.config() // Load the environment

const openai = new OpenAI({ key: process.env.API_KEY });

Alternatively

You can use the preferred environment variable

echo "OPENAI_API_KEY=" >> .env
const dotenv = require('dotenv');
const OpenAI = require('openai');

dotenv.config() // Load the environment

const openai = new OpenAI(); // The key is picked-up automagically

You must update your package.json to specify v4:

 "openai": "^4.0.0"

Run "npm install".

After this, you can use:

const OpenAI = require("openai");
const openai = new OpenAI({
    apiKey: process.env.OPENAI_API_KEY,
});

More info: https://github./openai/openai-node/discussions/217

I have tried some of the suggested solutions and did kept getting an error when you

new OpenAI({key:apiKey})

i resolved the initialisation issue writing the code like this

const apiKey = process.env.OPEN_AI_KEY;
const { OpenAI } = require('openai');

const openai = new OpenAI({
  apiKey,
});

thank you, hope this helps.

发布评论

评论列表(0)

  1. 暂无评论