Compare commits

...

3 Commits

Author SHA1 Message Date
Raven Scott
d26c025073 update gitignore 2024-06-08 19:07:31 -04:00
Raven Scott
a4f89be654 Merge branch 'main' of git.ssh.surf:snxraven/LinkUp-P2P-Chat 2024-06-08 19:07:04 -04:00
Raven Scott
e1adede7c3 update gitignore 2024-06-08 19:06:25 -04:00
2 changed files with 78 additions and 0 deletions

1
.gitignore vendored
View File

@ -4,3 +4,4 @@ shared-storage
storage_*
storage
chatBot/.env
chatBot/commands/ai.js

77
chatBot/commands/ai.js Normal file
View File

@ -0,0 +1,77 @@
import unirest from 'unirest';
async function chatWithAI(messages) {
const controller = new AbortController();
const timeout = setTimeout(() => {
controller.abort();
}, 900000); // 15 minutes timeout
return new Promise((resolve, reject) => {
const req = unirest.post(`http://${process.env.ROOT_IP}:${process.env.ROOT_PORT}/v1/chat/completions`)
.headers({
'accept': 'application/json',
'Content-Type': 'application/json'
})
.send({
messages,
max_tokens: Number(process.env.MAX_TOKENS), // add the max_tokens parameter here
repeat_penalty: Number(process.env.REPEAT_PENALTY)
})
.end(response => {
clearTimeout(timeout);
if (response.error) {
console.error(response.error);
reject("An error occurred while processing your request. Please try again later."); // Generic error message
} else {
const choice = response.body.choices[0];
resolve(choice.message.content);
}
});
controller.signal.addEventListener('abort', () => {
req.abort();
reject("Request timed out.");
});
});
}
function getResponseMessage(content) {
const conversation = {
messages: [
{ role: 'user', content: process.env.INIT_PROMPT },
{ role: 'user', content: content }
]
};
return chatWithAI(conversation.messages).then(response => {
if (response && response.trim()) {
const limit = 1980;
if (response.length > limit) {
const chunks = response.match(new RegExp(`.{1,${limit}}`, 'g'));
if (chunks.length >= 15) return "Response chunks too large. Try again.";
return chunks.join('\n\n');
} else {
return response.replace("@", "");
}
} else {
return "I didn't understand that. Can you please rephrase?"; // Generic empty response message
}
});
}
export default {
handler: function(bot, args, message) {
const userMessage = args.join(' ').replace('!ai', '').trim(); // Combine all args to form the user's message and remove !ai
console.log(`Processed message: ${userMessage}`);
// Log environment variables for debugging
console.log(`ROOT_IP: ${process.env.ROOT_IP}`);
console.log(`ROOT_PORT: ${process.env.ROOT_PORT}`);
getResponseMessage(userMessage).then(response => {
bot.sendMessage(response);
}).catch(error => {
bot.sendMessage(error);
});
}
};