renaming directories and moving some code
This commit is contained in:
62
commands/system/chat.js
Normal file
62
commands/system/chat.js
Normal 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;
|
||||
}
|
||||
},
|
||||
};
|
Reference in New Issue
Block a user