First Commit

This commit is contained in:
Raven Scott
2023-03-25 19:09:44 +02:00
commit 0ec7419cb7
12 changed files with 416 additions and 0 deletions

21
commands/Info/about.js Normal file
View File

@ -0,0 +1,21 @@
const { EmbedBuilder } = require('discord.js');
let cpu = require('cpu-load')
module.exports = {
name: "about",
description: "Info about the bot",
run: async (client, interaction) => {
cpu(1000, function (load) {
const embed = new EmbedBuilder()
.setColor("#FF0000")
.setTitle("About rAI")
.setDescription(`Latency : ${client.ws.ping}ms\n\nrAI is a bot managed by \`snxraven#8205\` running an LLM called Alpaca.\n\nSpecs: Intel i7-1065G7 (8) @ 3.900GHz with 11 GB of RAM\nChat is set to 7 Threads\nConfigured Memory Speed: 3733 MT/s\nUSB Liveboot\n\nSingle Task Only - 256 Max Token Output`)
.setTimestamp()
.setFooter({ text: `Requested by ${interaction.user.tag}`, iconURL: `${interaction.user.displayAvatarURL()}` });
interaction.followUp({ embeds: [embed] });
})
},
};

58
commands/Info/chat.js Normal file
View File

@ -0,0 +1,58 @@
const { MessageEmbed } = require('discord.js');
const axios = require('axios');
const jsonfile = require('jsonfile');
let session2;
let isProcessing = false; // Semaphore variable
module.exports = {
name: 'chat',
description: 'Chat with Alpaca using rAI',
options: [{
"name": "query",
"description": "The thing you want to ask",
"required": true,
"type": 3 // 6 is type USER
}],
run: async (client, interaction) => {
const file = './cache/' + interaction.user.id;
let chatToSend = interaction.options._hoistedOptions[0].value;
// Check if another request is already being processed
if (isProcessing) {
interaction.editReply('Sorry, another request is already being processed. Please try again in a few minutes.');
return;
}
// Set the semaphore to true
isProcessing = true;
await jsonfile.readFile(file, async function (err, session) {
if (err) return interaction.editReply('Please create a session using /create-session.');
session2 = session.id;
if (!session2) {
console.log("No Session!");
isProcessing = false;
return;
}
let prompt = interaction.options._hoistedOptions[0].value.replace(" ", "+");
const headers = {
'authority': 'rai.snxraven.me',
'accept': 'text/event-stream',
};
console.log(session);
const response = await axios.post(`http://${process.env.INTERNAL_IP}:8008/api/chat/` + session2 + '/question?prompt=' + prompt, {
headers
});
console.log(response.data.answer);
interaction.editReply(response.data.answer);
// Set the semaphore to false
isProcessing = false;
});
},
};

38
commands/Info/new-chat.js Normal file
View File

@ -0,0 +1,38 @@
const { EmbedBuilder } = require('discord.js');
var unirest = require('unirest');
const jsonfile = require('jsonfile')
module.exports = {
name: "create-session",
description: "create a new session chat",
private: true,
run: async (client, interaction) => {
const file = './cache/' + interaction.user.id
unirest
.post(`https://${process.env.PUBLIC_URL}/api/chat`)
.headers({ 'Accept': 'application/json', 'Content-Type': 'application/json' })
.auth({
user: 'rai',
pass: 'ilikeai',
sendImmediately: true
})
.then((response) => {
console.log(response)
const obj = { id: response.body }
jsonfile.writeFile(file, obj, function (err) {
if (err) console.error(err)
})
const embed = new EmbedBuilder()
.setColor("#FF0000")
.setTitle("New Chat Session Started!")
.setDescription(`New Chat Session ID: \n` + response.body)
.setTimestamp()
.setFooter({ text: `Requested by ${interaction.user.tag}`, iconURL: `${interaction.user.displayAvatarURL()}` });
interaction.followUp({ embeds: [embed] });
})
},
};

View File

@ -0,0 +1,27 @@
const { EmbedBuilder } = require('discord.js');
var unirest = require('unirest');
const jsonfile = require('jsonfile')
module.exports = {
name: "view-session-id",
description: "View your currently assigned session ID",
private: true,
run: async (client, interaction) => {
const file = './cache/' + interaction.user.id
jsonfile.readFile(file, function (err, obj) {
if (err) return interaction.editReply('Please create a session using /create-session.');
console.dir(obj)
const embed = new EmbedBuilder()
.setColor("#FF0000")
.setTitle("New Chat Session Started!")
.setDescription(`Current Session ID: \n` + obj.id)
.setTimestamp()
.setFooter({ text: `Requested by ${interaction.user.tag}`, iconURL: `${interaction.user.displayAvatarURL()}` });
interaction.followUp({ embeds: [embed] });
})
},
};

View File

@ -0,0 +1,56 @@
const axios = require('axios');
const jsonfile = require('jsonfile');
let session2;
function parseQuestionsAndAnswers(jsonStr) {
const obj = JSON.parse(jsonStr);
const questions = obj.questions;
const answers = questions.map(q => q.answer);
return { questions, answers };
}
module.exports = {
name: "view-session-history",
description: "View your current conversation.",
run: async (client, interaction) => {
const file = './cache/' + interaction.user.id;
await jsonfile.readFile(file, async function (err, session) {
if (err) return interaction.editReply('Please create a session using /create-session.');
session2 = session.id;
if (!session2) {
console.log("No Session!");
isProcessing = false;
return;
}
const headers = {
'authority': 'rai.snxraven.me',
'accept': 'text/event-stream',
};
const response = await axios.get(`http://${process.env.INTERNAL_IP}:8008/api/chat/` + session2, {
headers,
auth: {
username: "rai",
password: "ilikeai"
}
});
console.log(response.data)
if (!response.data.questions) return interaction.editReply("You have no history in this session yet :) ");
const result = parseQuestionsAndAnswers(JSON.stringify(response.data));
let pairsString = "";
for (const question of result.questions) {
pairsString += `***Question:*** ${question.question}\n***Answer:*** ${question.answer}\n`;
}
interaction.editReply(pairsString);
})
if (err) console.error("woo" + err)
},
};