Stumble with openai key in nodejs

Hey everyone I’m beginner self-taught programmer in nodejs. I am following the code of @bradtraversy github - creating chatbot using chat gpt in openai api key.

when I tried to run npm start in my end I got the error says:
{ Configuration, OpenAIApi} from ‘openai’;

SyntaxError: The requested module ‘openai’ does not provide an export named ‘Configuration’
at async ModuleJob.run (node:internal/modules/esm/module_job:190:5)

can anyone enlighten me with this?
Appreciate you help thank you

The code you are referring use v3 of OpenAI nodeJS library. For v4, things have changed.

Before, v3

const configuration = new Configuration({
    apiKey: process.env.OPENAI_API_KEY,
})

const openai = new OpenAIApi(configuration)

Now, v4

const openai = new OpenAI({
    apiKey: process.env.OPENAI_API_KEY
})

If you are using the Chat Completion API, it is also changed

Before, v3

const result = await openai.createChatCompletion({
            messages,
            model,
            max_tokens,
            temperature,
        })

Now, v4

const result = await openai.chat.completions.create({
messages, 
model, 
temperature, 
max_tokens
})

Refer to the API reference page for more info.

1 Like