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`
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")
);`