skip to Main Content

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 follow 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 });

The error must gone.

2

Answers


  1. 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 comments, 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 });
    
    Login or Signup to reply.
  2. 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 compiles 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
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search