-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
94 lines (80 loc) · 2.79 KB
/
index.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import { create } from 'venom-bot'
import * as dotenv from 'dotenv'
import { Configuration, OpenAIApi } from "openai"
dotenv.config()
create({
session: 'Chat-GPT',
multidevice: true
})
.then((client) => start(client))
.catch((erro) => {
console.log(erro);
});
const configuration = new Configuration({
organization: process.env.ORGANIZATION_ID,
apiKey: process.env.OPENAI_KEY,
});
const openai = new OpenAIApi(configuration);
const getDavinciResponse = async (clientText) => {
const options = {
model: "text-davinci-003", // Modelo GPT a ser usado
prompt: clientText, // Texto enviado pelo usuário
temperature: 1, // Nível de variação das respostas geradas, 1 é o máximo
max_tokens: 4000 // Quantidade de tokens (palavras) a serem retornadas pelo bot, 4000 é o máximo
}
try {
const response = await openai.createCompletion(options)
let botResponse = ""
response.data.choices.forEach(({ text }) => {
botResponse += text
})
return `${botResponse.trim()}`
} catch (e) {
return `❌ OpenAI Response Error: ${e.response.data.error.message}`
}
}
const getDalleResponse = async (clientText) => {
const options = {
prompt: clientText, // Descrição da imagem
n: 1, // Número de imagens a serem geradas
size: "1024x1024", // Tamanho da imagem
}
try {
const response = await openai.createImage(options);
return response.data.data[0].url
} catch (e) {
return `❌ OpenAI Response Error: ${e.response.data.error.message}`
}
}
const commands = (client, message) => {
if (message.from === process.env.BOT_NUMBER) return;
const iaCommands = {
dalle: "/img"
}
let firstWord = message.text.substring(0, message.text.indexOf(" "));
if (message.text.substring(0, 1) === "/") {
switch (firstWord) {
case iaCommands.dalle:
const imgDescription = message.text.substring(message.text.indexOf(" "));
getDalleResponse(imgDescription, message).then((imgUrl) => {
client.sendImage(
message.from === process.env.PHONE_NUMBER ? message.to : message.from,
imgUrl,
imgDescription,
'Imagem gerada pela IA DALL-E'
)
})
break;
default:
client.sendText(message.from, "Comando não reconhecido!")
break;
}
} else {
getDavinciResponse(message.text).then((response) => {
client.sendText(message.from, response)
})
}
}
async function start(client) {
client.onAnyMessage((message) => commands(client, message));
}