rai-serge-discord-bot/commands/system/chat.js

69 lines
2.3 KiB
JavaScript
Raw Permalink Normal View History

2023-03-25 13:09:44 -04:00
const { MessageEmbed } = require('discord.js');
const axios = require('axios');
const jsonfile = require('jsonfile');
const util = require('util');
const readFile = util.promisify(jsonfile.readFile);
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;
try {
const session = await readFile(file);
2023-03-25 13:09:44 -04:00
sessionID = session.id;
2023-03-25 13:09:44 -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,
'accept': 'text',
2023-03-25 13:09:44 -04:00
};
console.log(session);
2023-04-07 15:18:40 -04:00
const response = await axios.post(`http://${process.env.INTERNAL_IP}:${process.env.SERGE_PORT}/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
if (!response.data.answer) throw new Error("Did not receive a reply. API error?")
2023-03-25 13:09:44 -04:00
interaction.editReply(response.data.answer);
} 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 13:09:44 -04:00
},
};