This commit is contained in:
Ashley Graves 2024-10-03 11:45:11 +02:00
parent 1206eb9190
commit ce28ba511e
9 changed files with 88 additions and 20 deletions

View file

@ -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": {

View file

@ -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",

54
src/commands/ai/prompt.js Normal file
View file

@ -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);
},
};

View file

@ -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
});

View file

@ -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
});

View file

@ -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);

2
src/prompts/query.txt Normal file
View file

@ -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.

1
src/prompts/summary.txt Normal file
View file

@ -0,0 +1 @@
Please summarize the following information as concisely as possible.