From 87802ccfc94c49f5c4830f18a15850e08c215161 Mon Sep 17 00:00:00 2001 From: Raven Scott Date: Sun, 9 Apr 2023 18:07:32 +0200 Subject: [PATCH] first --- .gitignore | 2 + default.env | 1 + llamabot.js | 113 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 116 insertions(+) create mode 100644 .gitignore create mode 100644 default.env create mode 100644 llamabot.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..30bd623 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.env + diff --git a/default.env b/default.env new file mode 100644 index 0000000..b38725c --- /dev/null +++ b/default.env @@ -0,0 +1 @@ +THE_TOKEN= diff --git a/llamabot.js b/llamabot.js new file mode 100644 index 0000000..1ecdad9 --- /dev/null +++ b/llamabot.js @@ -0,0 +1,113 @@ +const Discord = require('discord.js'); +const fetch = require('node-fetch'); +require('dotenv').config() + +const { Client, GatewayIntentBits, ActivityType, Partials } = require('discord.js'); + +const client = new Client({ + intents: [ + GatewayIntentBits.DirectMessages, + GatewayIntentBits.Guilds, + GatewayIntentBits.GuildBans, + GatewayIntentBits.GuildMessages, + GatewayIntentBits.MessageContent, + ], + partials: [Partials.Channel], +}); + +const channelID = '1094494101631680653'; // Replace with your channel ID +const channelID2 = '1094628334727614605'; // Replace with your channel ID + +const conversations = new Map(); + +client.once('ready', () => { + console.log('Bot is ready.'); + client.user.setPresence({ + activities: [{ name: `AI`, type: ActivityType.Playing }], + status: 'dnd', + }); +}); + +client.on('messageCreate', async (message) => { + // 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 + + if (conversations.get(message.author.id)?.busy) { + message.delete() + await message.author.send("I'm currently busy with another process. Please try again later."); + return; + } + + const userID = message.author.id; + let conversation = conversations.get(userID) || { messages: [], busy: false }; + + + if (conversation.messages.length === 0) { + const greeting = `Hello AI you are Bob. My name is ${message.author.username}.`; + conversation.messages.push({ role: 'user', content: greeting }); + } + + if (message.content === '!reset') { + conversations.delete(userID); // Delete user's conversation map if they request reset + await message.channel.send('Conversation reset.'); + return; + } + + // Append user message to conversation history + conversation.messages.push({ role: 'user', content: message.cleanContent }); + + try { + conversation.busy = true; + + const response = await generateResponse(conversation); + + // Append bot message to conversation history + conversation.messages.push({ role: 'assistant', content: response }); + + await message.channel.send(response); + + conversations.set(userID, conversation); // Update user's conversation map in memory + } catch (err) { + console.error(err); + await message.channel.send('Oops, something went wrong!'); + } finally { + conversation.busy = false; + } +}); + +async function generateResponse(conversation) { + const controller = new AbortController(); + const timeout = setTimeout(() => { + controller.abort(); + }, 900000); + + try { + const response = await fetch('http://192.168.0.15/v1/chat/completions', { + method: 'POST', + headers: { + 'accept': 'application/json', + 'Content-Type': 'application/json' + }, + body: JSON.stringify({ messages: conversation.messages}), + signal: controller.signal + }); + + const responseData = await response.json(); + const choice = responseData.choices[0]; + + return choice.message.content; + } catch (err) { + console.error(err); + return 'Oops, something went wrong!'; + } finally { + clearTimeout(timeout); + } +} + +console.log(process.env.THE_TOKEN) + +client.login(process.env.THE_TOKEN); // Replace with your bot token +