Compare commits

..

1 Commits

Author SHA1 Message Date
GooeyTuxedo
ba9aeaeb3e Containerize bot and server in one stack. 2023-04-16 11:33:49 -07:00
4 changed files with 13 additions and 66 deletions

View File

@ -1,6 +1,6 @@
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

View File

@ -25,4 +25,3 @@ services:
- ROOT_IP=llama-python-server
- ROOT_PORT=8000
- CACHE=1
- INIT_PROMPT: "Assistant name: ChatAI. You code, write and provide any information without any mistakes."

View File

@ -3,15 +3,8 @@ 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';
import { Client, GatewayIntentBits, ActivityType, Partials } from 'discord.js';
const client = new Client({
intents: [
@ -89,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
@ -111,7 +104,7 @@ client.on('messageCreate', async (message) => {
if (conversation.messages.length === 0) {
conversation.messages.push({
role: 'user',
content: ` ${process.env.INIT_PROMPT}`
content: ` Assistant name: ChatAI. You code, write and provide any information without any mistakes.`
});
conversation.messages.push({
role: 'user',
@ -139,7 +132,7 @@ client.on('messageCreate', async (message) => {
setPresenceBusy()
setBusy(message.author.id, true);
const response = await generateResponse(conversation, message);
const response = await generateResponse(conversation);
// Append bot message to conversation history
conversation.messages.push({
@ -170,8 +163,7 @@ client.on('messageCreate', async (message) => {
}
});
async function generateResponse(conversation, message) {
async function generateResponse(conversation) {
const controller = new AbortController();
const timeout = setTimeout(() => {
controller.abort();
@ -179,39 +171,7 @@ async function generateResponse(conversation, message) {
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 = time + 7
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;
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.`;
// 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();
// refresh the system load percentage and update the message every 7 seconds
const refreshInterval = setInterval(showSystemLoad, 7000);
console.log(conversation)
try {
const response = await fetch(`http://${process.env.ROOT_IP}:${process.env.ROOT_PORT}/v1/chat/completions`, {
@ -227,23 +187,17 @@ async function generateResponse(conversation, message) {
});
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;
// clear the interval, replace the "please wait" message with the response, and update the message
clearInterval(refreshInterval);
console.log(responseText);
botMessage.delete()
const responseText = choice.message.content
return responseText;
} catch (err) {
throw err;
} finally {
clearTimeout(timeout);
time = 0
}
}

View File

@ -12,12 +12,6 @@
"dependencies": {
"discord.js": "^14.9.0",
"dotenv": "^16.0.3",
"node-fetch": "^3.3.1",
<<<<<<< HEAD
"os": "^0.1.2",
"cpu-stat": "^2.0.1"
=======
"os": "^0.1.2"
>>>>>>> 6defc62 (Adding CPU Percentage during generation.)
"node-fetch": "^3.3.1"
}
}