错误信息
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〃).",
}
}
}