llama-cpp-python-djs-bot/llamabot.js

265 lines
8.1 KiB
JavaScript
Raw Normal View History

2023-04-12 10:17:18 -04:00
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';
2023-04-16 21:17:15 -04:00
import cpuStat from 'cpu-stat';
2023-04-16 21:57:02 -04:00
import os from 'os';
2023-04-16 21:17:15 -04:00
2023-04-16 20:30:38 -04:00
import {
Client,
GatewayIntentBits,
ActivityType,
Partials
} from 'discord.js';
2023-04-09 12:07:32 -04:00
const client = new Client({
2023-04-09 19:49:51 -04:00
intents: [
GatewayIntentBits.DirectMessages,
GatewayIntentBits.Guilds,
2023-04-12 10:17:18 -04:00
GatewayIntentBits.GuildModeration,
2023-04-09 19:49:51 -04:00
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
],
partials: [Partials.Channel],
2023-04-09 12:07:32 -04:00
});
// Grab ChannelIDs from the .env file
const channelIDs = process.env.CHANNEL_IDS.split(',');
2023-04-09 12:07:32 -04:00
const conversations = new Map();
function setBusy(userId, isBusy) {
if (conversations.has(userId)) {
conversations.get(userId).busy = isBusy;
} else {
2023-04-09 22:38:38 -04:00
conversations.set(userId, {
busy: isBusy
});
}
}
function isAnyConversationBusy() {
for (const conversation of conversations.values()) {
if (conversation.busy) {
setPresenceBusy()
return true;
}
}
return false;
}
function setPresenceBusy() {
2023-04-09 19:49:51 -04:00
client.user.setPresence({
activities: [{
name: `Processing a Request`,
2023-04-09 19:49:51 -04:00
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.');
2023-04-09 22:38:38 -04:00
setPresenceOnline()
2023-04-09 12:07:32 -04:00
});
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.
}
2023-04-09 19:49:51 -04:00
// Only respond in the specified channels
if (!channelIDs.includes(message.channel.id)) {
return;
2023-04-09 19:49:51 -04:00
}
2023-04-09 19:49:51 -04:00
if (message.author.bot) return; // Ignore messages from bots
2023-04-09 12:07:32 -04:00
// Check if any conversation is busy
if (isAnyConversationBusy()) {
// Update bot presence to "Busy"
setPresenceBusy()
message.delete();
sendRandDM(busyResponses);
2023-04-09 19:49:51 -04:00
return;
}
2023-04-09 19:49:51 -04:00
const userID = message.author.id;
let conversation = conversations.get(userID) || {
messages: [],
busy: false
};
if (conversation.messages.length === 0) {
conversation.messages.push({
role: 'user',
2023-04-16 18:50:04 -04:00
content: ` ${process.env.INIT_PROMPT}`
2023-04-09 19:49:51 -04:00
});
conversation.messages.push({
role: 'user',
2023-04-16 08:47:04 -04:00
content: ` User name: ${message.author.username}.`
2023-04-09 19:49:51 -04:00
});
conversation.messages.push({
role: 'assistant',
2023-04-16 08:47:04 -04:00
content: ` Hello, ${message.author.username}, how may I help you?`
2023-04-09 19:49:51 -04:00
});
}
2023-04-09 19:49:51 -04:00
if (message.content === '!reset' || message.content === '!r') {
conversations.delete(userID); // Delete user's conversation map if they request reset
sendRand(userResetMessages)
2023-04-09 19:49:51 -04:00
return;
}
2023-04-09 12:07:32 -04:00
// Append user message to conversation history
2023-04-09 19:49:51 -04:00
conversation.messages.push({
role: 'user',
content: ` ${message.cleanContent}`
2023-04-09 19:49:51 -04:00
});
2023-04-09 12:07:32 -04:00
2023-04-09 19:49:51 -04:00
try {
setPresenceBusy()
setBusy(message.author.id, true);
2023-04-09 19:49:51 -04:00
const response = await generateResponse(conversation, message);
2023-04-09 19:49:51 -04:00
// 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 * 3000); // 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);
2023-04-09 19:49:51 -04:00
} else {
// Handle empty response here
sendRand(emptyResponses)
2023-04-09 19:49:51 -04:00
conversations.delete(userID); // Delete user's conversation map if they request reset
2023-04-09 23:18:15 -04:00
sendRand(resetResponses)
setPresenceOnline()
2023-04-09 19:49:51 -04:00
conversation.busy = false;
}
conversations.set(userID, conversation); // Update user's conversation map in memory
} catch (err) {
console.error(err);
sendRand(errorMessages)
} finally {
setPresenceOnline()
setBusy(message.author.id, false);
}
2023-04-09 12:07:32 -04:00
});
async function generateResponse(conversation, message) {
2023-04-09 19:49:51 -04:00
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
2023-04-16 21:56:09 -04:00
let time = 0
// define a function that shows the system load percentage and updates the message
const showSystemLoad = async () => {
2023-04-16 21:56:09 -04:00
time = time + 7
2023-04-16 21:17:15 -04:00
cpuStat.usagePercent(function(err, percent, seconds) {
if (err) {
return console.log(err);
}
const systemLoad = percent //the percentage cpu usage over all cores
const freeMemory = os.freemem() / 1024 / 1024 / 1024;
const totalMemory = os.totalmem() / 1024 / 1024 / 1024;
const usedMemory = totalMemory - freeMemory;
2023-04-16 22:02:16 -04:00
const messageData = `Please wait, I am thinking...\nSystem Load: ${systemLoad.toFixed(2)}%\nMemory Usage: ${usedMemory.toFixed(2)} GB / ${totalMemory.toFixed(2)} GB | Time: ~${time} seconds.`;
2023-04-16 21:17:15 -04:00
// if the message object doesn't exist, create it
if (!botMessage) {
(async () => {
botMessage = await message.channel.send(messageData);
})()
} else {
botMessage.edit(messageData); // otherwise, update the message
}
})
};
// call the function initially
await showSystemLoad();
2023-04-16 20:30:38 -04:00
// refresh the system load percentage and update the message every 7 seconds
const refreshInterval = setInterval(showSystemLoad, 7000);
2023-04-11 11:38:47 -04:00
2023-04-09 19:49:51 -04:00
try {
const response = await fetch(`http://${process.env.ROOT_IP}:${process.env.ROOT_PORT}/v1/chat/completions`, {
2023-04-09 19:49:51 -04:00
method: 'POST',
headers: {
2023-04-26 20:56:27 -04:00
'accept': 'application/json',
'Content-Type': 'application/json'
2023-04-09 19:49:51 -04:00
},
body: JSON.stringify({
2023-04-26 20:56:27 -04:00
messages: messagesCopy,
2023-04-26 21:42:52 -04:00
max_tokens: Number(process.env.MAX_TOKENS) // add the max_tokens parameter here
2023-04-09 19:49:51 -04:00
}),
signal: controller.signal
2023-04-26 20:56:27 -04:00
});
2023-04-09 19:49:51 -04:00
const responseData = await response.json();
console.log(JSON.stringify(responseData));
2023-04-09 19:49:51 -04:00
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()
2023-04-16 10:22:57 -04:00
return responseText;
2023-04-09 19:49:51 -04:00
} catch (err) {
throw err;
2023-04-09 19:49:51 -04:00
} finally {
clearTimeout(timeout);
2023-04-16 21:56:09 -04:00
time = 0
2023-04-09 19:49:51 -04:00
}
2023-04-09 12:07:32 -04:00
}
2023-04-09 19:49:51 -04:00
client.login(process.env.THE_TOKEN); // Replace with your bot token