transitive-bullshit / agentic

AI agent stdlib that works with any LLM and TypeScript AI SDK.
https://agentic.so
MIT License
16.3k stars 2.13k forks source link

Issue with published cjs version of package #31

Closed minimum-devs closed 1 year ago

minimum-devs commented 1 year ago

wondering if i'm doing something wrong but

with version 1.3.0 installed like this:

{
    "dependencies": {
        "chatgpt": "^1.3.0" 
    }
}

and trying to import it like this:

let { ChatGPTAPI } = require("chatgpt");

i'm still getting this error:

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /var/task/node_modules/chatgpt/build/index.js require() of ES modules is not supported.

am i doing anything wrong?

GoneTone commented 1 year ago

I'm using in my Discord bot, it works fine.

DerKorb commented 1 year ago

Try installing chatgpt@1.3.0, I think the ^ operator might allow higher versions aswell

oceanlvr commented 1 year ago

use rollup. you could see my project (CJS format): https://github.com/oceanlvr/ChatGPTBot. 🌟 it if possibl :D

transitive-bullshit commented 1 year ago

Maybe I should just cave and add CJS support to main...

DerKorb commented 1 year ago

Would help me as well. I'm using nestjs and did not find a way to use the module except for the 1.3 version.

minimum-devs commented 1 year ago

@DerKorb i think you're right! totally missed that 🤦

i will try that and update this issue if that was the problem.

DerKorb commented 1 year ago

I was able to import the esm module in my nestjs project using this guide: https://jaywolfe.dev/how-to-use-es-modules-with-older-node-js-projects-the-right-way/

GoneTone commented 1 year ago

Use ESM in CommonJS

async function example() {
  // use ESM in CommonJS, dynamic import
  const { ChatGPTAPI } = await import('chatgpt')

  // sessionToken is required; see below for details
  const api = new ChatGPTAPI({ sessionToken: process.env.SESSION_TOKEN })

  // ensure the API is properly authenticated
  await api.ensureAuth()

  // send a message and wait for the response
  const response = await api.sendMessage(
    'Write a python version of bubble sort. Do not include example usage.'
  )

  // response is a markdown-formatted string
  console.log(response)
}
transitive-bullshit commented 1 year ago

Thanks for the example + PR @GoneTone 🙏