forked from Zamphiropolos/Bot-Watsapp-ChatGPT
-
Notifications
You must be signed in to change notification settings - Fork 1
/
chatgpt.class.js
40 lines (31 loc) · 1022 Bytes
/
chatgpt.class.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
const {CoreClass} = require("@bot-whatsapp/bot");
class ChatGPTClass extends CoreClass {
queue = [];
optionsGPT = {model: "text-davinci-003" };
openai = undefined;
constructor(_database, _provider) {
super (null, _database, _provider);
this.init().then();
}
//Iniciando
init = async () => {
const { ChatGPTAPI} = await import ("chatgpt");
this.openai = new ChatGPTAPI ({
apiKey: process.env.OPENAI_API_KEY,
});
};
handleMsg = async (ctx) => {
const { from, body } = ctx;
const completion = await this.openai.sendMessage(body, {
conversationId: (!this.queue.length) ? undefined :this.queue[this.queue.length-1].conversationId,
parentMessageId: (!this.queue.length) ? undefined :this.queue[this.queue.length-1].id
});
this.queue.push(completion);
const parseMessage = {
...completion,
answer: completion.text,
};
this.sendFlowSimple([parseMessage], from);
};
}
module.exports = ChatGPTClass;