openai-node
openai-node copied to clipboard
createChatCompletion method returning undefined in the choices array
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.

To Reproduce

Code snippets
No response
OS
windows
Node version
v10.19.0
Library version
3.2.1
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 you should call createChatCompletion instead of createCompletion
@Fndroid its says "openai.createChatCompletion" is not a function
@Fndroid its says "openai.createChatCompletion" is not a function
Make sure to use the latest version of openai (current: 3.2.1)
@lucianot you should call
createChatCompletioninstead ofcreateCompletion
Worked! Thanks.
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 updatemake 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 ofopenai.createCompletion() - [x] the response now is
data.choices[0].message.contentnotdata.choices[0].text
Thanks to everyone who clarified this question above
Still getting the error after following the recent suggestions.
const {Configuration, OpenAIApi} = require('openai');
const configuration = new Configuration({ apiKey: apiKey }); it shold be work
@yPrianikc In that case how is "openai" defined? Same error here and still doesn't work for me.
You also need this line:
const openai = new OpenAIApi( new Configuration({ apiKey: process.env.OPENAI_API_KEY }) );
If you still need help working with the API, give the community a try: https://community.openai.com
you have to write it like this, the syntax has been updated for chatcompletions
const chatCompletion = await openai.chat.completions.create
const chatCompletion = await openai.chat.completions.create({...})
The response structure also should be modified like below. console.log(chatCompletion .choices[0].message);