llama-cpp-python-djs-bot/llamabot.js
2023-05-05 21:07:39 +02:00

340 lines
11 KiB
JavaScript

import "dotenv/config.js";
import fetch from 'node-fetch';
import { emptyResponses } from './assets/emptyMessages.js';
import { resetResponses, userResetMessages } from './assets/resetMessages.js';
import { errorMessages, busyResponses } from './assets/errorMessages.js';
import cpuStat from 'cpu-stat';
import os from 'os';
import {
Client,
GatewayIntentBits,
ActivityType,
Partials
} from 'discord.js';
const client = new Client({
intents: [
GatewayIntentBits.DirectMessages,
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildModeration,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
],
partials: [Partials.Channel],
});
// Grab ChannelIDs from the .env file
const channelIDs = process.env.CHANNEL_IDS.split(',');
const conversations = new Map();
function setBusy(userId, isBusy) {
if (conversations.has(userId)) {
conversations.get(userId).busy = isBusy;
} else {
conversations.set(userId, {
busy: isBusy
});
}
}
function isAnyConversationBusy() {
for (const conversation of conversations.values()) {
if (conversation.busy) {
setPresenceBusy()
return true;
}
}
return false;
}
function setPresenceBusy() {
client.user.setPresence({
activities: [{
name: `Processing a Request`,
type: ActivityType.Playing
}],
status: 'dnd',
});
}
function setPresenceOnline() {
client.user.setPresence({
activities: [{
name: `Ready for Request`,
type: ActivityType.Playing
}],
status: 'online',
});
}
client.once('ready', () => {
console.log('Bot is ready.');
setPresenceOnline()
});
client.on('messageCreate', async (message) => {
async function sendRand(array) {
const arrayChoice = array[Math.floor(Math.random() * array.length)];
await message.channel.send(arrayChoice); // give a notification of reset using a human like response.
}
async function sendRandDM(array) {
const arrayChoice = array[Math.floor(Math.random() * array.length)];
await message.author.send(arrayChoice); // give a notification of reset using a human like response.
}
// Only respond in the specified channels
if (!channelIDs.includes(message.channel.id)) {
return;
}
if (message.author.bot) return; // Ignore messages from bots
// Check if any conversation is busy
if (isAnyConversationBusy()) {
// Update bot presence to "Busy"
setPresenceBusy()
message.delete();
sendRandDM(busyResponses);
return;
}
const userID = message.author.id;
let conversation = conversations.get(userID) || {
messages: [],
busy: false
};
if (conversation.messages.length === 0) {
conversation.messages.push({
role: 'user',
content: ` ${process.env.INIT_PROMPT}`
});
conversation.messages.push({
role: 'user',
content: ` User name: ${message.author.username}.`
});
conversation.messages.push({
role: 'assistant',
content: ` Hello, ${message.author.username}, how may I help you?`
});
}
if (message.content === '!reset' || message.content === '!r') {
conversations.delete(userID); // Delete user's conversation map if they request reset
sendRand(userResetMessages)
return;
}
// Append user message to conversation history
conversation.messages.push({
role: 'user',
content: ` ${message.cleanContent}`
});
try {
setPresenceBusy()
setBusy(message.author.id, true);
const response = await generateResponse(conversation, message);
// Append bot message to conversation history
conversation.messages.push({
role: 'assistant',
content: response
});
if (response && response.trim()) {
// Send response to user if it's not empty
const limit = 1980;
// if we are over the discord char limit we need chunks...
if (response.length > limit) {
const chunks = response.match(new RegExp(`.{1,${limit}}`, "g"));
for (let i = 0; i < chunks.length; i++) {
setTimeout(() => {
message.channel.send(chunks[i]);
}, i * (process.env.OVERFLOW_DELAY || 3) * 1000); // delay of 3 seconds between each chunk to save on API requests
}
} else {
// We are good to go, send the response
await message.channel.send(response);
}
setPresenceOnline()
setBusy(message.author.id, false);
} else {
// Handle empty response here
sendRand(emptyResponses)
conversations.delete(userID); // Delete user's conversation map if they request reset
sendRand(resetResponses)
setPresenceOnline()
conversation.busy = false;
}
conversations.set(userID, conversation); // Update user's conversation map in memory
console.log(conversation)
} catch (err) {
console.error(err);
return sendRand(errorMessages)
} finally {
setPresenceOnline()
setBusy(message.author.id, false);
}
});
import cheerio from 'cheerio';
async function generateResponse(conversation, message) {
// Check if message contains a URL
const urlRegex = /(https?:\/\/[^\s]+)/g;
const urls = message.content.match(urlRegex);
if (urls) {
// If there are multiple URLs, process them one by one
for (const url of urls) {
try {
const res = await fetch(url);
const html = await res.text();
const $ = cheerio.load(html);
// Extract page title, meta description and content
const pageTitle = $('head title').text().trim();
const pageDescription = $('head meta[name="description"]').attr('content');
const pageContent = $('body').text().trim();
// Construct response message with page details
let response = `Title: ${pageTitle}\n`;
if (pageDescription) {
response += `Description: ${pageDescription}\n`;
}
if (pageContent) {
const MAX_CONTENT_LENGTH = process.env.MAX_CONTENT_LENGTH;
let plainTextContent = $('<div>').html(pageContent).text().trim().replace(/[\r\n\t]+/g, ' ');
const codePattern = /\/\/|\/\*|\*\/|\{|\}|\[|\]|\bfunction\b|\bclass\b|\b0x[0-9A-Fa-f]+\b|\b0b[01]+\b/;
const isCode = codePattern.test(plainTextContent);
if (isCode) {
plainTextContent = plainTextContent.replace(codePattern, '');
}
// Remove anything enclosed in brackets
plainTextContent = plainTextContent.replace(/ *\([^)]*\) */g, '');
if (plainTextContent.length > MAX_CONTENT_LENGTH) {
plainTextContent = plainTextContent.substring(0, MAX_CONTENT_LENGTH) + '...';
}
response += `Content: ${plainTextContent.trim()}`;
}
response += `URL: ${url}`;
// Get the index of the last message in the array
const lastMessageIndex = conversation.messages.length - 1;
// Append a new line and the new content to the existing content of the last message
conversation.messages[lastMessageIndex].content += "\n" + response;
console.log("A URL was provided, response: " + response)
} catch (err) {
console.error(err);
return sendRand(errorMessages);
}
}
}
const controller = new AbortController();
const timeout = setTimeout(() => {
controller.abort();
}, 900000);
const messagesCopy = [...conversation.messages]; // create a copy of the messages array
let botMessage; // define a variable to hold the message object
let time = 0
// define a function that shows the system load percentage and updates the message
const showSystemLoad = async () => {
time = Number(time) + Number(process.env.REFRESH_INTERVAL);
cpuStat.usagePercent(function (err, percent, seconds) {
if (err) {
return console.log(err);
}
const systemLoad = percent;
const freeMemory = os.freemem() / 1024 / 1024 / 1024;
const totalMemory = os.totalmem() / 1024 / 1024 / 1024;
const usedMemory = totalMemory - freeMemory;
const embedData = {
color: 0x0099ff,
title: 'Please wait.. I am thinking...',
fields: [
{
name: 'System Load',
value: `${systemLoad.toFixed(2)}%`,
},
{
name: 'Memory Usage',
value: `${usedMemory.toFixed(2)} GB / ${totalMemory.toFixed(2)} GB`,
},
{
name: 'Time',
value: `~${time} seconds.`,
},
],
};
// if the message object doesn't exist, create it
if (!botMessage) {
(async () => {
botMessage = await message.channel.send({ embeds: [embedData] });
})();
} else {
botMessage.edit({ embeds: [embedData] }); // otherwise, update the message
}
});
};
// call the function initially
await showSystemLoad();
// Grab the REFRESH_INTERVAL from ENV if not exist, lets use 7 (seconds)
const refreshInterval = setInterval(showSystemLoad, (process.env.REFRESH_INTERVAL || 7) * 1000);
try {
const response = await fetch(`http://${process.env.ROOT_IP}:${process.env.ROOT_PORT}/v1/chat/completions`, {
method: 'POST',
headers: {
'accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
messages: messagesCopy,
max_tokens: Number(process.env.MAX_TOKENS) // add the max_tokens parameter here
}),
signal: controller.signal
});
const responseData = await response.json();
console.log(JSON.stringify(responseData));
const choice = responseData.choices[0];
const responseText = choice.message.content;
// clear the interval, replace the "please wait" message with the response, and update the message
clearInterval(refreshInterval);
console.log(responseText);
botMessage.delete()
return responseText;
} catch (err) {
throw err;
} finally {
clearTimeout(timeout);
time = 0
}
}
client.login(process.env.THE_TOKEN); // Replace with your bot token