2 Commits

Author SHA1 Message Date
Kevin Dang
da1f08a070 Message Blocks for Normal Message Style (#37)
* add: if check for message length

* update: version increment

* update: readme
2024-04-07 16:09:27 -07:00
Kevin Dang
2bdc7b8583 Capacity Context Modify Command (#35)
* add: modify capacity command

* update: version increment
2024-04-03 15:22:34 -07:00
10 changed files with 75 additions and 32 deletions

View File

@@ -15,7 +15,7 @@ The project aims to:
* [ ] Message Persistance on Channels and Threads
* [x] Containerization with Docker
* [x] Slash Commands Compatible
* [ ] Generated Token Length Handling for >2000 or >6000 characters
* [x] Generated Token Length Handling for >2000 ~~or >6000 characters~~
* [ ] External WebUI Integration
* [ ] Administrator Role Compatible
* [ ] Allow others to create their own models personalized for their own servers!

View File

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

4
package-lock.json generated
View File

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

View File

@@ -1,6 +1,6 @@
{
"name": "discord-ollama",
"version": "0.3.5",
"version": "0.4.0",
"description": "Ollama Integration into discord",
"main": "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 { Disable } from './disable.js'
import { Shutoff } from './shutoff.js'
import { Capacity } from './capacity.js'
export default [
ThreadCreate,
MessageStyle,
MessageStream,
Disable,
Shutoff
Shutoff,
Capacity
] 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
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 {
const config: Configuration = await new Promise((resolve, reject) => {
@@ -38,16 +29,36 @@ export default event(Events.MessageCreate, async ({ log, msgHist, tokens, ollama
}
// check if chat is disabled
if(!config.options['toggle-chat']) {
if (!config.options['toggle-chat']) {
reject(new Error('Admin(s) have disabled chat features.\n\n Please contact your server\'s admin(s).'))
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)
})
})
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
if (config.options['message-style'])
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 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({
role: 'assistant',
content: response.message.content

View File

@@ -17,7 +17,7 @@ export class Queue<T> implements IQueue<T> {
* Set up Queue
* @param capacity max length of queue
*/
constructor(private capacity: number = 5) {}
constructor(public capacity: number = 5) {}
/**
* Put item in front of queue
@@ -59,12 +59,4 @@ export class Queue<T> implements IQueue<T> {
getItems(): T[] {
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: {
'message-stream'?: boolean,
'message-style'?: boolean,
'toggle-chat'?: boolean
'toggle-chat'?: boolean,
'history-capacity'?: number
}
}

View File

@@ -21,7 +21,7 @@ export async function normalMessage(
// bot's respnse
let response: ChatResponse
await message.reply('Generating Response . . .').then(async sentMessage => {
await message.channel.send('Generating Response . . .').then(async sentMessage => {
try {
// Attempt to query model for message
response = await ollama.chat({
@@ -36,7 +36,11 @@ export async function normalMessage(
stream: false
})
// edit the 'generic' response to new message
// check if message length > discord max for normal messages
if (response.message.content.length > 2000) {
sentMessage.edit(response.message.content.slice(0, 2000))
message.channel.send(response.message.content.slice(2000))
} else // edit the 'generic' response to new message
sentMessage.edit(response.message.content)
} catch(error: any) {
console.log(`[Util: messageNormal] Error creating message: ${error.message}`)