s***@163.com
s***@163.com
  • 发布:2024-10-25 14:03
  • 更新:2024-10-25 15:56
  • 阅读:87

【报Bug】云对象中require node模块,本地云函数正常,云端云函数报错

分类:uniCloud

产品分类: uniCloud/支付宝小程序云

示例代码:
//package.json  

{  
  "name": "bc-ai",  
  "dependencies": {},  
  "extensions": {  
    "uni-cloud-jql": {}  
  }  
}  

// index.obj.js,云对象代码,调用Moonshot大模型  

const OpenAI = require('openai');  

module.exports = {  
_before: function() { // 通用预处理器  
    this.client = new OpenAI({  
      apiKey: 'xxx',  
      baseURL: 'https://api.moonshot.cn/v1'  
    });  
  },  
async format(text) {  
    const completion = await this.client.chat.completions.create({  
      model: "moonshot-v1-8k",  
      messages: [{  
          role: 'system',  
          content: '你是 Kimi,由 Moonshot AI 提供的人工智能助手,你更擅长中文和英文的对话。你会为用户提供安全,有帮助,准确的回答。同时,你会拒绝一切涉及恐怖主义,种族歧视,黄色暴力等问题的回答。Moonshot AI 为专有名词,不可翻译成其他语言。'  
        },  
        {  
          role: 'user',  
          content: text  
        }  
      ],  
      temperature: 0.3,  
      response_format: {  
        type: "json_object"  
      }  
    })  

    const res = completion.choices[0].message.content;  

    return JSON.parse(res);  

  }  
}  

// index.vue 调用云对象  
<template>  
<button type="default" :loading="processing" size="default"  @click="generate()">生成</button>  
</template>  

<script setup>  

  const bcai = uniCloud.importObject('bc-ai');  
  const generate = async () => {  
     const data = await bcai.format('生成零售领域测试数据,限制:1. 20条,2. 以json格式返回');  

     console.log(data);  
}  

</script>  

操作步骤:
  1. HBuilder X -> 运行 -> 运行到浏览器 -> Edge
  2. 切换为连接云端函数
  3. 打开Edge访问 http://localhost:5173/#/
  4. 点击生成按钮
  5. 页面弹出"用户函数代码语法或逻辑异常"
  6. 浏览器控制台报错
  7. 云对象报错

预期结果:

能通过云对象调用node openai库,来调用Moonshot大模型

实际结果:

云对象报错,提示Cannot find module 'openai'

bug描述:

云对象中引入第三方node模块(openai),引入方式require('openai'),本地云函数正常,云端云函数报错。

云端错误日志如下:

Error: Cannot find module 'openai'
Require stack:

  • /var/task/code/index.obj.js
  • /var/task/code/__index.js
  • /var/task/code/index.js
    at Module._resolveFilename (node:internal/modules/cjs/loader:1077:15)
    at Module._load (node:internal/modules/cjs/loader:922:27)
    at Module.require (node:internal/modules/cjs/loader:1143:19)
    at require (node:internal/modules/cjs/helpers:119:18)
    at Object.<anonymous> (/var/task/code/index.obj.js:1:78)
    at Module._compile (node:internal/modules/cjs/loader:1256:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
    at Module.load (node:internal/modules/cjs/loader:1119:32)
    at Module._load (node:internal/modules/cjs/loader:960:12)
    at Module.require (node:internal/modules/cjs/loader:1143:19)

浏览器报错信息如下:

{
"errDetail": User Function Code Logic Error, Please Check Function Code: {\"code\":\"400\",\"message\":\"error: Cannot find module 'openai'\nRequire stack:\n- /var/task/code/index.obj.js\n- /var/task/code/index.js\n- /var/task/code/index.js. stack: Error: Cannot find module 'openai'\nRequire stack:\n- /var/task/code/index.obj.js\n- /var/task/code/index.js\n- /var/task/code/index.js\n at Module._resolveFilename (node:internal/modules/cjs/loader:1077:15)\n at Module._load (node:internal/modules/cjs/loader:922:27)\n at Module.require (node:internal/modules/cjs/loader:1143:19)\n at require (node:internal/modules/cjs/helpers:119:18)\n at Object.\u003canonymous\u003e (/var/task/code/index.obj.js:1:78)\n at Module._compile (node:internal/modules/cjs/loader:1256:14)\n at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)\n at Module.load (node:internal/modules/cjs/loader:1119:32)\n at Module._load (node:internal/modules/cjs/loader:960:12)\n at Module.require (node:internal/modules/cjs/loader:1143:19)\"}",
"errCode": "40000",
"errMsg": "用户函数代码语法或逻辑异常",
"errOrigin": "{\"code\":\"400\",\"message\":\"error: Cannot find module 'openai'\nRequire stack:\n- /var/task/code/index.obj.js\n- /var/task/code/index.js\n- /var/task/code/index.js. stack: Error: Cannot find module 'openai'\nRequire stack:\n- /var/task/code/index.obj.js\n- /var/task/code/index.js\n- /var/task/code/index.js\n at Module._resolveFilename (node:internal/modules/cjs/loader:1077:15)\n at Module._load (node:internal/modules/cjs/loader:922:27)\n at Module.require (node:internal/modules/cjs/loader:1143:19)\n at require (node:internal/modules/cjs/helpers:119:18)\n at Object.\u003canonymous\u003e (/var/task/code/index.obj.js:1:78)\n at Module._compile (node:internal/modules/cjs/loader:1256:14)\n at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)\n at Module.load (node:internal/modules/cjs/loader:1119:32)\n at Module._load (node:internal/modules/cjs/loader:960:12)\n at Module.require (node:internal/modules/cjs/loader:1143:19)\"}"
}

2024-10-25 14:03 负责人:无 分享
已邀请:
DCloud_uniCloud_CRL

DCloud_uniCloud_CRL

依赖模块在package.json文件里吗?

s***@163.com

s***@163.com (作者)

在的,在package.json的dependencies里面

要回复问题请先登录注册