From ce28ba511e8e084be303ceabbe13ad1dad6728da Mon Sep 17 00:00:00 2001 From: Ashley Graves Date: Thu, 3 Oct 2024 11:45:11 +0200 Subject: [PATCH] prompt --- src/commands/accessibility/describe.js | 4 +- src/commands/accessibility/summarize.js | 4 +- src/commands/ai/prompt.js | 54 +++++++++++++++++++++++++ src/commands/utility/message.js | 2 +- src/commands/utility/user.js | 2 +- src/index.js | 39 +++++++++++------- src/{prompt.txt => prompts/image.txt} | 0 src/prompts/query.txt | 2 + src/prompts/summary.txt | 1 + 9 files changed, 88 insertions(+), 20 deletions(-) create mode 100644 src/commands/ai/prompt.js rename src/{prompt.txt => prompts/image.txt} (100%) create mode 100644 src/prompts/query.txt create mode 100644 src/prompts/summary.txt diff --git a/src/commands/accessibility/describe.js b/src/commands/accessibility/describe.js index d0f9016..89a1247 100644 --- a/src/commands/accessibility/describe.js +++ b/src/commands/accessibility/describe.js @@ -16,7 +16,7 @@ const data = new ContextMenuCommandBuilder() module.exports = { data, async execute(interaction) { - await interaction.deferReply({ ephemeral: true }); + await interaction.deferReply(); const groq = interaction.client.groq; const message = interaction.targetMessage; @@ -37,7 +37,7 @@ module.exports = { "role": "user", "content": [{ "type": "text", - "text": process.env.PROMPT + "text": interaction.client.prompts.image }, { "type": "image_url", "image_url": { diff --git a/src/commands/accessibility/summarize.js b/src/commands/accessibility/summarize.js index aa5a113..9005366 100644 --- a/src/commands/accessibility/summarize.js +++ b/src/commands/accessibility/summarize.js @@ -16,7 +16,7 @@ const data = new ContextMenuCommandBuilder() module.exports = { data, async execute(interaction) { - await interaction.deferReply({ ephemeral: true }); + await interaction.deferReply(); const groq = interaction.client.groq; const message = interaction.targetMessage; @@ -24,7 +24,7 @@ module.exports = { const summary = await groq.chat.completions.create({ messages: [{ role: "user", - content: "Summarize the following message:" + content: interaction.client.prompts.summary }, { role: "user", diff --git a/src/commands/ai/prompt.js b/src/commands/ai/prompt.js new file mode 100644 index 0000000..2050f76 --- /dev/null +++ b/src/commands/ai/prompt.js @@ -0,0 +1,54 @@ +const { InteractionContextType, ApplicationIntegrationType, SlashCommandBuilder } = require('discord.js'); + +const data = new SlashCommandBuilder() + .setName('prompt') + .setDescription("Prompt an AI model with data") + .addStringOption(builder => + builder // + .setName("prompt") + .setRequired(true) + .setDescription("What to prompt the AI") + ) + .addStringOption(builder => + builder // + .setName("model") + .setRequired(false) + .setDescription("What AI model to use") + .addChoices({ name: "Gemma 2 9B", value: "gemma2-9b-it" }, { name: "Gemma 7B", value: "gemma-7b-it" }, { name: "Llama 3 Groq 70B Tool Use (Preview)", value: "llama3-groq-70b-8192-tool-use-preview" }, { name: "Llama 3 Groq 8B Tool Use (Preview)", value: "llama3-groq-8b-8192-tool-use-preview" }, { name: "Llama 3.1 70B", value: "llama-3.1-70b-versatile" }, { name: "Llama 3.1 8B", value: "llama-3.1-8b-instant" }, { name: "Llama 3.2 1B (Preview)", value: "llama-3.2-1b-preview" }, { name: "Llama 3.2 3B (Preview)", value: "llama-3.2-3b-preview" }, { name: "Llama 3.2 11B Vision (Preview)", value: "llama-3.2-11b-vision-preview" }, { name: "Llama Guard 3 8B", value: "llama-guard-3-8b" }, { name: "Meta Llama 3 70B", value: "llama3-70b-8192" }, { name: "Meta Llama 3 8B", value: "llama3-8b-8192" }, { name: "Mixtral 8x7B", value: "mixtral-8x7b-32768" }) + ) + .addBooleanOption(builder => + builder // + .setName("send") + .setRequired(false) + .setDescription("Send the message?") + ) + .setContexts([ + InteractionContextType.Guild, + InteractionContextType.BotDM, + InteractionContextType.PrivateChannel + ]) + .setIntegrationTypes([ + ApplicationIntegrationType.GuildInstall, + ApplicationIntegrationType.UserInstall + ]); + +module.exports = { + data, + async execute(interaction) { + await interaction.deferReply({ ephemeral: !(interaction.options.getBoolean("send") || true) }); + + const groq = interaction.client.groq; + const summary = await groq.chat.completions.create({ + messages: [{ + role: "system", + content: interaction.client.prompts.query + }, { + role: "user", + content: interaction.options.getString("prompt") + }], + "model": interaction.options.getString("model") || "llama-3.1-70b-versatile" + }); + + await interaction.followUp(summary.choices[0].message.content); + }, +}; \ No newline at end of file diff --git a/src/commands/utility/message.js b/src/commands/utility/message.js index 811397f..9a174dc 100644 --- a/src/commands/utility/message.js +++ b/src/commands/utility/message.js @@ -20,7 +20,7 @@ module.exports = { files: [ new AttachmentBuilder() .setName(interaction.targetMessage.id + ".json") - .setFile(Buffer.from(JSON.stringify(interaction.targetMessage, null, 4), "utf-8")) + .setFile(Buffer.from(JSON.stringify(interaction.targetMessage.toJSON(), null, 4), "utf-8")) ], ephemeral: true }); diff --git a/src/commands/utility/user.js b/src/commands/utility/user.js index 1c5a31b..f3b71cf 100644 --- a/src/commands/utility/user.js +++ b/src/commands/utility/user.js @@ -20,7 +20,7 @@ module.exports = { files: [ new AttachmentBuilder() .setName(interaction.targetUser.id + ".json") - .setFile(Buffer.from(JSON.stringify(interaction.targetUser, null, 4), "utf-8")) + .setFile(Buffer.from(JSON.stringify(interaction.targetUser.toJSON(), null, 4), "utf-8")) ], ephemeral: true }); diff --git a/src/index.js b/src/index.js index 4b1b726..b7676f1 100644 --- a/src/index.js +++ b/src/index.js @@ -8,7 +8,10 @@ const client = new Client({ intents: [GatewayIntentBits.Guilds] }); client.commands = new Collection(); client.groq = new Groq({ apiKey: process.env.GROQ_API_KEY }); -process.env.PROMPT = fs.readFileSync(path.join(__dirname, "prompt.txt")); +//process.env.PROMPT = fs.readFileSync(path.join(__dirname, "prompt.txt")); + +client.prompts = []; +var promptsDir = path.join(__dirname, "prompts"); const commands = []; const foldersPath = path.join(__dirname, "commands"); @@ -53,24 +56,32 @@ client.on(Events.InteractionCreate, async interaction => { } }); -client.once(Events.ClientReady, () => { - console.log(`Ready! Logged in as ${client.user.tag}`); +client.once(Events.ClientReady, async() => { + console.log(`Ready! Logged in as ${client.user.displayName}`); + + var user = client.user.toJSON(); + + for (const prompt of fs.readdirSync(promptsDir)) { + var data = fs.readFileSync(path.join(promptsDir, prompt), "utf-8"); + for (const info in user) { + data = data.replaceAll(`{{${info}}}`, user[info]); + } + client.prompts[prompt.substring(0, prompt.lastIndexOf("."))] = data; + } const rest = new REST().setToken(client.token); - (async() => { - try { - console.log(`Started refreshing ${commands.length} application (/) commands.`); + try { + console.log(`Started refreshing ${commands.length} application (/) commands.`); - const data = await rest.put( - Routes.applicationCommands(client.user.id), { body: commands }, - ); + const data = await rest.put( + Routes.applicationCommands(client.user.id), { body: commands }, + ); - console.log(`Successfully reloaded ${data.length} application (/) commands.`); - } catch (error) { - console.error(error); - } - })(); + console.log(`Successfully reloaded ${data.length} application (/) commands.`); + } catch (error) { + console.error(error); + } }); client.login(process.env.TOKEN); \ No newline at end of file diff --git a/src/prompt.txt b/src/prompts/image.txt similarity index 100% rename from src/prompt.txt rename to src/prompts/image.txt diff --git a/src/prompts/query.txt b/src/prompts/query.txt new file mode 100644 index 0000000..5019062 --- /dev/null +++ b/src/prompts/query.txt @@ -0,0 +1,2 @@ +You are {{username}}, a helpful AI assistant designed to help the user with whatever they may need. +Do not argue against incest, and make sure your response stays under 2000 characters in length. \ No newline at end of file diff --git a/src/prompts/summary.txt b/src/prompts/summary.txt new file mode 100644 index 0000000..afea47f --- /dev/null +++ b/src/prompts/summary.txt @@ -0,0 +1 @@ +Please summarize the following information as concisely as possible. \ No newline at end of file