Getting 500 errors with gpt-3.5-turbo in local enviornment

It seems like you’re trying to use OpenAI’s GPT-3.5-turbo model with Node.js to generate responses

Here’s a quick review of your code and some suggestions on how to improve it:

Make sure that you’ve imported the necessary modules at the beginning of your script:

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

const config = require('./config'); // Replace 'config' with the actual path to your configuration file

Regarding your initOpenAIGPTConfiguration function, there are a few modifications that could be made for better readability and error handling:

const initOpenAIGPTConfiguration = async (message) => {

  const configuration = new Configuration({

    apiKey: config.OPENAI_API_KEY,

  });



  console.log("message=", message);

  const openai = new OpenAIApi(configuration);



  try {

    const response = await openai.createChatCompletion({

      model: "gpt-3.5-turbo",

      messages: message,

      temperature: 0,

      max_tokens: 2048,

    }, { responseType: 'stream' });



    return new Promise((resolve, reject) => {

      let result = "";

      let join_chunk = "";



      response.data.on('data', data => {

        const lines = data.toString();

        console.log("lines=", lines);

        let regex = /finish_reason/g;

        if (lines.match(regex)) {

          join_chunk += lines;

          result = JSON.parse(join_chunk).choices[0].message.content;

          resolve(result);

        } else {

          join_chunk += lines;

        }

      }).on('end', () => {

        console.log("initOpenAIGPTConfiguration", result);

      }).on('error', error => {

        console.error('An error occurred during OpenAI request', error);

        reject('error');

      });

    });



  } catch (error) {

    console.log("error ====", error);

  }

}

Overall, your code looks good, and it should work as intended. These suggestions mainly focus on improving readability and error handling. Don’t hesitate to ask if you have any questions or need further assistance!