我正在尝试使用 Twilio 和 Firebase 功能创建一个 whatsapp/openai 聊天“应用程序”。我正在处理这个人的项目video
当我尝试将项目部署到 firebase 函数时,出现以下错误。
Error: Failed to load function definition from source: Failed to generate manifest from function source: ReferenceError: require is not defined in ES module scope,你可以改用import
这是我要部署的代码:
const functions = require("firebase-functions");
import {
Configuration,
OpenAIApi
} from "openai";
const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});
const openAI = new OpenAIApi(configuration);
export default async function handler(req, res) {
const MessagingResponse = require('twilio').twiml.MessagingResponse;
var messageResponse = new MessagingResponse();
const sentMessage = req.body.Body || '';
let replyToBeSent = "";
if (sentMessage.trim().length === 0) {
replyToBeSent = "We could not get your message. Please try again";
} else {
try {
const completion = await openAI.createCompletion({
model: "text-davinci-003", // required
prompt: req.body.Body, // completion based on this
temperature: 0.6, //
n: 1,
max_tokens: 50,
// stop: "."
});
replyToBeSent = removeIncompleteText(completion.data.choices[0].text)
} catch (error) {
if (error.response) {
replyToBeSent = "There was an issue with the server"
} else { // error getting response
replyToBeSent = "An error occurred during your request.";
}
}
}
messageResponse.message(replyToBeSent);
// send response
res.writeHead(200, {
'Content-Type': 'text/xml'
});
res.end(messageResponse.toString());
}
//trims out the words/char that may be left hanging by chatgpt because the character(token) limit is reached for a response
function removeIncompleteText(inputString) {
const match = inputString.match(/\b\.\s\d+/g);
const removeAfter = match ? inputString.slice(0, inputString.lastIndexOf(match[match.length - 1])) : inputString;
return removeAfter
}
这是 package.json 文件
{
"name": "appName",
"version": "1.0.0",
"description": "",
"type": "module",
"scripts": {
"lint": "eslint .",
"serve": "firebase emulators:start --only functions",
"shell": "firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log",
"test": "echo \"Error: no test specified\" && exit 1"
},
"engines": {
"node": "16"
},
"main": "index.js",
"dependencies": {
"firebase-admin": "^11.5.0",
"firebase-functions": "^4.2.0",
"next": "^13.2.4",
"openai": "^3.2.1",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"twilio": "^4.9.0"
},
"devDependencies": {
"eslint": "^8.15.0",
"eslint-config-google": "^0.14.0",
"firebase-functions-test": "^3.0.0"
},
"private": true,
"keywords": [],
"author": "",
"license": "ISC"
}
当我从包 json 文件中删除“类型”:“模块”时,出现以下错误。
错误:无法从源加载函数定义:无法从函数源生成清单:语法错误:无法在模块外使用导入语句
我不确定 cannot import statement outside a module 是什么意思,但我已将 openAi 模块文件移动到 node_modules 文件夹,但我仍然收到错误消息。
我改变了:
import {Configuration, OpenAIApi} from "openai";
改为
const { Configuration, OpenAIApi } = require('openai');
.....也变了
export default async function handler(req, res) {
到这条线
exports.messages = functions.https.onRequest( async (req, res) => {