llama-cpp-python-djs-bot/llamabot.js

194 lines
6.5 KiB
JavaScript
Raw Normal View History

2023-04-09 16:07:32 +00:00
const Discord = require('discord.js');
const fetch = require('node-fetch');
const emptyResponses = require('./assets/emptyMessages.js');
const {resetResponses, userResetMessages} = require('./assets/resetMessages.js');
2023-04-10 00:42:58 +00:00
const {errorMessages, busyResponses} = require('./assets/errorMessages.js');
2023-04-09 16:07:32 +00:00
require('dotenv').config()
2023-04-09 16:31:48 +00:00
// hi
2023-04-09 23:49:51 +00:00
const {
Client,
GatewayIntentBits,
ActivityType,
Partials
} = require('discord.js');
2023-04-09 16:07:32 +00:00
const client = new Client({
2023-04-09 23:49:51 +00:00
intents: [
GatewayIntentBits.DirectMessages,
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildBans,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
],
partials: [Partials.Channel],
2023-04-09 16:07:32 +00:00
});
const channelID = '1094494101631680653'; // Replace with your channel ID
const channelID2 = '1094628334727614605'; // Replace with your channel ID
const conversations = new Map();
function setBusy(userId, isBusy) {
if (conversations.has(userId)) {
conversations.get(userId).busy = isBusy;
} else {
conversations.set(userId, { busy: isBusy });
}
}
function isAnyConversationBusy() {
for (const conversation of conversations.values()) {
if (conversation.busy) {
return true;
}
}
return false;
}
2023-04-09 16:07:32 +00:00
client.once('ready', () => {
2023-04-09 23:49:51 +00:00
console.log('Bot is ready.');
client.user.setPresence({
activities: [{
name: `AI`,
type: ActivityType.Playing
}],
status: 'dnd',
});
2023-04-09 16:07:32 +00:00
});
client.on('messageCreate', async (message) => {
2023-04-09 23:49:51 +00:00
// Only respond in the specified channels
if (message.channel.id !== channelID && message.channel.id !== channelID2) {
return;
}
if (message.author.bot) return; // Ignore messages from bots
2023-04-09 16:07:32 +00:00
if (isAnyConversationBusy()) {
2023-04-09 23:49:51 +00:00
message.delete()
2023-04-10 00:42:58 +00:00
const busyResponse = busyResponses[Math.floor(Math.random() * busyResponses.length)];
await message.author.send(busyResponse); // give a notification of reset using a human like response.
2023-04-09 23:49:51 +00:00
return;
}
2023-04-09 16:07:32 +00:00
2023-04-09 23:49:51 +00:00
const userID = message.author.id;
let conversation = conversations.get(userID) || {
messages: [],
busy: false
};
if (conversation.messages.length === 0) {
conversation.messages.push({
role: 'user',
2023-04-10 00:00:51 +00:00
content: `Welcome to our conversation your name is "llama".`
2023-04-09 23:49:51 +00:00
});
conversation.messages.push({
role: 'user',
content: `My name is ${message.author.username}.`
});
conversation.messages.push({
role: 'assistant',
content: `Hello, ${message.author.username}, I am here to answer any question you have, how may I help you?`
2023-04-09 23:49:51 +00:00
});
conversation.messages.push({
role: 'user',
content: message.cleanContent
});
2023-04-09 16:07:32 +00:00
2023-04-09 23:49:51 +00:00
} else {
// Append user message to conversation history
conversation.messages.push({
role: 'user',
content: message.cleanContent
});
}
if (message.content === '!reset' || message.content === '!r') {
conversations.delete(userID); // Delete user's conversation map if they request reset
const userResetMessage = userResetMessages[Math.floor(Math.random() * userResetMessages.length)];
await message.channel.send(userResetMessage); // give a notification of reset using a human like response.
2023-04-09 23:49:51 +00:00
return;
}
2023-04-09 16:07:32 +00:00
// Append user message to conversation history
2023-04-09 23:49:51 +00:00
conversation.messages.push({
role: 'user',
content: message.cleanContent
});
2023-04-09 16:07:32 +00:00
2023-04-09 23:49:51 +00:00
try {
setBusy(message.author.id, true);
2023-04-09 23:49:51 +00:00
const response = await generateResponse(conversation);
// Append bot message to conversation history
conversation.messages.push({
role: 'assistant',
content: response
});
if (response && response.trim()) {
// Send response to user if it's not empty
await message.channel.send(response);
setBusy(message.author.id, false);
2023-04-09 23:49:51 +00:00
} else {
// Handle empty response here
const randomResponse = emptyResponses[Math.floor(Math.random() * emptyResponses.length)];
// We had an empty response, create a new memory map for sanity.
await message.channel.send(randomResponse); // give the user a human like reponse about the error
conversations.delete(userID); // Delete user's conversation map if they request reset
const resetMessage = resetResponses[Math.floor(Math.random() * resetResponses.length)];
await message.channel.send(resetMessage); // give a notification of reset using a human like response.
conversation.busy = false;
}
conversations.set(userID, conversation); // Update user's conversation map in memory
} catch (err) {
console.error(err);
const errorMessage = errorMessages[Math.floor(Math.random() * errorMessages.length)];
await message.channel.send(errorMessage); // give a notification of reset using a human like response. } finally {
setBusy(message.author.id, false);
}
2023-04-09 16:07:32 +00:00
});
async function generateResponse(conversation) {
2023-04-09 23:49:51 +00:00
const controller = new AbortController();
const timeout = setTimeout(() => {
controller.abort();
}, 900000);
const messagesCopy = [...conversation.messages]; // create a copy of the messages array
2023-04-09 23:49:51 +00:00
try {
const response = await fetch(`http://${process.env.ROOT_IP}:${process.env.ROOT_PORT}/v1/chat/completions`, {
2023-04-09 23:49:51 +00:00
method: 'POST',
headers: {
'accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
messages: messagesCopy // use the copy of the messages array
2023-04-09 23:49:51 +00:00
}),
signal: controller.signal
});
const responseData = await response.json();
2023-04-10 00:42:58 +00:00
console.log(JSON.stringify(responseData))
2023-04-09 23:49:51 +00:00
const choice = responseData.choices[0];
// Remove "user None:" and any text after it from the response
const responseText = choice.message.content.trim();
const startIndex = responseText.indexOf('user None:');
const sanitizedResponse = startIndex === -1 ? responseText : responseText.substring(0, startIndex);
return sanitizedResponse;
} catch (err) {
throw err;
2023-04-09 23:49:51 +00:00
} finally {
clearTimeout(timeout);
}
2023-04-09 16:07:32 +00:00
}
2023-04-09 23:49:51 +00:00
client.login(process.env.THE_TOKEN); // Replace with your bot token