renaming directories and moving some code

This commit is contained in:
Raven Scott
2023-03-26 02:03:15 +02:00
parent 25772c9acc
commit f34805479d
5 changed files with 0 additions and 0 deletions

62
commands/system/chat.js Normal file
View File

@ -0,0 +1,62 @@
const { MessageEmbed } = require('discord.js');
const axios = require('axios');
const jsonfile = require('jsonfile');
const util = require('util');
const readFile = util.promisify(jsonfile.readFile);
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;
try {
const session = await readFile(file);
session2 = session.id;
if (!session2) {
console.log("No Session!");
isProcessing = false;
return;
}
let prompt = interaction.options._hoistedOptions[0].value.replace(" ", "+");
const headers = {
'authority': process.env.PUBLIC_URL,
'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);
} catch (err) {
console.error(err);
interaction.editReply('Please create a session using /create-session.');
} finally {
// Set the semaphore to false
isProcessing = false;
}
},
};

View File

@ -0,0 +1,36 @@
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: process.env.USERNAME,
pass: process.env.PASS,
sendImmediately: true
})
.then((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,26 @@
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, session) {
if (err) return interaction.editReply('Please create a session using /create-session.');
const embed = new EmbedBuilder()
.setColor("#FF0000")
.setTitle("Your current session ID")
.setDescription(`**Session ID:** \`${session.id}\`\n\nThe above ID is a unique session memories are stored for processing.`)
.setTimestamp()
.setFooter({ text: `Requested by ${interaction.user.tag}`, iconURL: `${interaction.user.displayAvatarURL()}` });
interaction.followUp({ embeds: [embed] });
})
},
};

View File

@ -0,0 +1,55 @@
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': process.env.PUBLIC_URL,
'accept': 'text/event-stream',
};
const response = await axios.get(`http://${process.env.INTERNAL_IP}:8008/api/chat/` + session2, {
headers,
auth: {
username: process.env.USERNAME,
password: process.env.PASS
}
});
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)
},
};