const Discord = require("discord.js"); const fetch = require("node-fetch"); const fs = require('fs'); const server = "192.168.1.213"; class BotChat { async get_response(history, input_text) { const complete_prompt = this.bot_prompt.replace("",history).replace("",input_text); console.log("PROMPT:" + complete_prompt); const response = await fetch(`http://${server}:8080/v1/engines/llama_13B_q4/completions`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ prompt: complete_prompt }) }); const response_json = await response.json(); console.log(response_json); return response_json.text; } constructor() { this.matches = [ "botchat", "botmention", "botrandom" ]; this.bot_prompt = fs.readFileSync("bot-modules/botchat/prompts/default.txt").toString(); } async getRecentChannelHistory(chan) { let history = ""; // Fetch the last 5 messages in the channel const messages = await chan.messages.fetch({ limit: 5 }); // Reverse the order of the messages so that the oldest one is first const reversed = messages.reverse(); // Loop through the messages and print their content and author for (let msg of reversed) { const user = msg[1].author.username; const content = msg[1].content; history += `${user}: ${content}\n`; } return history; } async action(msg, botrandom) { let chan = msg.channel; try { const input_text = msg.content.replace("!botchat","").replace(/<@.+?>/,"bot,").trim(); console.log(`Input Text: ${input_text}`); const history = await this.getRecentChannelHistory(chan); const trimmed_text = await this.get_response(history,input_text); chan.send(trimmed_text); // chan.send(trimmed_text); /* const input_text = msg.content.replace("!botchat","").replace(/<@.+?>/,"anon").trim(); console.log(`Input Text: ${input_text}`); await chan.sendTyping(); if (botrandom == "botrandom" || msg.content.startsWith('!botrandom')) { const output_json = await this.random_prompt(); const output_text = (output_json.data[0]); const startIndex = output_text.indexOf("865467536"); const output_only = output_text.slice(startIndex+9).split("---")[0].trim(); console.log(`Generating random nonsense: ${output_only}`); chan.send(output_only); } else if (msg.reference) { const repliedTo = await msg.channel.messages.fetch(msg.reference.messageId); const oldReply = repliedTo.content; const output_json = await this.prompt_reply(oldReply,input_text); const output_text = (output_json.data[0]); const trimmed_text = this.find_response(output_text); console.log(`Generated Reply: ${trimmed_text}`); chan.send(trimmed_text); } else { const output_json = await this.prompt(input_text); const output_text = (output_json.data[0]); const trimmed_text = this.find_response(output_text); console.log(`Generated Message: ${trimmed_text}`); chan.send(trimmed_text); }*/ } catch (e) { chan.send(`Error: ${e}`); } } } module.exports = BotChat;