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 Chat Completions API. This one doesn't work.
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 });
}
};
Error I'm getting:
POST http://localhost:3000/api/openai/chatbot 404 (Not Found)
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.