mirror of
https://github.com/kevinthedang/discord-ollama.git
synced 2025-12-13 03:56:07 -05:00
User Preferences and Setup Docs (#20)
* added message style command * docker setup scripts * reformat messageStyle.ts * fix: register unregister on deploy * add: messageStream preference * add: json config handler * update: messageCreate gets config * update: shifted chat to config callback * fix: naming conventions based on discord * update: setup in docs now * add: static docker ips * version increment * add: bot message for no config * fix: no config case * add: clarification for subnetting * update: version increment in lock file --------- Co-authored-by: JT2M0L3Y <jtsmoley@icloud.com>
This commit is contained in:
@@ -1,6 +1,10 @@
|
||||
import { SlashCommand } from '../utils/commands.js'
|
||||
import { ThreadCreate } from './threadCreate.js'
|
||||
import { MessageStyle } from './messageStyle.js'
|
||||
import { MessageStream } from './messageStream.js'
|
||||
|
||||
export default [
|
||||
ThreadCreate
|
||||
ThreadCreate,
|
||||
MessageStyle,
|
||||
MessageStream
|
||||
] as SlashCommand[]
|
||||
33
src/commands/messageStream.ts
Normal file
33
src/commands/messageStream.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import { ApplicationCommandOptionType, ChannelType, Client, CommandInteraction } from 'discord.js'
|
||||
import { SlashCommand } from '../utils/commands.js'
|
||||
import { openFile } from '../utils/jsonHandler.js'
|
||||
|
||||
export const MessageStream: SlashCommand = {
|
||||
name: 'message-stream',
|
||||
description: 'change preference on message streaming from ollama. WARNING: can be very slow.',
|
||||
|
||||
// user option(s) for setting stream
|
||||
options: [
|
||||
{
|
||||
name: 'stream',
|
||||
description: 'enable or disable stream preference',
|
||||
type: ApplicationCommandOptionType.Boolean,
|
||||
required: true
|
||||
}
|
||||
],
|
||||
|
||||
// change preferences based on command
|
||||
run: async (client: Client, interaction: CommandInteraction) => {
|
||||
// verify channel
|
||||
const channel = await client.channels.fetch(interaction.channelId)
|
||||
if (!channel || channel.type !== ChannelType.GuildText) return
|
||||
|
||||
// save value to json and write to it
|
||||
openFile('config.json', interaction.commandName, interaction.options.get('stream')?.value)
|
||||
|
||||
interaction.reply({
|
||||
content: `Message streaming preferences for embed set to: \`${interaction.options.get('stream')?.value}\``,
|
||||
ephemeral: true
|
||||
})
|
||||
}
|
||||
}
|
||||
33
src/commands/messageStyle.ts
Normal file
33
src/commands/messageStyle.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import { ChannelType, Client, CommandInteraction, ApplicationCommandOptionType } from 'discord.js'
|
||||
import { SlashCommand } from '../utils/commands.js'
|
||||
import { openFile } from '../utils/jsonHandler.js'
|
||||
|
||||
export const MessageStyle: SlashCommand = {
|
||||
name: 'message-style',
|
||||
description: 'sets the message style to embed or normal',
|
||||
|
||||
// set available user options to pass to the command
|
||||
options: [
|
||||
{
|
||||
name: 'embed',
|
||||
description: 'toggle embedded or normal message',
|
||||
type: ApplicationCommandOptionType.Boolean,
|
||||
required: true
|
||||
}
|
||||
],
|
||||
|
||||
// Query for message information and set the style
|
||||
run: async (client: Client, interaction: CommandInteraction) => {
|
||||
// fetch channel and message
|
||||
const channel = await client.channels.fetch(interaction.channelId)
|
||||
if (!channel || channel.type !== ChannelType.GuildText) return
|
||||
|
||||
// set the message style
|
||||
openFile('config.json', interaction.commandName, interaction.options.get('embed')?.value)
|
||||
|
||||
interaction.reply({
|
||||
content: `Message style preferences for embed set to: \`${interaction.options.get('embed')?.value}\``,
|
||||
ephemeral: true
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -17,11 +17,11 @@ export const ThreadCreate: SlashCommand = {
|
||||
})
|
||||
|
||||
// Send a message in the thread
|
||||
thread.send(`**User:** ${interaction.user}`)
|
||||
thread.send(`**User:** ${interaction.user} \n**People in Coversation:** ${thread.memberCount}`)
|
||||
|
||||
// user only reply
|
||||
return interaction.reply({
|
||||
content: 'I can help you in the Thread below.',
|
||||
content: `I can help you in the Thread below. \n**Thread ID:** ${thread.id}`,
|
||||
ephemeral: true
|
||||
})
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ import commands from '../commands/index.js'
|
||||
export default event(Events.InteractionCreate, async ({ log, client }, interaction) => {
|
||||
if (!interaction.isCommand() || !interaction.isChatInputCommand()) return
|
||||
|
||||
log(`Interaction called \'${interaction.commandName}\' from ${interaction.client.user.tag}.`)
|
||||
log(`Interaction called \'${interaction.commandName}\' from ${interaction.user.tag}.`)
|
||||
|
||||
// ensure command exists, otherwise kill event
|
||||
const command = commands.find(command => command.name === interaction.commandName)
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
import { embedMessage, event, Events } from '../utils/index.js'
|
||||
import { ChatResponse } from 'ollama'
|
||||
import { embedMessage, event, Events, normalMessage } from '../utils/index.js'
|
||||
import { Configuration, getConfig } from '../utils/jsonHandler.js'
|
||||
|
||||
/**
|
||||
* Max Message length for free users is 2000 characters (bot or not).
|
||||
* @param message the message received from the channel
|
||||
*/
|
||||
export default event(Events.MessageCreate, async ({ log, msgHist, tokens, ollama }, message) => {
|
||||
log(`Message created \"${message.content}\" from ${message.author.tag}.`)
|
||||
log(`Message \"${message.content}\" from ${message.author.tag} in channel/thread ${message.channelId}.`)
|
||||
|
||||
// Hard-coded channel to test output there only, in our case "ollama-endpoint"
|
||||
if (message.channelId != tokens.channel) return
|
||||
@@ -22,18 +24,36 @@ export default event(Events.MessageCreate, async ({ log, msgHist, tokens, ollama
|
||||
content: message.content
|
||||
})
|
||||
|
||||
// Try to query and send embed
|
||||
const response = await embedMessage(message, ollama, tokens, msgHist)
|
||||
// Try to query and send embed
|
||||
try {
|
||||
const config: Configuration = await new Promise((resolve, reject) => {
|
||||
getConfig('config.json', (config) => {
|
||||
if (config === undefined) {
|
||||
reject(new Error('No Configuration is set up.'))
|
||||
return
|
||||
}
|
||||
resolve(config)
|
||||
})
|
||||
})
|
||||
|
||||
// Try to query and send message
|
||||
// log(normalMessage(message, tokens, msgHist))
|
||||
let response: ChatResponse
|
||||
|
||||
// If something bad happened, remove user query and stop
|
||||
if (response == undefined) { msgHist.pop(); return }
|
||||
// undefined or false, use normal, otherwise use embed
|
||||
if (config.options['message-style'])
|
||||
response = await embedMessage(message, ollama, tokens, msgHist)
|
||||
else
|
||||
response = await normalMessage(message, ollama, tokens, msgHist)
|
||||
|
||||
// successful query, save it as history
|
||||
msgHist.push({
|
||||
role: 'assistant',
|
||||
content: response.message.content
|
||||
})
|
||||
// If something bad happened, remove user query and stop
|
||||
if (response == undefined) { msgHist.pop(); return }
|
||||
|
||||
// successful query, save it as history
|
||||
msgHist.push({
|
||||
role: 'assistant',
|
||||
content: response.message.content
|
||||
})
|
||||
} catch (error: any) {
|
||||
msgHist.pop() // remove message because of failure
|
||||
message.reply(`**Response generation failed.**\n\nReason: ${error.message}\n\nPlease use any config slash command.`)
|
||||
}
|
||||
})
|
||||
@@ -1,10 +1,14 @@
|
||||
import { CommandInteraction, ChatInputApplicationCommandData, Client } from 'discord.js'
|
||||
import { CommandInteraction, ChatInputApplicationCommandData, Client, ApplicationCommandOption } from 'discord.js'
|
||||
|
||||
/**
|
||||
* interface for how slash commands should be run
|
||||
*/
|
||||
export interface SlashCommand extends ChatInputApplicationCommandData {
|
||||
run: (client: Client, interaction: CommandInteraction) => void
|
||||
run: (
|
||||
client: Client,
|
||||
interaction: CommandInteraction,
|
||||
options?: ApplicationCommandOption[]
|
||||
) => void
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -16,7 +20,28 @@ export function registerCommands(client: Client, commands: SlashCommand[]): void
|
||||
// ensure the bot is online before registering
|
||||
if (!client.application) return
|
||||
|
||||
// map commands into an array of names, used to checking registered commands
|
||||
const commandsToRegister: string[] = commands.map(command => command.name)
|
||||
|
||||
// fetch all the commands and delete them
|
||||
client.application.commands.fetch().then((fetchedCommands) => {
|
||||
for (const command of fetchedCommands.values()) {
|
||||
if (!commandsToRegister.includes(command.name)) {
|
||||
command.delete().catch(console.error)
|
||||
console.log(`[Command: ${command.name}] Removed from Discord`)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
// clear the cache of the commands
|
||||
client.application.commands.cache.clear()
|
||||
|
||||
// iterate through all commands and register them with the bot
|
||||
for (const command of commands)
|
||||
client.application.commands.create(command)
|
||||
client.application.commands
|
||||
.create(command)
|
||||
.then((c) => {
|
||||
console.log(`[Command: ${c.name}] Registered on Discord`)
|
||||
c.options?.forEach((o) => console.log(` - ${o.name}`))
|
||||
})
|
||||
}
|
||||
56
src/utils/jsonHandler.ts
Normal file
56
src/utils/jsonHandler.ts
Normal file
@@ -0,0 +1,56 @@
|
||||
import fs from 'fs'
|
||||
|
||||
export interface Configuration {
|
||||
readonly name: string
|
||||
options: {
|
||||
'message-stream'?: boolean,
|
||||
'message-style'?: boolean
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to open a file in the working directory and modify/create it
|
||||
*
|
||||
* @param filename name of the file
|
||||
* @param key key value to access
|
||||
* @param value new value to assign
|
||||
*/
|
||||
export function openFile(filename: string, key: string, value: any) {
|
||||
// check if the file exists, if not then make the config file
|
||||
if (fs.existsSync(filename)) {
|
||||
fs.readFile(filename, 'utf8', (error, data) => {
|
||||
if (error)
|
||||
console.log(`[Error: openFile] Incorrect file format`)
|
||||
else {
|
||||
const object = JSON.parse(data)
|
||||
object['options'][key] = value
|
||||
fs.writeFileSync(filename, JSON.stringify(object, null, 2))
|
||||
}
|
||||
})
|
||||
} else {
|
||||
const object: Configuration = JSON.parse('{ \"name\": \"Discord Ollama Confirgurations\" }')
|
||||
|
||||
// set standard information for config file and options
|
||||
object['options'] = {
|
||||
[key]: value
|
||||
}
|
||||
|
||||
fs.writeFileSync(filename, JSON.stringify(object, null, 2))
|
||||
console.log(`[Util: openFile] Created 'config.json' in working directory`)
|
||||
}
|
||||
}
|
||||
|
||||
export async function getConfig(filename: string, callback: (config: Configuration | undefined) => void): Promise<void> {
|
||||
// attempt to read the file and get the configuration
|
||||
if (fs.existsSync(filename)) {
|
||||
fs.readFile(filename, 'utf8', (error, data) => {
|
||||
if (error) {
|
||||
callback(undefined)
|
||||
return // something went wrong... stop
|
||||
}
|
||||
callback(JSON.parse(data))
|
||||
})
|
||||
} else {
|
||||
callback(undefined) // file not found
|
||||
}
|
||||
}
|
||||
@@ -52,10 +52,10 @@ export async function embedMessage(
|
||||
// edit the message
|
||||
sentMessage.edit({ embeds: [newEmbed] })
|
||||
} catch(error: any) {
|
||||
console.log(`[Event: messageEmbed] Error creating message: ${error.message}`)
|
||||
console.log(`[Util: messageEmbed] Error creating message: ${error.message}`)
|
||||
const errorEmbed = new EmbedBuilder()
|
||||
.setTitle(`Responding to ${message.author.tag}`)
|
||||
.setDescription(`Issue creating response: ${error.message}`)
|
||||
.setDescription(`**Response generation failed.**\n\nReason: ${error.message}`)
|
||||
.setColor('#00FF00')
|
||||
|
||||
// send back error
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { Message } from 'discord.js'
|
||||
import ollama, { ChatResponse } from 'ollama'
|
||||
import { ChatResponse, Ollama } from 'ollama'
|
||||
import { UserMessage } from './events.js'
|
||||
|
||||
/**
|
||||
@@ -8,8 +8,9 @@ import { UserMessage } from './events.js'
|
||||
* @param tokens tokens to run query
|
||||
* @param msgHist message history between user and model
|
||||
*/
|
||||
export function normalMessage(
|
||||
message: Message,
|
||||
export async function normalMessage(
|
||||
message: Message,
|
||||
ollama: Ollama,
|
||||
tokens: {
|
||||
channel: string,
|
||||
model: string
|
||||
@@ -19,7 +20,7 @@ export function normalMessage(
|
||||
// bot's respnse
|
||||
let response: ChatResponse
|
||||
|
||||
message.reply('Generating Response . . .').then(async sentMessage => {
|
||||
await message.reply('Generating Response . . .').then(async sentMessage => {
|
||||
try {
|
||||
// Attempt to query model for message
|
||||
response = await ollama.chat({
|
||||
@@ -37,7 +38,8 @@ export function normalMessage(
|
||||
// edit the 'generic' response to new message
|
||||
sentMessage.edit(response.message.content)
|
||||
} catch(error: any) {
|
||||
sentMessage.edit(error.error)
|
||||
console.log(`[Util: messageNormal] Error creating message: ${error.message}`)
|
||||
sentMessage.edit(`**Response generation failed.**\n\nReason: ${error.message}`)
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
@@ -4,6 +4,8 @@ import { AxiosResponse } from 'axios'
|
||||
* When running a /api/chat stream, the output needs to be parsed into an array of objects
|
||||
* This method is used for development purposes and testing
|
||||
*
|
||||
* This will not work as intended with the inclusion of ollama-js, needs to be modified to work with it
|
||||
*
|
||||
* @param stream Axios response to from Ollama
|
||||
*/
|
||||
export function parseStream(stream: AxiosResponse<any, any>) {
|
||||
|
||||
Reference in New Issue
Block a user