diff --git a/default.env b/default.env index 5a2278a..6e7aa55 100644 --- a/default.env +++ b/default.env @@ -1,8 +1,11 @@ THE_TOKEN = "DISCORD_TOKEN_HERE" CHANNEL_IDS = 1094494101631680653,1094628334727614605 INIT_PROMPT = "Assistant name: ChatAI. You code, write and provide any information without any mistakes." -# ROOT_IP is only used when running the bot without docker compose -ROOT_IP = 192.168.0.15 ROOT_PORT = 8000 DATA_DIR = /home/USERNAME/weights CACHE = 1 + +# ROOT_IP is only used when running the bot without docker compose +ROOT_IP = 192.168.0.15 + + diff --git a/llamabot.js b/llamabot.js index 286eca8..deda2a4 100644 --- a/llamabot.js +++ b/llamabot.js @@ -3,7 +3,7 @@ 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 os from 'os'; import { Client, GatewayIntentBits, ActivityType, Partials } from 'discord.js'; const client = new Client({ @@ -82,7 +82,7 @@ client.on('messageCreate', async (message) => { // Only respond in the specified channels if (!channelIDs.includes(message.channel.id)) { - return; + return; } if (message.author.bot) return; // Ignore messages from bots @@ -132,7 +132,7 @@ client.on('messageCreate', async (message) => { setPresenceBusy() setBusy(message.author.id, true); - const response = await generateResponse(conversation); + const response = await generateResponse(conversation, message); // Append bot message to conversation history conversation.messages.push({ @@ -163,7 +163,8 @@ client.on('messageCreate', async (message) => { } }); -async function generateResponse(conversation) { + +async function generateResponse(conversation, message) { const controller = new AbortController(); const timeout = setTimeout(() => { controller.abort(); @@ -171,7 +172,26 @@ async function generateResponse(conversation) { const messagesCopy = [...conversation.messages]; // create a copy of the messages array - console.log(conversation) + let botMessage; // define a variable to hold the message object + + // define a function that shows the system load percentage and updates the message + const showSystemLoad = async () => { + const systemLoad = os.loadavg()[0] / os.cpus().length * 100; + const messageData = `Please wait, I am thinking... System Load: ${systemLoad.toFixed(2)}%`; + + // if the message object doesn't exist, create it + if (!botMessage) { + botMessage = await message.channel.send(messageData); + } else { + botMessage.edit(messageData); // otherwise, update the message + } + }; + + // call the function initially + await showSystemLoad(); + + // refresh the system load percentage and update the message every 5 seconds + const refreshInterval = setInterval(showSystemLoad, 5000); try { const response = await fetch(`http://${process.env.ROOT_IP}:${process.env.ROOT_PORT}/v1/chat/completions`, { @@ -187,13 +207,18 @@ async function generateResponse(conversation) { }); const responseData = await response.json(); - console.log(JSON.stringify(responseData)) + console.log(JSON.stringify(responseData)); const choice = responseData.choices[0]; - const responseText = choice.message.content - + 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 { diff --git a/package.json b/package.json index dbe4318..0deb1bd 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,7 @@ "dependencies": { "discord.js": "^14.9.0", "dotenv": "^16.0.3", - "node-fetch": "^3.3.1" + "node-fetch": "^3.3.1", + "os": "^0.1.2" } }