错误信息

Time: 2023-06-12 16:53:13
Request ID: d5d39439-76ae-4f12-ab80-bac3c004d257
Function: gpt-test
Content:
'require() of ES Module /app/node_modules/chatgpt/build/index.js from /app/dist/support/function-engine/cache.js not supported.\n' +
  'Instead change the require of index.js in /app/dist/support/function-engine/cache.js to a dynamic import() which is available in all CommonJS modules.' '/app/dist/support/function-engine/cache.js:80\n' +
  '        return require(module);\n' +
  '               ^\n' +
  '\n' +
  'Error [ERR_REQUIRE_ESM]: require() of ES Module /app/node_modules/chatgpt/build/index.js from /app/dist/support/function-engine/cache.js not supported.\n' +
  'Instead change the require of index.js in /app/dist/support/function-engine/cache.js to a dynamic import() which is available in all CommonJS modules.\n' +
  '    at requireFunc (/app/dist/support/function-engine/cache.js:80:16)\n' +
  '    at evalmachine.<anonymous>:4:19\n' +
  '    at Script.runInContext (node:vm:141:12)\n' +
  '    at Script.runInNewContext (node:vm:146:17)\n' +
  '    at FunctionEngine.run (/app/dist/support/function-engine/engine.js:31:40)\n' +
  '    at CloudFunction.invoke (/app/dist/support/function-engine/function.js:73:36)\n' +
  '    at handleDebugFunction (/app/dist/handler/debug-func.js:55:35)\n' +
  '    at handleInvokeFunction (/app/dist/handler/invoke-func.js:21:59)'

代码

import { ChatGPTAPI } from 'chatgpt'
import cloud from '@lafjs/cloud';

export default async function (ctx: FunctionContext) {
  console.log('Hello World')

  getOpenAIReply('你好呀')
    .then(() => { })
    .catch((e) => {
      console.log('error: ', e)
    })
  return { data: 'hi, laf' }
}

async function getOpenAIReply(question: string) {
  console.log("getOpenAIReply 执行了")


  const api = new ChatGPTAPI({
    apiKey: 'sk-xxxx'
  })


  try {
    // 如果有上下文 id,就带上
    let res = await api.sendMessage(question)
    // 返回 OpenAI 回复的内容及上下文 id
    console.log('open api response: ', res)

  } catch (e) {
    console.log(e)
    return {
      error: "问题太难了 出错了. (uДu〃).",
    }
  }

}
1楼

chatgpt是ESM模块,要换写法,改成await import,后续可能会支持esm模块

const { ChatGPTAPI } = await import(‘chatgpt’)

  • Ym9i 回复了此帖
    2楼

    juukee

    换了写法,
    有另外报错,是不是不能直接调用openai的API,了

    TypeError: fetch failed
        at Object.fetch (node:internal/deps/undici/undici:11457:11)
        at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
        at async Promise.then.promptTokens (file:///app/node_modules/chatgpt/build/index.js:298:25) {
      cause: ConnectTimeoutError: Connect Timeout Error
          at onConnectTimeout (node:internal/deps/undici/undici:8422:28)
          at node:internal/deps/undici/undici:8380:50
          at Immediate._onImmediate (node:internal/deps/undici/undici:8411:13)
          at process.processImmediate (node:internal/timers:476:21) {
        code: 'UND_ERR_CONNECT_TIMEOUT'
      }
    }
      4楼

      juukee 大佬,我一直上不去laf.dev和laf.run无论怎么弄都不行,有梯子也不行,怎么弄

        6 天 后
        1 个月 后