Adding CPU Percentage during generation.

This commit is contained in:
Raven Scott 2023-04-17 02:07:41 +02:00
parent c2396f7e5d
commit 4ff67ff28b
3 changed files with 41 additions and 12 deletions

View File

@ -1,8 +1,11 @@
THE_TOKEN = "DISCORD_TOKEN_HERE" THE_TOKEN = "DISCORD_TOKEN_HERE"
CHANNEL_IDS = 1094494101631680653,1094628334727614605 CHANNEL_IDS = 1094494101631680653,1094628334727614605
INIT_PROMPT = "Assistant name: ChatAI. You code, write and provide any information without any mistakes." 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 ROOT_PORT = 8000
DATA_DIR = /home/USERNAME/weights DATA_DIR = /home/USERNAME/weights
CACHE = 1 CACHE = 1
# ROOT_IP is only used when running the bot without docker compose
ROOT_IP = 192.168.0.15

View File

@ -3,7 +3,7 @@ import fetch from 'node-fetch';
import { emptyResponses } from './assets/emptyMessages.js'; import { emptyResponses } from './assets/emptyMessages.js';
import { resetResponses, userResetMessages } from './assets/resetMessages.js'; import { resetResponses, userResetMessages } from './assets/resetMessages.js';
import { errorMessages, busyResponses } from './assets/errorMessages.js'; import { errorMessages, busyResponses } from './assets/errorMessages.js';
import os from 'os';
import { Client, GatewayIntentBits, ActivityType, Partials } from 'discord.js'; import { Client, GatewayIntentBits, ActivityType, Partials } from 'discord.js';
const client = new Client({ const client = new Client({
@ -82,7 +82,7 @@ client.on('messageCreate', async (message) => {
// Only respond in the specified channels // Only respond in the specified channels
if (!channelIDs.includes(message.channel.id)) { if (!channelIDs.includes(message.channel.id)) {
return; return;
} }
if (message.author.bot) return; // Ignore messages from bots if (message.author.bot) return; // Ignore messages from bots
@ -132,7 +132,7 @@ client.on('messageCreate', async (message) => {
setPresenceBusy() setPresenceBusy()
setBusy(message.author.id, true); setBusy(message.author.id, true);
const response = await generateResponse(conversation); const response = await generateResponse(conversation, message);
// Append bot message to conversation history // Append bot message to conversation history
conversation.messages.push({ 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 controller = new AbortController();
const timeout = setTimeout(() => { const timeout = setTimeout(() => {
controller.abort(); controller.abort();
@ -171,7 +172,26 @@ async function generateResponse(conversation) {
const messagesCopy = [...conversation.messages]; // create a copy of the messages array 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 { try {
const response = await fetch(`http://${process.env.ROOT_IP}:${process.env.ROOT_PORT}/v1/chat/completions`, { 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(); const responseData = await response.json();
console.log(JSON.stringify(responseData)) console.log(JSON.stringify(responseData));
const choice = responseData.choices[0]; 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; return responseText;
} catch (err) { } catch (err) {
throw err; throw err;
} finally { } finally {

View File

@ -12,6 +12,7 @@
"dependencies": { "dependencies": {
"discord.js": "^14.9.0", "discord.js": "^14.9.0",
"dotenv": "^16.0.3", "dotenv": "^16.0.3",
"node-fetch": "^3.3.1" "node-fetch": "^3.3.1",
"os": "^0.1.2"
} }
} }