skip to Main Content

I have a rather simple NodeJS project, which accesses ChatGPT by installing and using the npm package named ‘openai’. The problem is that although I can do this in my index.js file:

import {Configuration, OpenAIApi} from 'openai';

Everything works fine, including getting responses back from ChatGPT. However, when I move my code into an ES6 module that defines a class, which I import in my index.js, the ES6 module contains the exact same import statement, but my script fails with the error message:

SyntaxError: The requested module 'openai' does not provide an export named 'Con
figuration'

I don’t even know how that’s possible.

NOTE: I just found that even when putting that import statement in my main script, I still get this error message. I’m absolutely certain that it previously worked from the main script, index.js (generated by the coffee command from index.coffee). But now I’m not sure that it’s relevant whether the import statement appears in the main script or the imported module file.

A few points:

  1. My package.json file includes "type": "module"
  2. I’m actually using CoffeeScript 2.7.0 to develop the scripts, but then I use the coffee command to generate *.js files, which is what I actually execute. I prefer that over trying to execute the coffee files directly. When I import the class I’m creating, I import from the *.js file.

Here are my source CoffeeScript files, first index.coffee:

# index.coffee

import {Chat} from './Chat.js'

chat = new Chat({"echo": true})

await say("Suggest a name for a large black cat")
await say("What if the cat were small and white?")

Then Chat.coffee:

# Chat.coffee

import dotenv from 'dotenv'
import {Configuration, OpenAIApi} from 'openai'

dotenv.config()  # read env vars from .env into the current process

openai = new OpenAIApi(new Configuration({
    apiKey: process.env.API_KEY
    }))

# --- Create a synonym for console.log
LOG = (str) =>
    console.log str

# ---------------------------------------------------------------------------

export class Chat

    constructor: (hOptions={}) ->
        # --- Valid options:
        #        'echo' - causes chat to be logged to console
        #        'model' - name of the ChatGPT model to use
        #        'temperature'
        @setOptions(hOptions)
        @lChat = []

    # ..........................................................

    setOptions: (hOptions) ->

        @echo = hOptions.echo
        @model = hOptions.model || 'gpt-3.5-turbo'
        @temp = hOptions.temperature || 0.6
        return

    # ..........................................................

    say: (str) ->

        if @echo
            LOG "Q: #{str}"

        # --- Add the sentence to the current chat
        @lChat.push {
            role: 'user'
            content: str
            }

        resp = await openai.createChatCompletion({
            model: @model
            messages: lChat
            temperature: @temp
            })
        {role, content} = resp.data.choices[0].message

        if @echo
            LOG "A: #{content}"

        # --- Add the response to the current chat
        @lChat.push {role, content}
        return content

For completeness, here is the output that resulted back when the import statement was accepted:

Q: Suggest a name for a large black cat
A: Midnight
Q: What if the cat were small and white?
A: Snowball

I think it’s pretty slick how it remembers what we were talking about when the second question is asked.

2

Answers


  1. Chosen as BEST ANSWER

    There is a bug in my code. I'll explain it, but though my code now works, I have no idea how this bug could result in the error message I showed.

    In the function named say, there's this:

    messages: lChat
    

    which should be this, because lChat is an object property:

    messages: @lChat
    

    Since I rebuilt everything, I'm not sure if things are working now due to this bug fix or not, but on the surface, at least, it appears totally unrelated to whether a particular symbol is available for import. Also, it's a run time error, so it's not like the *.coffee to *.js file conversion would have failed.


  2. OK, I now found the REAL reason for the error message I was getting. In my package.json file, I had this:

    "name": "openai",
    

    So, I was naming my project the same as one of the packages that I installed. So, when it said that "The requested module ‘openai’ does not provide an export named ‘Configuration’", it wasn’t referring to the installed ‘openai’ package, it was referring to my new project. Really, NodeJS should not allow me to name my project the same as any of the packages I install, but in the end, it was my mistake.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search