Hi. I am doing the AI Engineer course. I am getting
TypeError: OpenAI is not a constructor
I’m pretty sure this is because my key is not being picked up correctly (it is saved in my environment settings). I know the code and the key do work as I have recreated the project exactly in VScode / node.js and it runs and gives me a reply from OpenAI. I did have a key saved in scrimba env before that was not valid, but I pasted the new key in its place and pressed save, and I can see it is now correct - could that be part of the problem?
The relevant part of my code looks like this. As per the tutorial, I shouldn’t need to include any reference to my key, right? It just gets picked up automatically?
import OpenAI from 'openai'
const openai = new OpenAI({
dangerouslyAllowBrowser: true
})
But I think there’s some other bug going on, because sometimes the thing seems to hang, and the error message doesn’t change whatever gibberish I write in the code, and then a full minute later an error pops up on the bottom of the screen for a second or two, saying something like
DOCTYPE is not valid JSON
or something that suggests to me it is something about the scrimba browser that isn’t working right.
@Tom_Chant are you able to help me solve this? For now I have resorted to copy-pasting in to VScode and running the exercises locally in node, but obviously I would prefer to do it all within your lovely platform.
Hi @AndreaBarghigiani. So I have now swapped one problem for another. I realised I had not given my key the correct name in the scrimba env. It needs to be OPENAI_API_KEY. Now that I’ve corrected that, I am getting a new error which is
TypeError: Failed to resolve module specifier "openai". Relative references must start with either "/", "./", or "../"
So it seems that the path to openai is not right. This only happens if I have the openai dependency installed. If I remove it, I get the same “not a constructor” error as before. If I put a slash before ‘openai’, I get
I’ve just tried the same thing in Safari (was previously using Chrome). In Safari, instead of the “failed to resolve module” error, I get the error message
Error: Cannot parse given Error object
If I delete the dependency I get no error message in Safari (but nothing happens.)
EDIT: @roku I fixed it! It occured to me it might be the version of the OpenAI dependency that was the problem, and I found another post from you elsewhere in the forum asking someone to try openai@4.14.2. I deleted the current package and tried that instead, and it works now.
You might want to pin this advice somewhere? I would have thought there must be a lot of people having this same problem, if the latest OpenAI package, which Scrimba installs as a dependency automatically when you type “openai” in dependenices, doesn’t work with Scrimba?