Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Dohr Zerkavod
Dohr Zerkavod
7,846 Points

Configuration is not a constructor error

I am following along with the video and I have run everything pretty smoothly up until now but when I run the code I keep getting an error in my terminal that Configuration ( the one set up from the openai package is not a valid constructor.

So I cant finish the bot...

1 Answer

Rohald van Merode
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Rohald van Merode
Treehouse Staff

Hey Dohr Zerkavod 👋

As with a lot of things in Tech these packages get updated quite frequently and it therefor can happen that one way of doing things works in version x but no longer is in version y. This also seems to be the case for the Configuration class which seems to be deprecated since the v4 update.

Based on the v3 to v4 Migration Guide you'll want to make the following changes to get things working:

Instead of importing Configuration and OpenAIApi from the openai package, import OpenAI

// Old
import { Configuration, OpenAIApi } from "openai";
// New
import OpenAI from 'openai';

Then instead of using Configuration and OpenAIApi as Dustin Usey shows in the video you'll be able to instantiate the OpenAI class in a single line:

// Old
const configuration = new Configuration({
  apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);
// New
const openai = new OpenAI({
  apiKey: process.env.OPENAI_API_KEY // This is also the default, can be omitted
});

I hope this helps to get you going again! 😃

Dohr Zerkavod
Dohr Zerkavod
7,846 Points

I didnt see you responded and researched the problem myself and only now saw your answer. thats exactly what I found. Thank you!

Dohr Zerkavod
Dohr Zerkavod
7,846 Points

Another thing to add is that .data is not a method needed to pickup on response from chatgpt Old: msg.reply(res.data.choices[0].message);

New: msg.reply(res.choices[0].message);