#```

1 messages · Page 1 of 1 (latest)

ruby sentinel
#
import OpenAI from "openai";

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

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

    const response = await openai.completions.create({
      model: "text-davinci-003",
      prompt: `${prompt}`,
      max_tokens: 300,
    });

    res.status(200).send({
      bot: response.data.choices[0].text,
    });
  } catch (error) {
    console.log(error);
    res.status(500).send(error);
  }
});
#

This is my code, excuse the old model

barren sleet
#

And the full error?

ruby sentinel
#

> openai_hat@1.0.0 start
> node ./server/server.js

const openai = new OpenAI({
               ^

TypeError: OpenAI is not a constructor
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:385:24)
    at async loadESM (node:internal/process/esm_loader:88:5)
    at async handleMainPromise (node:internal/modules/run_main:61:12)
#

I also deployed the app onto railway since I suspect it might be a local env problem, and I get a different error on railway:

#

at file:///app/server/server.js:47:26

at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
``` where server.js:47 is this line: `bot: response.data.choices[0].text,`
barren sleet
#

response.data.choices[0].text,

#

Must be response.choices[0].text

#

Since the 4.x

ruby sentinel
#

ohh gotcha!

barren sleet
#

So must be local problem i think

#

Try to do a npm install to update your packages maybe

ruby sentinel
#

I did npm uninstall openai and npm install

#

npm list openai gives me 4.19.0

barren sleet
#

Also try to avoid at any cost the davinci model, it's same price than GPT4 for very bad results

ruby sentinel
#
└── openai@4.19.0
#

Will do

barren sleet
#

Maybe you have a package which use OpenAI too

ruby sentinel
#

What's your process for debugging something like this? I've usually worked on c++ code and easier to build unit test for them

barren sleet
#

and use an older version

ruby sentinel
#

ah maybe

barren sleet
#

I'm not really sure since I don't know your whole env

#

You can check the node_modules folder to check the folder of openai

ruby sentinel
#

will do

#

i think my packagelock.json is outdated

#

thanks

barren sleet
#

Or maybe you installed openai package globally by mistake in your local env

#

npm list -g --depth 0

#

You can try this

ruby sentinel
#

Oh I have an outdated package.json in my server folder ........

#

root folder is updated lmao