5

I have this in my MERN stack code file, and it works well.

exports.chatbot = async (req, res) => {
  console.log("OpenAI Chatbot Post");

  const { textInput } = req.body;

  try {

    const response = await openai.createCompletion({
      model: "text-davinci-003",
      prompt: `
            What is your name?
            My name is Chatbot.
            How old are you?
            I am 900 years old.
            ${textInput}`,
      max_tokens: 100,
      temperature: 0,
    });
    if (response.data) {
      if (response.data.choices[0].text) {
        return res.status(200).json(response.data.choices[0].text);
      }
    }
  } catch (err) {
    return res.status(404).json({ message: err.message });
  }
};

While I change the API request, use the new API for chat completion, This one doesn't work(the API code is from openAI website, and works on postman)


exports.chatbot = async (req, res) => {
  console.log("OpenAI Chatbot Post");

  const { textInput } = req.body;

  try {
    const completion = await openai.createChatCompletion({
      model: "gpt-3.5-turbo",
      messages: [{ role: "user", content: textInput }],
    });
    console.log(completion.data.choices[0].message);

    if (completion.data) {
      if (completion.data.choices[0].message) {
        return res.status(200).json(completion.data.choices[0].message);
      }
    }

  } catch (err) {
    return res.status(404).json({ message: err.message });
  }
};

the error message:

POST http://localhost:3000/api/openai/chatbot 404 (Not Found)

Rok Benko
  • 14,265
  • 2
  • 24
  • 49
WynMars
  • 53
  • 1
  • 5

2 Answers2

8

You need to upgrade the OpenAI package.

Python:

pip install --upgrade openai

NodeJS:

npm update openai

Close the terminal and open it again. Run the code. The error should disappear.

Rok Benko
  • 14,265
  • 2
  • 24
  • 49
3

You need to reinstall the openai npm package. It has only just been updated with the createChatCompletion in the past 2 days.

When I reinstalled the package and ran your code it worked successfully.

Kane Hooper
  • 1,531
  • 1
  • 9
  • 21
  • Oh, you are a life saver, I looked into the codes for hours and never realize that I need to reinstall the package. Thank you so much. – WynMars Mar 03 '23 at 22:19