2023-03-25 13:09:44 -04:00
|
|
|
const { MessageEmbed } = require('discord.js');
|
|
|
|
const axios = require('axios');
|
|
|
|
const jsonfile = require('jsonfile');
|
2023-03-25 19:44:43 -04:00
|
|
|
const util = require('util');
|
|
|
|
const readFile = util.promisify(jsonfile.readFile);
|
2023-03-25 20:27:33 -04:00
|
|
|
let sessionID;
|
2023-03-25 13:09:44 -04:00
|
|
|
let isProcessing = false; // Semaphore variable
|
|
|
|
|
2023-03-26 23:02:55 -04:00
|
|
|
// TODO: Chose an http client and stick with it.
|
|
|
|
// Axios Vs Unirest - TBD
|
|
|
|
|
2023-03-25 13:09:44 -04:00
|
|
|
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
|
|
|
|
}],
|
2023-03-25 13:50:48 -04:00
|
|
|
|
2023-03-25 13:09:44 -04:00
|
|
|
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) {
|
2023-04-07 14:54:46 -04:00
|
|
|
interaction.followUp({ content: 'Sorry, another request is already being processed. Please try again in a few minutes.', ephemeral: true });
|
2023-03-25 13:09:44 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Set the semaphore to true
|
|
|
|
isProcessing = true;
|
|
|
|
|
2023-03-25 19:44:43 -04:00
|
|
|
try {
|
|
|
|
const session = await readFile(file);
|
2023-03-25 13:09:44 -04:00
|
|
|
|
2023-03-25 20:27:33 -04:00
|
|
|
sessionID = session.id;
|
2023-03-25 13:09:44 -04:00
|
|
|
|
2023-03-25 20:27:33 -04:00
|
|
|
if (!sessionID) {
|
2023-03-25 13:09:44 -04:00
|
|
|
console.log("No Session!");
|
|
|
|
isProcessing = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let prompt = interaction.options._hoistedOptions[0].value.replace(" ", "+");
|
|
|
|
const headers = {
|
2023-03-25 13:40:50 -04:00
|
|
|
'authority': process.env.PUBLIC_URL,
|
2023-03-25 20:27:33 -04:00
|
|
|
'accept': 'text',
|
2023-03-25 13:09:44 -04:00
|
|
|
};
|
|
|
|
console.log(session);
|
2023-03-25 20:27:33 -04:00
|
|
|
const response = await axios.post(`http://${process.env.INTERNAL_IP}:8008/api/chat/${sessionID}/question?prompt=${prompt}`, {
|
2023-03-25 13:09:44 -04:00
|
|
|
headers
|
|
|
|
});
|
|
|
|
|
2023-04-02 21:11:54 -04:00
|
|
|
console.log(`Prompt: ${prompt.replace("+", " ")}\nResponse: ${response.data.answer}`);
|
2023-03-25 13:09:44 -04:00
|
|
|
|
|
|
|
interaction.editReply(response.data.answer);
|
2023-03-25 19:44:43 -04:00
|
|
|
} catch (err) {
|
|
|
|
console.error(err);
|
|
|
|
interaction.editReply('Please create a session using /create-session.');
|
|
|
|
} finally {
|
2023-03-25 13:09:44 -04:00
|
|
|
// Set the semaphore to false
|
|
|
|
isProcessing = false;
|
2023-03-25 19:44:43 -04:00
|
|
|
}
|
2023-03-25 13:09:44 -04:00
|
|
|
},
|
|
|
|
};
|