openai-node icon indicating copy to clipboard operation
openai-node copied to clipboard

createChatCompletion method returning undefined in the choices array

Open ViLourenco opened this issue 2 years ago • 5 comments

Describe the bug

I'm using node.js with the official opean ai lib and when I tried to send a request, I got nothing, the choices array is empty.

It's working fine with davinci model, just this new one model: "gpt-3.5-turbo", isn't working.

image

To Reproduce

image

Code snippets

No response

OS

windows

Node version

v10.19.0

Library version

3.2.1

ViLourenco avatar Mar 02 '23 19:03 ViLourenco

The response for the 'gpt-3.5-turbo' model is not choices[0].text but rather choices[0].message.content. In any case, choices should not be empty.

I am also getting an error for the following code:

async function gptCompletion() {
  try {
    const response = await openai.createCompletion({
      model: "gpt-3.5-turbo",
      messages: [{"role":"user", "content":"Hello World"}],
    })

    return response
  } catch (e) {
    console.log("Error getting GPT completion: ", e)
    throw e
  }
}

gptCompletion() 

Getting an "Error: Request failed with status code 400"

lucianot avatar Mar 02 '23 22:03 lucianot

@lucianot you should call createChatCompletion instead of createCompletion

Fndroid avatar Mar 03 '23 02:03 Fndroid

@Fndroid its says "openai.createChatCompletion" is not a function

tv-ankur avatar Mar 03 '23 04:03 tv-ankur

@Fndroid its says "openai.createChatCompletion" is not a function

Make sure to use the latest version of openai (current: 3.2.1)

benunternaehrer avatar Mar 03 '23 08:03 benunternaehrer

@lucianot you should call createChatCompletion instead of createCompletion

Worked! Thanks.

lucianot avatar Mar 03 '23 13:03 lucianot

To summarize, all the issues you may have struggled with during the update to the new gpt-3.5-turbo model are as follows:

  • [x] npm update make sure you are using the latest version of OpenAI, which is 3.2.1
  • [x] this new model optimized for chat and you should call openai.createChatCompletion() instead of openai.createCompletion()
  • [x] the response now is data.choices[0].message.content not data.choices[0].text

Thanks to everyone who clarified this question above

timashoff avatar Mar 12 '23 11:03 timashoff

Still getting the error after following the recent suggestions.

BustosAndrew avatar Mar 30 '23 09:03 BustosAndrew

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

const configuration = new Configuration({ apiKey: apiKey }); it shold be work

yPrianikc avatar Apr 07 '23 22:04 yPrianikc

@yPrianikc In that case how is "openai" defined? Same error here and still doesn't work for me.

fractastical avatar May 30 '23 12:05 fractastical

You also need this line:

const openai = new OpenAIApi( new Configuration({ apiKey: process.env.OPENAI_API_KEY }) );

fractastical avatar May 30 '23 16:05 fractastical

If you still need help working with the API, give the community a try: https://community.openai.com

rattrayalex avatar Jul 10 '23 01:07 rattrayalex

you have to write it like this, the syntax has been updated for chatcompletions

const chatCompletion = await openai.chat.completions.create

shakstzy avatar Sep 17 '23 20:09 shakstzy

const chatCompletion = await openai.chat.completions.create({...})

The response structure also should be modified like below. console.log(chatCompletion .choices[0].message);

Kamilhc1125 avatar Dec 10 '23 16:12 Kamilhc1125