Compare commits
10 Commits
ba9aeaeb3e
...
21690c48c1
Author | SHA1 | Date | |
---|---|---|---|
|
21690c48c1 | ||
|
c061e0d7ab | ||
|
a854079e3c | ||
|
c97e525d45 | ||
|
3806892349 | ||
|
b72875e744 | ||
|
fffef84119 | ||
|
4e69329501 | ||
|
4ff67ff28b | ||
|
c2396f7e5d |
@ -1,6 +1,6 @@
|
||||
THE_TOKEN = "DISCORD_TOKEN_HERE"
|
||||
CHANNEL_IDS = 1094494101631680653,1094628334727614605
|
||||
# ROOT_IP is only used when running the bot without docker compose
|
||||
ROOT_IP = 192.168.0.15
|
||||
INIT_PROMPT = "Assistant name: ChatAI. You code, write and provide any information without any mistakes."
|
||||
ROOT_PORT = 8000
|
||||
DATA_DIR = /home/USERNAME/weights
|
||||
CACHE = 1
|
||||
|
@ -25,3 +25,4 @@ 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."
|
||||
|
62
llamabot.js
62
llamabot.js
@ -3,8 +3,15 @@ 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: [
|
||||
@ -82,7 +89,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
|
||||
@ -104,7 +111,7 @@ client.on('messageCreate', async (message) => {
|
||||
if (conversation.messages.length === 0) {
|
||||
conversation.messages.push({
|
||||
role: 'user',
|
||||
content: ` Assistant name: ChatAI. You code, write and provide any information without any mistakes.`
|
||||
content: ` ${process.env.INIT_PROMPT}`
|
||||
});
|
||||
conversation.messages.push({
|
||||
role: 'user',
|
||||
@ -132,7 +139,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 +170,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 +179,39 @@ 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
|
||||
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);
|
||||
|
||||
try {
|
||||
const response = await fetch(`http://${process.env.ROOT_IP}:${process.env.ROOT_PORT}/v1/chat/completions`, {
|
||||
@ -187,10 +227,15 @@ 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;
|
||||
|
||||
@ -198,6 +243,7 @@ async function generateResponse(conversation) {
|
||||
throw err;
|
||||
} finally {
|
||||
clearTimeout(timeout);
|
||||
time = 0
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -12,6 +12,12 @@
|
||||
"dependencies": {
|
||||
"discord.js": "^14.9.0",
|
||||
"dotenv": "^16.0.3",
|
||||
"node-fetch": "^3.3.1"
|
||||
"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.)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user