SyntaxError: The requested module 'openai' does not provide an export named 'OpenAIApi'

Hi I’m working on a project and I have this error message : import { Configuration, OpenAIApi } from “openai”;
^^^^^^^^^
SyntaxError: The requested module ‘openai’ does not provide an export named ‘OpenAIApi’
at ModuleJob._instantiate (internal/modules/esm/module_job.js:92:21)
at async ModuleJob.run (internal/modules/esm/module_job.js:107:20)
at async Loader.import (internal/modules/esm/loader.js:179:24)
[nodemon] app crashed - waiting for file changes before starting…

This is the code :

import express from "express";
import * as dotenv from "dotenv";
import cors from "cors";
import { Configuration, OpenAIApi } from "openai";

const configuration = new Configuration({
    apiKey: "",
});

const openai = new OpenAIApi(configuration);

const app = express();
app.use(cors());
app.use(express.json());

app.get("/", async (req, res) => {
    res.status(200).send({
        message: "Hi Patrick! Welcome to ChatGPT",
    });
});

app.post("/", async (req, res) => {
    try {
        const prompt = req.body.prompt;

        const response = await openai.createCompletion({
            model: "text-davinci-003",
            prompt: `${prompt}`,
            max_tokens: 3000,
            top_p: 1,
            frequency_penalty: 0.5,
            presence_penalty: 0,
        });

        res.status(200).send({
            bot: response.data.choices[0].text,
        });
    } catch (error) {
        console.log(error);
        res.status(500).send(error || "Something went very wrong");
    }
});

app.listen(3000, () =>
console.log("AI server started on port: http//localhost: 3000")
);

Can you help me?`Preformatted text`
1 Like

Hi! Welcome to the forum!

Could you kindly edit your post and use the preformatted text tool (or simply wrap your code in ``` )

Edit: that said, why not use OpenAI like the playground generates for you?

import OpenAI from "openai";

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

const response = await openai.completions.create({
  model: "gpt-3.5-turbo-instruct",
  prompt: "Hi!\n\n",
  temperature: 1,
  max_tokens: 256,
  top_p: 1,
  frequency_penalty: 0,
  presence_penalty: 0,
});

This is using an obsolete AI model and wrong method, likely for the wrong endpoint for the application.

Did an AI write this?

Try API Reference on the sidebar of this forum for modern model marvels and coding methods.


(The shown AI model is now past its permanent shutdown time everywhere in the world but still saying hello…)
image

This is the new error message const openai = new OpenAI({
^

TypeError: OpenAI is not a constructor
at file:///Users/user/Downloads/chatGPT-Zaamu/server/server.js:7:16
at ModuleJob.run (internal/modules/esm/module_job.js:110:37)
at async Loader.import (internal/modules/esm/loader.js:179:24)

This is my new code : `

import express from "express";
import * as dotenv from "dotenv";
import cors from "cors";
import OpenAI from "openai";

// Utilisez un seul nom de variable pour l'instanciation de la classe OpenAI
const openai = new OpenAI({
    apiKey: "",
});

const app = express();
app.use(cors());
app.use(express.json());

app.get("/", async (req, res) => {
    res.status(200).send({
        message: "Hi Patrick! Welcome to ChatGPT",
    });
});

app.post("/", async (req, res) => {
    try {
        const prompt = req.body.prompt;

        const response = await openai.completions.create({
            model: "gpt-3.5-turbo-instruct",
            prompt: `${prompt}`,
            max_tokens: 3000,
            top_p: 1,
            frequency_penalty: 0.5,
            presence_penalty: 0,
        });

        res.status(200).send({
            bot: response.data.choices[0].text,
        });
    } catch (error) {
        console.log(error);
        res.status(500).send(error || "Something went very wrong");
    }
});

app.listen(3000, () =>
    console.log("AI server started on port: http//localhost:3000")
);`

hmm. works on my machine :thinking:

what version are you using?

I tried with "openai": "4.24.1",

all that said

I’m not a fan of the libs. In production, we use axios.

the project requested version “openai”: “^3.1.0”

is there anything that prevents you from updating?