1 Commits

Author SHA1 Message Date
Kevin Dang
2bdc7b8583 Capacity Context Modify Command (#35)
* add: modify capacity command

* update: version increment
2024-04-03 15:22:34 -07:00
8 changed files with 66 additions and 27 deletions

View File

@@ -8,7 +8,7 @@ services:
build: ./ # find docker file in designated path build: ./ # find docker file in designated path
container_name: discord container_name: discord
restart: always # rebuild container always restart: always # rebuild container always
image: discord/bot:0.3.5 image: discord/bot:0.3.6
environment: environment:
CLIENT_TOKEN: ${CLIENT_TOKEN} CLIENT_TOKEN: ${CLIENT_TOKEN}
GUILD_ID: ${GUILD_ID} GUILD_ID: ${GUILD_ID}

4
package-lock.json generated
View File

@@ -1,12 +1,12 @@
{ {
"name": "discord-ollama", "name": "discord-ollama",
"version": "0.3.5", "version": "0.3.6",
"lockfileVersion": 3, "lockfileVersion": 3,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "discord-ollama", "name": "discord-ollama",
"version": "0.3.5", "version": "0.3.6",
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"axios": "^1.6.2", "axios": "^1.6.2",

View File

@@ -1,6 +1,6 @@
{ {
"name": "discord-ollama", "name": "discord-ollama",
"version": "0.3.5", "version": "0.3.6",
"description": "Ollama Integration into discord", "description": "Ollama Integration into discord",
"main": "build/index.js", "main": "build/index.js",
"exports": "./build/index.js", "exports": "./build/index.js",

33
src/commands/capacity.ts Normal file
View 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 Capacity: SlashCommand = {
name: 'modify-capacity',
description: 'number of messages bot will hold for context.',
// set available user options to pass to the command
options: [
{
name: 'context-capacity',
description: 'a number to set capacity',
type: ApplicationCommandOptionType.Number,
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 state of bot chat features
openFile('config.json', interaction.commandName, interaction.options.get('context-capacity')?.value)
interaction.reply({
content: `Message History Capacity has been set to \`${interaction.options.get('context-capacity')?.value}\``,
ephemeral: true
})
}
}

View File

@@ -4,11 +4,13 @@ import { MessageStyle } from './messageStyle.js'
import { MessageStream } from './messageStream.js' import { MessageStream } from './messageStream.js'
import { Disable } from './disable.js' import { Disable } from './disable.js'
import { Shutoff } from './shutoff.js' import { Shutoff } from './shutoff.js'
import { Capacity } from './capacity.js'
export default [ export default [
ThreadCreate, ThreadCreate,
MessageStyle, MessageStyle,
MessageStream, MessageStream,
Disable, Disable,
Shutoff Shutoff,
Capacity
] as SlashCommand[] ] as SlashCommand[]

View File

@@ -18,15 +18,6 @@ export default event(Events.MessageCreate, async ({ log, msgHist, tokens, ollama
// Only respond if message mentions the bot // Only respond if message mentions the bot
if (!message.mentions.has(tokens.clientUid)) return if (!message.mentions.has(tokens.clientUid)) return
// check if we can push, if not, remove oldest
if (msgHist.size() === msgHist.getCapacity()) msgHist.dequeue()
// push user response
msgHist.enqueue({
role: 'user',
content: message.content
})
// Try to query and send embed // Try to query and send embed
try { try {
const config: Configuration = await new Promise((resolve, reject) => { const config: Configuration = await new Promise((resolve, reject) => {
@@ -42,12 +33,32 @@ export default event(Events.MessageCreate, async ({ log, msgHist, tokens, ollama
reject(new Error('Admin(s) have disabled chat features.\n\n Please contact your server\'s admin(s).')) reject(new Error('Admin(s) have disabled chat features.\n\n Please contact your server\'s admin(s).'))
return return
} }
// check if there is a set capacity in config
if (typeof config.options['history-capacity'] !== 'number')
log(`Capacity is undefined, using default capacity of ${msgHist.capacity}.`)
else if (config.options['history-capacity'] === msgHist.capacity)
log(`Capacity matches config as ${msgHist.capacity}, no changes made.`)
else {
log(`New Capacity found. Setting Context Capacity to ${config.options['history-capacity']}.`)
msgHist.capacity = config.options['history-capacity']
}
resolve(config) resolve(config)
}) })
}) })
let response: ChatResponse let response: ChatResponse
// check if we can push, if not, remove oldest
if (msgHist.size() === msgHist.capacity) msgHist.dequeue()
// push user response before ollama query
msgHist.enqueue({
role: 'user',
content: message.content
})
// undefined or false, use normal, otherwise use embed // undefined or false, use normal, otherwise use embed
if (config.options['message-style']) if (config.options['message-style'])
response = await embedMessage(message, ollama, tokens, msgHist) response = await embedMessage(message, ollama, tokens, msgHist)
@@ -58,9 +69,9 @@ export default event(Events.MessageCreate, async ({ log, msgHist, tokens, ollama
if (response == undefined) { msgHist.pop(); return } if (response == undefined) { msgHist.pop(); return }
// if queue is full, remove the oldest message // if queue is full, remove the oldest message
if (msgHist.size() === msgHist.getCapacity()) msgHist.dequeue() if (msgHist.size() === msgHist.capacity) msgHist.dequeue()
// successful query, save it as history // successful query, save it in context history
msgHist.enqueue({ msgHist.enqueue({
role: 'assistant', role: 'assistant',
content: response.message.content content: response.message.content

View File

@@ -17,7 +17,7 @@ export class Queue<T> implements IQueue<T> {
* Set up Queue * Set up Queue
* @param capacity max length of queue * @param capacity max length of queue
*/ */
constructor(private capacity: number = 5) {} constructor(public capacity: number = 5) {}
/** /**
* Put item in front of queue * Put item in front of queue
@@ -59,12 +59,4 @@ export class Queue<T> implements IQueue<T> {
getItems(): T[] { getItems(): T[] {
return this.storage return this.storage
} }
/**
* Get capacity of the queue
* @returns capacity of queue
*/
getCapacity(): number {
return this.capacity
}
} }

View File

@@ -5,7 +5,8 @@ export interface Configuration {
options: { options: {
'message-stream'?: boolean, 'message-stream'?: boolean,
'message-style'?: boolean, 'message-style'?: boolean,
'toggle-chat'?: boolean 'toggle-chat'?: boolean,
'history-capacity'?: number
} }
} }