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

AI

build a ChatGPT discort bot error: configuration is not a constructor

so i get error : Configuration is not a constructor when i try to take the bot live after setting up OpenAi, the line that is failing is const configuration = new Configuration (line 17 in the vid).

Searched the web for answers but cant seem to find out whats wrong, please help!

1 Answer

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

Hey atle johansen 👋

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! 😃