I am getting an error in this code. what is wrong. It logs the messages in the console. message is being sent corectly from the frontend. api is throwing error

import { NextResponse } from “next/server”;
import { auth } from “@clerk/nextjs”;
import { OpenAI } from “openai”;
const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });

export async function POST(req: Request) {
try {
console.log(“*****************”);
const { userId } = auth();
const body = await req.json();
const { messages } = body;
if (!userId) {
return new NextResponse(“Unauthorized”, { status: 401 });
}
console.log(“iddddddddddddddd”);

console.log("keyyyyyyyyyyyy");
console.log(messages);

if (!messages)
  return new NextResponse("Messages are required", { status: 400 });

console.log("mesageeesssssssssssssssss");
const response = await openai.chat.completions.create({
  messages,
  model: "gpt-3.5-turbo",
});
console.log(response.choices[0].message.content);

return NextResponse.json(response.choices[0].message.content);

} catch (error) {
console.error(“[CONVERSATION_ERROR]”,error);
return new NextResponse(“Internal error”, { status: 500 });
}
}

Above, the you are missing the actual parameter value for messages.

It should be specified like the model (but with your variable with the json array object of messages instead of the model name as a string.) It is not a positional parameter.

Try to see what’s in messages it should look like here (an array).
If it’s indeed correct it could be useful to see what the error is saying.

1 Like

In my open ai api i am in free tier,and today i am trying to make the request after one day , but still it says 429 error, RateLimitError: 429 You exceeded your current quota, please check your plan and billing details. I am trying for the first time for today

Hey,

You should probably check your plan and billing details as suggested.

Here is a related post that might provide some help too: 429 Error : insufficient_quota