|
|
|
@ -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
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|