Using openai.ChatCompletion.create function

I was using the function openai.chatCompletion.create, which worked earlier, but now has stopped working by giving an error as invalid. It was allowing me to give functions to gpt as well, although that isn’t necessary. Does anyone know how it can be solved, if I use chatCompletioncreate, there is some issue with json callback.

Welcome to the forum!

Can you please post your API calling code and any setup that it relies upon along with the error logs you are receiving.

this is the function under a class which calls the API:

_createChatCompletion(messages, useFunctions = true) {
      if (useFunctions && this.functions) {
        return openai.createCompletion({
          model: this.model_name,
          messages: messages,
          functions: this.functions,
        });
      } else {
        return openai.createCompletion({
          model: this.model_name,
          messages: messages,
        });
      }
    }

This is the error log:

  },
      agent: undefined,
      agents: { http: undefined, https: undefined },
      auth: undefined,
      hostname: 'api.openai.com',
      port: null,
      nativeProtocols: { 'http:': [Object], 'https:': [Object] },
      pathname: '/v1/completions'
    },
    _ended: true,
    _ending: true,
    _redirectCount: 0,
    _redirects: [],
    _requestBodyLength: 101,
    _requestBodyBuffers: [],
    _onNativeResponse: [Function (anonymous)],
    _currentRequest: [Circular *2],
    _currentUrl: 'https://api.openai.com/v1/completions',
    [Symbol(kCapture)]: false
  },
  [Symbol(kCapture)]: false,
  [Symbol(kBytesWritten)]: 0,
  [Symbol(kNeedDrain)]: false,
  [Symbol(corked)]: 0,
  [Symbol(kOutHeaders)]: [Object: null prototype] {
    accept: [ 'Accept', 'application/json, text/plain, */*' ],
    'content-type': [ 'Content-Type', 'application/json' ],
    'user-agent': [ 'User-Agent', 'OpenAI/NodeJS/3.2.1' ],
    authorization: [
      'Authorization',
      'Bearer -----------------------------'
    ],
    'content-length': [ 'Content-Length', 101 ],
    host: [ 'Host', 'api.openai.com' ]
  },
  [Symbol(errored)]: null,
  [Symbol(kUniqueHeaders)]: null
},
data: {
  error: {
    message: "We could not parse the JSON body of your request. (HINT: This likely means you aren't using your HTTP library correctly. The OpenAI API expects a JSON payload, but what was sent was not valid JSON. If you have trouble figuring out how to fix this, please contact us through our help center at help.openai.com.)",
    type: 'invalid_request_error',
    param: null,
    code: null
  }

Ok, so the error message is informing you that the message body is not in the correct format, where are you getting the openai.createCompletion function from?

How are you constructing the messages variable? also, what is the type of this.functions?

this.function is a null value in this case, I created it to pass any mathematical function, to give an input float, and obtain an output float. The messages variable are being sent through express using an async function

async function handleSubmit(e) {
// “preventDefault” Prevents refreshing of page on submission
e.preventDefault();
/* Only add our user message and fetchs response if we are not already waiting for a response
Also checks that there even is any input*/

if (!waitingForResponse && input ) {
  setWaitingForResponse(true);
  setIsLoading(true);
  let newMessageList = [
    ...messageList,
    { user: "user", message: `${input}` },
  ];
  // Resets input field to blank
  setInput("");
  setMessageList(newMessageList);

  const messages = newMessageList
    .map((message) => message.message)
    .join("/n");

  const response = await fetch(
    "http://localhost:9000",
    {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
      },
      body: JSON.stringify({
        message: messages,
      }),
    }
  );
  const data = await response.json();
  setMessageList([
    ...newMessageList,
    { user: "bot", message: `${data.message}` },
  ]);
  setWaitingForResponse(false);
  setIsLoading(false);
} message while we are waiting for a response

}

Something to check, you have .join("/n"); it should be "\n" for a newline