#```
1 messages · Page 1 of 1 (latest)
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
And the full error?
> 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,`
This is normal, now the return is little bit different. You don't need to use "data"
response.data.choices[0].text,
Must be response.choices[0].text
Since the 4.x
ohh gotcha!
So must be local problem i think
Try to do a npm install to update your packages maybe
Also try to avoid at any cost the davinci model, it's same price than GPT4 for very bad results
Maybe you have a package which use OpenAI too
What's your process for debugging something like this? I've usually worked on c++ code and easier to build unit test for them
and use an older version
ah maybe
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