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

59 lines
1.9 KiB
JavaScript

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;
});
},
};