Refactor to Use Dependency II - The Dependency

Tom Chant
InstructorTom Chant
Share this video with your friends

Social Share Links

Send Tweet

In this lesson, we begin by installing the OpenAI dependency in the Scrimba environment. The process is straightforward and can be done with just a few clicks.

Once the dependency is installed, we import the necessary constructors, Configuration, and OpenAI API and set up instances of each.

[00:00] Okay, let's install the OpenAI dependency and in Scrimba that's dead easy. Over here on the left hand side then I'll come to the three dot menu and I've got the option to add dependency and again that is not recorded but a dialogue box

[00:16] appears and I just need to enter the dependency I want and in this case that is just going to be OpenAI all as one word and lowercase. I hit add and there we are the dependency has appeared on the left hand side so Scrimba has done all of the hard work for me in the background.

[00:34] Now if you're working outside of the Scrimba environment perhaps you're following this on YouTube or creating your own project in VS Code or any other editor we will talk about how you can work with the OpenAI dependency in just a couple of scrims time. Now that we've got the

[00:49] dependency installed we need to import two constructors from that dependency. Let's just check this code. So we're going to need configuration and the OpenAI API and they both have uppercase first letters. Now they use require here we are using import so let's just

[01:07] come up here and say import and we need configuration and OpenAI API and just note that AI here has got uppercase letters that caught me out the first time. Now we're importing them from

[01:22] OpenAI and let's just sort out my typos and the first thing that we're going to do is set up a new instance of this configuration constructor so down here I'm going to say const configuration

[01:38] with a lowercase c and I'll set that equals to a new instance of configuration and as we can see here we'll need to pass in our API key inside an object and we've actually got the code we need

[01:53] for that right here. Now we need to use this second constructor the OpenAI API so I'll come down here and I'll set up a const OpenAI and this will store a new instance of the OpenAI API and we just need to pass in the new instance of configuration that we just created. Oh and I've

[02:13] just noticed that I've made a mistake right here the AI is uppercase but API is actually camel case so we need to change that here and here. Glad I saw that before we got a strange error. Okay now we can delete a load of code so we're not going to need this URL anymore we're not going to

[02:32] need the API key saved in here we actually only used it here for a test so let's delete all of that and now in the next grim we're going to come down here to this fetch bot reply function and we're going to make a lot of changes to all of this code right here. When you're ready for that

[02:49] I'll see you there.