我创建了一个 Whatsapp 聊天机器人,它使用 chatGPT 在 MacOS 上的 vscode 中回答 Node.js 项目,在其文件中安装了 openai、whatsapp-web.js、dotenv 和 qrcode-terminal 的软件包,一个 .env文件有
OPENAI_API_KEY="the API key created from openai.com acccount"
以及我在index.js文件上使用的代码,即:
const { Client } = require('whatsapp-web.js');
const qrcode = require('qrcode-terminal');
const { Configuration, OpenAIApi } = require("openai");
require('dotenv').config()
const client = new Client();
client.on('qr', (qr) => {
qrcode.generate(qr, {small: true});
});
client.on('ready', () => {
console.log('Client is ready!');
});
client.initialize();
const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);
client.on('message', message => {
console.log(message.body);
if(message.body.startsWith("#")) {
runCompletion(message.body.substring(1)).then(result => message.reply(result));
}
});
async function runCompletion (message) {
const completion = await openai.createCompletion({
model: "text-davinci-003",
prompt: message,
max_tokens: 200,
});
return completion.data.choices[0].text;
}
我使用
$ node index.js
运行它,但遇到了问题。首先我遇到了一个问题
const configuration = new Configuration({
^
TypeError: Configuration is not a constructor
我通过将
const { Configuration, OpenAIApi } = require("openai");
的部分更改为: 来修复它
const Configuration = require("openai");
const OpenAIApi = require("openai");
但是使用
$ node index.js
并生成 QRCode 后,当我从手机上使用 Whatsapp Business 应用程序扫描它时,它不会打开聊天机器人,也不会使用以下部分:
client.on('ready', () => {
console.log('Client is ready!');
});
client.initialize();
const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);
我该如何修复它才能正常工作?
使用命令
node index.js
运行它并扫描生成的 QRCode 后,它会在连接到 ChatGPT 的 Whatsapp 移动应用程序上打开聊天机器人,在提出问题后,chatGPT 会提供答案。
另外,您知道另一种使用 chatGPT 创建 Whatsapp 聊天机器人项目的方法吗?
我是whatsapp-web.js的维护者,我认为你应该遵循指南,即使在假定修复了openai问题之后,你的应用程序也将无法工作