Search code examples
javascriptnode.jsopenai-apichatgpt-apigpt-3

OpenAI gpt-3.5-turbo: Request failed with status code 400


does this method in node.js doesn't work anymore? Because back then it was working fine but now it doesn't work anymore and also this code is also based on their official docs which is this https://platform.openai.com/docs/api-reference/completions/create

My server end code:

    import { Configuration, OpenAIApi } from 'openai';
    //....
    const configuration = new Configuration({
      apiKey: API_KEY,
    });
    //....
    const openai = new OpenAIApi(configuration);
    //....
    const response = await openai.createChatCompletion({
      model: "gpt-3.5-turbo",
      messages: [
        {
          role: "system", 
          content: `You are a helpful assistant.` },
        ...prompt
      ],
      temperature: 0.2,
      max_tokens: 1500,
      top_p: 1,
      frequency_penalty: 0,
      presence_penalty: 0,
    });
    //....
    res.status(200).send({
      bot: response.data.choices[0].message.content
    });
    //....

The data I am trying to send:

{
  "prompt": [
    {
      "role": "bot",
      "content": "Something went wrong."
    },
    {
      "role": "user",
      "content": "What is wrong?"
    }
  ]
}

i am getting this kind of error: enter image description here| The output of the message prompt is in the terminal just incase you want to check if i am sending correct message prompt.

I also tried adding the org id and still didn't work and also tried updating it from v3.2.1 to v3.3.0 nothing works at all. I still have balance in the account.


Solution

  • The issue solved, I was sending a wrong role instead of bot it should be assistant. So this format will make everything work again:

    {
      "prompt": [
        {
          "role": "assistant",
          "content": "Something went wrong."
        },
        {
          "role": "user",
          "content": "What is wrong?"
        }
      ]
    }
    

    enter image description here

    Based on https://platform.openai.com/docs/api-reference/chat/create there is only 4 roles: system, user, assistant, or function

    enter image description here