Nodejs OpenAI ChatGPT API error 400 without error

560 views Asked by At

i am trying to use official openai nodejs with my backend, but i keep getting 400 empty error.. i am check the api key but still getting error,

here is my code

implementation
export class ChatGPTServiceImplemnt implements ChatGPTService {
  async sendMessage(message:string): Promise<string|undefined> {
    try {
      const respons = await openai.createChatCompletion({
        model: "gpt-3.5-turbo",
        messages: [{ role: "user", content: message }],
        temperature: 0,
        top_p: 1.0,
        n: 1,
        frequency_penalty: 0.0,
        presence_penalty: 0.0,
        stop: ["#", ";"],
      });
      return respons.data.choices[0].message?.content;
    } catch (error) {
      console.log(`this is the error from this ${error}`);
      throw error;
    }
  }
}

here is my request point ..

export async function
chatGPT(req: Request, res: Response,) {
  try {
    const {message} = req.body;
    if (message == null) {
      return res.status(400).send({message: "Missing fields"});
    }
    const chatGPTService = myContainer
        .get<ChatGPTService>(TYPES.ChatGPTservice);
    const dataAuthService = await chatGPTService.sendMessage(message);
    if (dataAuthService != undefined) {
      const data = dataAuthService;
      return res.status(200).send({message: "Succesfull", data});
    } else {
      return res.status(200).send({message: "faild", dataAuthService});
    }
  } catch (error) {
    return res.status(400).send({error, message: "error"});
  }
}

here is the error

error

0

There are 0 answers