Simple Completion call not working (in React)

Hey,

I thought this was supposed to be easy lol! Apparently I’m not sending the correct request? Doing this in React for a Next.js application if that matters.

Here’s a snippet of my code:

export default async function (req, res) {
  if (!checkApiKey(res)) return;

  const userMessage = req.body.userMessage || "";

  // Log the received request body
  console.log("Received request body:", req.body);

  // Prepare the messages for the chat conversation
  const inputs = [
    { role: "system", content: "You are a wise and loving grandmother." },
    { role: "user", content: userMessage },
  ];

  try {
    const response = await openai.createCompletion({
      model: "gpt-3.5-turbo",
      messages: inputs, // Use the messages parameter
      max_tokens: 100,
      temperature: 0.7,
    });

A little confused why it’s not recognizing the messages input? This is exactly what the docs say to do here: OpenAI API

Error response data: {
  error: {
    message: 'Unrecognized request argument supplied: messages',
    type: 'invalid_request_error',
    param: null,
    code: null
  }
}

For chat completions, it’s openai.createChatCompletion. That one got me too.

1 Like

Yeah, that worked. Had me scratching my head for hours over something hidden in plain sight.