-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.js
317 lines (261 loc) · 10.3 KB
/
functions.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
let setting = require("dotenv").config().parsed ?? process.env;
const { Configuration, OpenAIApi } = require("openai");
const axios = require('axios');
const cheerio = require('cheerio');
const util = require("util");
const GPT3Tokenizer = require("gpt3-tokenizer");
const Chat = require("./models/Chat");
const youtubesearchapi = require("youtube-search");
const chalk = require("chalk");
function getMenu(prefix) {
return `*Whatsapp MLSC Bot*
Ask anything to AI
Cmd:${prefix}bot
Give reply
Example: ${prefix}bot what is nlp
Cmd:${prefix}meme
Give A Random meme
Example: ${prefix}meme
*(MIDJOURNEY)*
Cmd:${prefix}imagine
Give a image based on query from midjounery
Example: ${prefix}imagine sunshine days
Cmd:${prefix}video
Give a video based on query
Example: ${prefix}video give me some sunshine
*(DALL-E)*
Cmd: ${prefix}img
Create an image from text
Cmd:${prefix}freegpt
Give reply from free bing gpt(slow)
Example: ${prefix}freegpt what is nlp
`;
}
async function textDavinci003(prompt) {
if (setting.keyopenai === "ISI_APIKEY_OPENAI_DISINI") {
return;
}
if (!prompt) return `Chat with AI.\n\nExample:\n /bot What is recession`;
const configuration = new Configuration({
apiKey: setting.keyopenai,
});
const openai = new OpenAIApi(configuration);
const response = await openai.createCompletion({
model: "text-davinci-003",
prompt: prompt,
temperature: 0, // Higher values means the model will take more risks.
max_tokens: 1024, // The maximum number of tokens to generate in the completion. Most models have a context length of 2048 tokens (except for the newest models, which support 4096).
top_p: 1, // alternative to sampling with temperature, called nucleus sampling
frequency_penalty: 0.3, // Number between -2.0 and 2.0. Positive values penalize new tokens based on their existing frequency in the text so far, decreasing the model's likelihood to repeat the same line verbatim.
presence_penalty: 0 // Number between -2.0 and 2.0. Positive values penalize new tokens based on whether they appear in the text so far, increasing the model's likelihood to talk about new topics.
});
return `${response.data.choices[0].text}`;
}
async function stableDiffusionApi(prompt) {
const response = await axios.post(
'https://stablediffusionapi.com/api/v3/dreambooth',
{
'key': setting.stablediffusionkey,
'model_id': 'midjourney',
// 'prompt': prompt + ", Unreal Engine 5, Octane Render, Redshift, ((cinematic lighting)), f/1.4, ISO 200, 1/160s, 8K, RAW, unedited, symmetrical balance, in-frame",
'prompt': prompt + ",art by midjourney,cgi",
'negative_prompt': '',
'width': '512',
'height': '512',
'samples': '1',
'num_inference_steps': '20',
'seed': null,
'guidance_scale': 7.5,
'safety_checker': 'yes',
'webhook': null,
'track_id': null
},
{
headers: {
'Content-type': 'application/json'
}
}
);
var image = response["data"]["output"][0];
return image;
}
async function memeHandler(client, mek, from) {
try {
const no = Math.floor(Math.random() * 3);
if (no == 1) {
const response = await axios.post(
'https://mememandir.com/PostService.asmx/GetPostData',
new URLSearchParams({
'pageNumber': `${Math.floor(Math.random() * 10)}`,
'pageSize': '6'
}),
);
var resp = response["data"];
await client.sendMessage(
from,
{ video: { url: "https://mememandir.com/" + resp[0]["VideoLink"] }, mimetype: 'video/mp4', caption: resp[0]["Title"], }
// gifPlayback: true
);
} else if (no == 2) {
var re = await axios.get("https://meme-api.com/gimme");
re = re["data"];
client.sendImage(from, re["url"], re["title"], mek);
} else {
const memedata = await axios.get("https://memes.co.in/?random=1");
var meme = memedata["data"];
const $ = cheerio.load(meme);
var dat = $('body > main > div.infini-column > section:nth-child(1) > article > div.video-wrapper.vxplayer > div.vx_el').attr('src');
var header = $('body > main > div.infini-column > section:nth-child(1) > article > h1').text();
dat = "https://memes.co.in/" + dat;
await client.sendMessage(
from,
{ video: { url: dat }, mimetype: 'video/mp4', caption: header, }
// gifPlayback: true
);
console.log(dat + header);
}
} catch (err) {
console.log(util.format(err));
}
}
async function dalleHandler(prompt, m) {
try {
if (setting.keyopenai === "ISI_APIKEY_OPENAI_DISINI") {
return;
}
if (!prompt) return `Generate Image with AI.\n\nExample:\n /img beautiful nature`;
const configuration = new Configuration({
apiKey: setting.keyopenai,
});
const openai = new OpenAIApi(configuration);
const response = await openai.createImage({
prompt: prompt,
n: 1,
size: "512x512",
});
return response.data.data[0].url;
} catch (error) {
if (error.response) {
console.log(error.response.status);
console.log(error.response.data);
var errMsg = error.response.data.error.message;
m.reply("Sorry, there seems to be an error :" + errMsg);
} else {
console.log(error.message);
m.reply("Sorry, there seems to be an error :" + error.message);
}
}
}
async function ytApiHandler(client, text, from, m) {
try {
var opts = {
maxResults: 4,
key: setting.yt_key
};
youtubesearchapi(text, opts, function (err, results) {
if (err) return console.log(err);
// console.dir(results);
// if(results.length > 3){
// results = [results[0],results[1],results[2]];
// }
for (let i = 0; i < results.length; i++) {
const templateMessage = {
text: results[i]['title'] + "\n" + results[i]['link'],
};
const sendMsg = client.sendMessage(from, templateMessage);
}
});
}
catch (err) {
console.log(util.format(err));
m.reply("Sorry, there seems to be an error :" + err.message);
}
}
async function generateSummaryRecursively(prompt, prev_summary = "") {
splitted_prompt = prompt.split("\n");
var final_prompt = prev_summary === "" ? `Summarize the following conversation: \n` : `Summarize the following Summary of conversation:${prev_summary}\nwith further conversation:\n`;
var tokens = calculateGptTokens(final_prompt);
var remaning_prompts = [];
splitted_prompt.forEach(prompt => {
var curr_tokens = calculateGptTokens(prompt);
if ((tokens + curr_tokens) < 3000) {
tokens += curr_tokens;
final_prompt += prompt;
} else {
remaning_prompts.push(prompt);
}
});
console.log(final_prompt);
var summary = await textDavinci003(final_prompt);
if (remaning_prompts.length > 2) {
return generateSummaryRecursively(remaning_prompts.join("\n"), summary);
// return await generateSummaryRecursively(remaning_prompts.join("\n"),summary);
}
return summary;
}
function calculateGptTokens(body) {
const tokenizer = new GPT3Tokenizer.default({ type: 'gpt3' });
const encoded = tokenizer.encode(body);
const length = encoded.bpe.length;
return length;
}
async function summaryHandler(groupMetadata, m) {
try {
var participants = groupMetadata.participants;
var senderId = m.sender;
var currentSender = participants.filter(participant => {
return participant.id === senderId;
});
console.log("Current Sender:");
currentSender = currentSender[0];
console.log(currentSender);
if (!(currentSender.admin == "admin" || currentSender.admin == "superadmin")) {
m.reply(`You must be an admin to use this command`);
return;
}
const msgs = await Chat.findAll();
if (msgs.length < 2) {
m.reply("more than 2 messages should be there to continue");
return;
}
var summary = ``;
msgs.forEach(async (msg) => {
msg.body = msg.body.replaceAll("\n", "");
const dateFormat = new Date(msg.createdAt);
const timeOfMsg = formatAMPM(dateFormat);
summary += `From:'${msg.pushName}',body:'${msg.body}' \n`;
});
var chat_gpt_resp = await generateSummaryRecursively(summary);
//sending Summary to group:
m.reply(chat_gpt_resp);
//logs:
console.log(chalk.red(summary), chalk.green("\n\nGenerated Summary:" + chat_gpt_resp));
}
catch (err) {
console.log(util.format(err));
m.reply("Sorry, there seems to be an error :" + err.message);
}
}
function formatAMPM(date) {
//convert the timezone
date = new Date((typeof date === "string" ? new Date(date) : date).toLocaleString("en-US", { timeZone: 'Asia/Kolkata' }));
var hours = date.getHours();
var minutes = date.getMinutes();
var ampm = hours >= 12 ? 'pm' : 'am';
hours = hours % 12;
hours = hours ? hours : 12; // the hour '0' should be '12'
minutes = minutes < 10 ? '0' + minutes : minutes;
var strTime = hours + ':' + minutes + ' ' + ampm;
return strTime;
}
async function freeBingGPT(prompt, m) {
try {
var res = await axios('https://tempmail.theunknowwn.repl.co/gpt/' + encodeURIComponent(prompt));
var resp = res.data.text;
m.reply(resp);
} catch (err) {
console.log(util.format(err));
m.reply("Error:" + err.message)
}
}
module.exports = { formatAMPM, freeBingGPT,getMenu, textDavinci003, stableDiffusionApi, memeHandler, stableDiffusionApi, dalleHandler, ytApiHandler, summaryHandler, calculateGptTokens, generateSummaryRecursively };