const configuration = new Configuration({
apiKey: “API_KEY”,
});
TypeError: Configuration is not a constructor
at Object. (D:\nodeJS-chatBot\index.cjs:5:23)
at Module._compile (node:internal/modules/cjs/loader:1226:14)
at Module._extensions…js (node:internal/modules/cjs/loader:1280:10)
at Module.load (node:internal/modules/cjs/loader:1089:32)
at Module._load (node:internal/modules/cjs/loader:930:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
at node:internal/main/run_main_module:23:47
It looks like vaibhavambhore803 is having an issue while trying to integrate OpenAI’s API with a Node.js application. The error message indicates that Configuration is not being recognized as a constructor.
Here’s a possible solution to the problem, outlined step-by-step:
1. Install the OpenAI Package
Ensure that you have installed the OpenAI package for Node.js. You can do this by running the following command in your project directory:
bash
npm install openai
2. Import the OpenAI Library
The code snippet you provided seems to be using incorrect import syntax. Make sure to import the OpenAI library properly by using the following code:
javascript
const { OpenAIApi } = require('openai');
3. Initialize the OpenAI API
Next, you can initialize the OpenAI API with your API key as follows:
javascript
const openai = new OpenAIApi({
api_key: 'API_KEY'
});
Make sure to replace 'API_KEY' with your actual API key.
4. Check Your Code
Here’s a revised version of the code snippet:
javascript
const { OpenAIApi } = require('openai');
const openai = new OpenAIApi({
api_key: 'API_KEY'
});
// Rest of your code
5. Run Your Code
Now, try running your code again to see if the error has been resolved.
If the issue persists, please share more details about your environment, such as the Node.js version and the version of the OpenAI package you are using. This will help diagnose the problem more accurately.
I was having the same error and found out this is due to some changes in the npm openai version 4 package. Check the Migration Guide in the openai-node github repository including all significant changes from v3 to v4, those changes include the initialization of the api, creating transcriptions and more.
I created a ChatGPT-4 chat that contains the migration guide text and is ready for a query, if you get code issues run them into that chat and you should get a useful result.
I got the same question here, it shows “const configuration = new OpenAIApi({
TypeError: OpenAIApi is not a constructor” after I change it into OpenAIApi