I am facing Issue while integrating the createChatCompletion function with openAI in NodeJS

Error: Request failed with status code 400.
Everytime I am sending the request to this function, its just giving the this error.

Not able to figure out why I am getting this error again and again

Can you share your code (without API key)? 400 error generally indicates a problem with the code

socket.on(“sendMessage”, async (message, callback) => {
try {
// Add the user message to the conversation history
conversationHistory.push({ role: “user”, content: message });

  console.log('CONVERSATION HISTORY :::: ', conversationHistory)
  const completion = await openai.createChatCompletion({
    model: 'text-davinci-003',
    messages: conversationHistory,
    max_tokens: 100,
    temperature: 0.5,
  });
  
  const response = completion.data.choices[0].text.trim();
  console.log('RESPONSE OF THE CHAT-GPT :::: ', response)
  conversationHistory.push({ role: "assistant", content: response });

  socket.emit("message", response);
  callback();
} catch (error) {
  console.error(error);
  callback("Error: Unable to connect to the chatbot");
}

});

I think you just have the wrong model. text-davinci-003 is only for completion not the chat completion. Should be gpt-3.5-turbo.

Chat Completion API

const { Configuration, OpenAIApi } = require("openai");

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

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

1 Like

message: ‘The model: gpt-3.5-turbo does not exist’,

I am getting this error when changing the model of the configuration

Sorry about the late response. I went to bed right after posting that. Try the link below. It has a bunch of different compatible models for each endpoint. If none of them work, maybe it is something in your settings that is wrong or you need to get an updated package. It is in beta so it’s always possible something accidently got changed.

Models - OpenAI API

1 Like

What model are you trying to use?

For GPT:

const { Configuration, OpenAIApi } = require("openai");

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

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

For davinci-003:

const { Configuration, OpenAIApi } = require("openai");
const configuration = new Configuration({
  apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);
const response = await openai.createCompletion({
  model: "text-davinci-003",
  prompt: "Say this is a test",
  max_tokens: 7,
  temperature: 0,
});