-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
395 lines (328 loc) · 14.2 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
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
const { Client, LocalAuth, MessageMedia } = require('whatsapp-web.js');
const { exec, spawn } = require('child_process');
const fs = require('fs');
const sqlite3 = require('sqlite3').verbose();
const db = new sqlite3.Database('./chats.db');
const OpenAI = require('openai');
const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
// get the system prompt from environment variable
const SYSTEM_PROMPT = process.env.SYSTEM_PROMPT || `You are an autoregressive language model that has been fine-tuned with instruction-tuning and RLHF. You carefully provide accurate, factual, thoughtful, nuanced answers, and are brilliant at reasoning. If you think there might not be a correct answer, you say so. Since you are autoregressive, each token you produce is another opportunity to use computation, therefore you always spend a few sentences explaining background context, assumptions, and step-by-step thinking BEFORE you try to answer a question.
Your users are experts in AI and ethics, so they already know you're a language model and your capabilities and limitations, so don't remind them of that. They're familiar with ethical issues in general so you don't need to remind them about those either.`
// Initialize the database tables
db.run(/*sql*/`
CREATE TABLE IF NOT EXISTS chats (
chat_id varchar(20) PRIMARY KEY,
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP
);
`);
db.run(/*sql*/`
CREATE TABLE IF NOT EXISTS messages (
message_id INTEGER PRIMARY KEY,
chat_id varchar(20) REFERENCES chats(chat_id),
sender_id varchar(20),
message TEXT,
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP
);
`);
const sql = (strings, ...values) => new Promise((resolve, reject) => {
const query = String.raw(strings, ...values.map(_ => '?')); // Use '?' as placeholder
db.all(query, values, (err, rows) => {
if (err) return reject(err);
resolve(rows);
});
});
const client = new Client({
authStrategy: new LocalAuth(),
puppeteer: {
headless: false,
executablePath: '/opt/google/chrome/chrome',
},
});
// Event listener for when the QR code is received for scanning
client.on('qr', (qr) => {
exec(`echo "${qr}" | qrencode -t ansiutf8`, (error, stdout, stderr) => {
if (error) {
console.error(error);
console.error(stderr);
return;
}
// Print the QR code in the terminal
console.log(stdout);
})
});
client.on('ready', () => {
console.log('Client is ready');
});
client.on('authenticated', (session) => {
console.log('Client is authenticated');
});
client.on('message', async (message) => {
if (!message.body) return;
if (message.body.startsWith('!')) {
const [rawCommand, ...args] = message.body.slice(1).split(' ');
const command = rawCommand.toLowerCase();
const action = ACTIONS_INDEXED_BY_COMMAND[command];
if (!action) {
message.reply(`Unknown command: ${command}`);
return;
}
return action.fn(args, message);
}
const contact = await message.getContact();
const senderName = contact.pushname || contact.name || contact.id.user;
const selfId = client.info.wid.user;
const selfName = client.info.pushname || client.info.name || selfId;
const chat = await message.getChat();
const chatId = message.from;
// insert chatId into DB if not already present
await sql`INSERT OR IGNORE INTO chats(chat_id) VALUES (${chatId});`;
let messageBody_formatted, systemPrompt;
if (chat.isGroup) {
// Call shouldRespond to determine if the bot should process this message
if (!await shouldRespond(message, selfId)) {
return;
}
// Remove mention of self from start of message
const mentionPattern = new RegExp(`^@${selfId} *`, 'g');
message.body = message.body.replace(mentionPattern, '');
const modifiedMessage = await replaceMentionsWithNames(message);
// prepend name of sender to prompt
messageBody_formatted = `${senderName}: ${modifiedMessage}`;
systemPrompt = SYSTEM_PROMPT + `\n\nYou are a brilliant AI assistant called ${selfName}.\nYou are in a group chat called "${chat.name}"`;
} else {
messageBody_formatted = await replaceMentionsWithNames(message);
systemPrompt = SYSTEM_PROMPT + `\n\nYou are a brilliant AI assistant called ${selfName}`;
}
// insert message into DB
await sql`INSERT INTO messages(chat_id, message, sender_id) VALUES (${chatId}, ${messageBody_formatted}, ${contact.id.user});`;
// obtain latest messages from DB
const chatMessages = await sql`SELECT message, sender_id FROM messages WHERE chat_id = ${chatId} ORDER BY timestamp DESC LIMIT 20;`;
// prepare messages for OpenAI
const chatMessages_formatted = chatMessages.filter(x=>x.message).map(({message, sender_id}) => {
return {
role: sender_id === selfId ? 'assistant' : 'user',
content: message,
}
}).reverse();
// call OpenAI
const response = await openai.chat.completions.create({
model: "gpt-3.5-turbo",
messages: [{role: "system", content: systemPrompt}, ...chatMessages_formatted],
functions: actions_openAI_formatted,
function_call: "auto",
});
const responseMessage = response.choices[0].message;
if (responseMessage.content) {
await sql`INSERT INTO messages(chat_id, message, sender_id) VALUES (${chatId}, ${responseMessage.content}, ${selfId});`;
message.reply(responseMessage.content);
}
if (responseMessage.function_call) {
const functionName = responseMessage.function_call.name;
const functionToCall = FUNCTIONS_INDEXED_BY_NAME[functionName];
const functionArgs = JSON.parse(responseMessage.function_call.arguments);
console.log("executing", functionName, functionArgs);
const functionResponse = await functionToCall(functionArgs, message);
console.log("response", functionResponse);
if (functionResponse) {
// insert into DB
await sql`INSERT INTO messages(chat_id, message, sender_id) VALUES (${chatId}, ${functionResponse}, ${selfId});`;
chat.sendMessage(functionResponse);
}
}
});
client.initialize();
async function shouldRespond (message, selfId) {
// Respond if I have been mentioned
if (message.mentionedIds.some(contactId => contactId.startsWith(selfId))) {
return true;
}
// Respond if I have been quoted
if (message.hasQuotedMsg) {
const quotedMsg = await message.getQuotedMessage();
const quotedContact = await quotedMsg.getContact();
if (quotedContact.id.user === selfId) {
return true;
}
}
return false;
};
async function replaceMentionsWithNames (message) {
let modifiedMessage = message.body;
const mentionedContacts = await message.getMentions();
for (const contact of mentionedContacts) {
const contactId = contact.id.user;
const contactName = contact.pushname || contact.name || contactId;
const mentionPattern = new RegExp(`@${contactId}`, 'g');
modifiedMessage = modifiedMessage.replace(mentionPattern, `@${contactName}`);
}
return modifiedMessage;
};
const ACTIONS = [
{
name: "download_video",
command: "video",
description: "Download video from URL, and send it to chat",
parameters: {
type: "object",
properties: {
url: {
type: "string",
description: "URL of the video to download",
},
},
required: ["url"],
},
fn: function (args, message) {
// check if args is an array
let url;
if (Array.isArray(args)) {
([url] = args);
} else {
// asume is an object
({url} = args);
}
return new Promise((resolve, reject) => {
const ytdlProcess = spawn('yt-dlp', ['-o', "/dev/shm/%(title)s.%(ext)s", url]);
ytdlProcess.on('error', (error) => {
console.error(`Error spawning yt-dlp: ${error}`);
message.reply('Failed to start the download.');
});
let stdoutData = '';
ytdlProcess.stdout.on('data', (data) => {
const stdoutString = data.toString();
stdoutData += stdoutString;
console.log(`yt-dlp stdout: ${stdoutString}`);
});
ytdlProcess.stderr.on('data', (data) => {
console.error(`yt-dlp stderr: ${data}`);
});
ytdlProcess.on('close', async (code) => {
if (code !== 0) {
message.reply('Download failed.');
return;
}
// Extract filename from stdout data
const downloadedFilepath =
stdoutData.match(/Merging formats into "([^"]+)"/)?.at(1)
|| stdoutData.match(/\[download\] (.+?) has already been downloaded/)?.at(1)
|| stdoutData.match(/\[download\] Destination: (.+?)\n/)?.at(1)
|| null;
const convertedFilepath = `${downloadedFilepath}.mp4`;
// Start FFmpeg to convert video to mp4
const ffmpegProcess = spawn('ffmpeg', ['-i', downloadedFilepath,
"-vf", "scale='bitand(oh*dar,65534)':'min(720,ih)'",
"-vf", "pad=ceil(iw/2)*2:ceil(ih/2)*2",
"-c:v", "libx264",
"-pix_fmt", "yuv420p",
"-profile:v", "baseline",
"-level", "3.0",
"-y",
convertedFilepath
]);
ffmpegProcess.on('error', (error) => {
console.error(`Error spawning FFmpeg: ${error}`);
message.reply('Failed to convert the video.');
});
ffmpegProcess.stdout.on('data', (data) => {
console.log(`FFmpeg stdout: ${data}`);
});
ffmpegProcess.stderr.on('data', (data) => {
console.error(`FFmpeg stderr: ${data}`);
});
ffmpegProcess.on('close', async (code) => {
if (code !== 0) {
message.reply('Conversion failed.');
return;
}
try {
const media = MessageMedia.fromFilePath(convertedFilepath);
await message.reply(media);
fs.unlinkSync(convertedFilepath);
// fs.unlinkSync(downloadedFilepath)
} catch (error) {
console.error(error);
message.reply('An error occurred while processing the video.');
return;
}
resolve()
});
});
});
}
},{
name: "download_audio",
command: "audio",
description: "Download audio from URL, and send it to chat",
parameters: {
type: "object",
properties: {
url: {
type: "string",
description: "URL of the audio to download",
},
},
required: ["url"],
},
fn: function (args, message) {
// check if args is an array
let url;
if (Array.isArray(args)) {
([url] = args);
} else {
// asume is an object
({url} = args);
}
return new Promise((resolve, reject) => {
const ytdlProcess = spawn('yt-dlp', ['-x', '-o', "/dev/shm/%(title)s.%(ext)s", url]);
ytdlProcess.on('error', (error) => {
console.error(`Error spawning yt-dlp: ${error}`);
message.reply('Failed to start the download.');
});
let stdoutData = '';
ytdlProcess.stdout.on('data', (data) => {
const stdoutString = data.toString();
stdoutData += stdoutString;
console.log(`yt-dlp stdout: ${stdoutString}`);
});
ytdlProcess.stderr.on('data', (data) => {
console.error(`yt-dlp stderr: ${data}`);
});
ytdlProcess.on('close', async (code) => {
if (code !== 0) {
message.reply('Download failed.');
return;
}
// Extract filename from stdout data
const downloadedFilepath =
stdoutData.match(/\[ExtractAudio\] Destination: (.+?)\n/)?.at(1)
|| stdoutData.match(/Destination: (.+?)\n/)?.at(1)
|| null;
console.log({ downloadedFilepath, stdoutData });
try {
const media = MessageMedia.fromFilePath(downloadedFilepath);
await message.reply(media);
// fs.unlinkSync(downloadedFilepath);
} catch (error) {
console.error(error);
message.reply('An error occurred while processing the audio.');
return;
}
resolve()
});
});
}
}
]
const FUNCTIONS_INDEXED_BY_NAME = {}
const ACTIONS_INDEXED_BY_COMMAND = {}
ACTIONS.forEach(action => {
FUNCTIONS_INDEXED_BY_NAME[action.name] = action.fn;
ACTIONS_INDEXED_BY_COMMAND[action.command] = action;
});
const actions_openAI_formatted = ACTIONS.map(({name, description, parameters}) => {
return {
name,
description,
parameters,
}
})