The code defines an interaction module that exports three functions (doInteraction, doMention, and doPrivate) to handle different types of interactions with the bot, including greetings, mentions, and private messages. Each function uses a combination of imported modules and functions to generate responses, such as triggering typing, selecting models, and creating messages.
npm run import -- "discord llm interactions"const {DEFAULT_MODEL} = importer.import("general chit chat")
const {DEFAULT_APPLICATION} = importer.import("discord configuration")
const {triggerTyping, createMessage} = importer.import("disrcord api")
const handleConversation = importer.import("handle conversation")
const selectModel = importer.import("select llm")
async function doInteraction(interaction) {
let prompt
if(interaction.data.name == 'hello') {
prompt = 'hello'
}
else { // if (interaction.data.name == 'prompt') {
prompt = interaction.data.options[0].value
}
let a1 = await llmPrompt(prompt + '\nmake it short.')
await updateInteraction({
content: a1.substring(0, 1800)
}, interaction.id, interaction.token)
}
async function doMention(interaction) {
await triggerTyping(interaction.channel_id)
let q1 = interaction.content.replace('<@' + DEFAULT_APPLICATION + '>', '') + '\nmake it short.'
let promptModel = await selectModel(DEFAULT_MODEL)
let result = await handleConversation(promptModel, interaction.author.id, q1)
return await createMessage({
content: result.substring(0, 1800),
message_reference: {
message_id: interaction.id
}
}, interaction.channel_id)
}
async function doPrivate(interaction) {
await triggerTyping(interaction.channel_id)
let promptModel = await selectModel(DEFAULT_MODEL)
let result = await handleConversation(promptModel, interaction.author.id, interaction.content)
return await createMessage({
content: result.substring(0, 1800),
message_reference: {
message_id: interaction.id
}
}, interaction.channel_id)
}
module.exports = {
doInteraction,
doMention,
doPrivate
}
const {
DEFAULT_MODEL,
DEFAULT_APPLICATION,
} = require('./general-chit-chat-importer');
const {
triggerTyping,
createMessage,
} = require('./discord-api-importer');
const {
handleConversation,
} = require('./handle-conversation-importer');
const {
selectModel,
} = require('./select-llm-importer');
/**
* Handles interactions with the bot.
*
* @param {object} interaction - The interaction object.
* @returns {Promise} A promise resolving when the interaction is handled.
*/
async function doInteraction(interaction) {
const prompt = getPrompt(interaction);
const result = await llmPrompt(prompt + '\nMake it short.');
await updateInteraction({
content: result.substring(0, 1800),
}, interaction.id, interaction.token);
}
/**
* Handles mentions with the bot.
*
* @param {object} interaction - The interaction object.
* @returns {Promise} A promise resolving when the mention is handled.
*/
async function doMention(interaction) {
await triggerTyping(interaction.channel_id);
const q1 = interaction.content.replace(`<@${DEFAULT_APPLICATION}>`, '') + '\nMake it short.';
const promptModel = await selectModel(DEFAULT_MODEL);
const result = await handleConversation(promptModel, interaction.author.id, q1);
return await createMessage({
content: result.substring(0, 1800),
message_reference: {
message_id: interaction.id,
},
}, interaction.channel_id);
}
/**
* Handles private messages with the bot.
*
* @param {object} interaction - The interaction object.
* @returns {Promise} A promise resolving when the private message is handled.
*/
async function doPrivate(interaction) {
await triggerTyping(interaction.channel_id);
const promptModel = await selectModel(DEFAULT_MODEL);
const result = await handleConversation(promptModel, interaction.author.id, interaction.content);
return await createMessage({
content: result.substring(0, 1800),
message_reference: {
message_id: interaction.id,
},
}, interaction.channel_id);
}
/**
* Gets the prompt for the interaction.
*
* @param {object} interaction - The interaction object.
* @returns {string} The prompt.
*/
function getPrompt(interaction) {
if (interaction.data.name === 'hello') {
return 'Hello';
} else {
return interaction.data.options[0].value;
}
}
module.exports = {
doInteraction,
doMention,
doPrivate,
};
// TODO: Move the getPrompt function to a separate file.
// TODO: Implement logging for errors.const {DEFAULT_MODEL} = importer.import('general chit chat')
const {DEFAULT_APPLICATION} = importer.import('discord configuration')
const {triggerTyping, createMessage} = importer.import('disrcord api')
const handleConversation = importer.import('handle conversation')
const selectModel = importer.import('select llm')
importer object:
DEFAULT_MODEL and DEFAULT_APPLICATION are imported from general chit chat and discord configuration respectively.triggerTyping and createMessage are imported from disrcord api.handleConversation is imported from handle conversation.selectModel is imported from select llm.async function doInteraction(interaction) {
//...
}
async function doMention(interaction) {
//...
}
async function doPrivate(interaction) {
//...
}
doInteraction(interaction): Handles interactions with a name of hello.doMention(interaction): Handles interactions that mention the bot.doPrivate(interaction): Handles private messages to the bot.async function doInteraction(interaction) {
let prompt
if(interaction.data.name == 'hello') {
prompt = 'hello'
}
else {
prompt = interaction.data.options[0].value
}
let a1 = await llmPrompt(prompt + '\nmake it short.')
await updateInteraction({
content: a1.substring(0, 1800)
}, interaction.id, interaction.token)
}
doInteraction function:
llmPrompt to generate a response.updateInteraction.async function doMention(interaction) {
await triggerTyping(interaction.channel_id)
let q1 = interaction.content.replace('<@' + DEFAULT_APPLICATION + '>', '') + '\nmake it short.'
let promptModel = await selectModel(DEFAULT_MODEL)
let result = await handleConversation(promptModel, interaction.author.id, q1)
return await createMessage({
content: result.substring(0, 1800),
message_reference: {
message_id: interaction.id
}
}, interaction.channel_id)
}
doMention function:
selectModel.handleConversation to generate a response.createMessage.async function doPrivate(interaction) {
await triggerTyping(interaction.channel_id)
let promptModel = await selectModel(DEFAULT_MODEL)
let result = await handleConversation(promptModel, interaction.author.id, interaction.content)
return await createMessage({
content: result.substring(0, 1800),
message_reference: {
message_id: interaction.id
}
}, interaction.channel_id)
}
doPrivate function:
selectModel.handleConversation to generate a response.createMessage.module.exports = {
doInteraction,
doMention,
doPrivate
}