Chat Queue Persistence (#33)
* fix: workflow env * update: center title on readme * update: readme goals and format * add: icons in readme * fix: plus margin * update: environment variables in contr. * add: queue for chat history * add: set -e for workflow failure * update: version increment * fix: client null info * fix: shutoff issues
This commit is contained in:
5
.github/CONTRIBUTING.md
vendored
5
.github/CONTRIBUTING.md
vendored
@@ -23,10 +23,7 @@
|
|||||||
## Environment
|
## Environment
|
||||||
* You will need two environment files:
|
* You will need two environment files:
|
||||||
* `.env`: for running the bot
|
* `.env`: for running the bot
|
||||||
* `CLIENT_TOKEN`: the token for the bot to log in
|
* Please refer to `.env.sample` for all environment variables to include
|
||||||
* `CHANNEL_ID`: the id of the channel you wish for the bot to listen in
|
|
||||||
* `MODEL`: the mode you wish to use
|
|
||||||
* `BOT_UID`: the user id the bot goes by (the id of the discord user)
|
|
||||||
* `.env.dev.local`: also runs the bot, but with development variables
|
* `.env.dev.local`: also runs the bot, but with development variables
|
||||||
* Currently there are no differences between the two, but when needed, you may add environment variables as needed.
|
* Currently there are no differences between the two, but when needed, you may add environment variables as needed.
|
||||||
|
|
||||||
|
|||||||
3
.github/workflows/build-test.yml
vendored
3
.github/workflows/build-test.yml
vendored
@@ -37,9 +37,12 @@ jobs:
|
|||||||
echo CLIENT_UID = ${{ secrets.CLIENT_UID }} >> .env
|
echo CLIENT_UID = ${{ secrets.CLIENT_UID }} >> .env
|
||||||
echo OLLAMA_IP = ${{ secrets.OLLAMA_IP }} >> .env
|
echo OLLAMA_IP = ${{ secrets.OLLAMA_IP }} >> .env
|
||||||
echo OLLAMA_PORT = ${{ secrets.OLLAMA_PORT }} >> .env
|
echo OLLAMA_PORT = ${{ secrets.OLLAMA_PORT }} >> .env
|
||||||
|
echo ADMINS = ${{ secrets.ADMINS }} >> .env
|
||||||
|
|
||||||
|
# set -e ensures if nohup fails, this section fails
|
||||||
- name: Startup Discord Bot Client
|
- name: Startup Discord Bot Client
|
||||||
run: |
|
run: |
|
||||||
|
set -e
|
||||||
nohup npm run prod &
|
nohup npm run prod &
|
||||||
|
|
||||||
Discord-Ollama-Container-Build: # test docker build and run
|
Discord-Ollama-Container-Build: # test docker build and run
|
||||||
|
|||||||
25
README.md
25
README.md
@@ -1,5 +1,26 @@
|
|||||||
# Discord Ollama Integration [](https://creativecommons.org/licenses/by-nc/4.0/) [](https://github.com/kevinthedang/discord-ollama/releases/latest)
|
<div align="center">
|
||||||
Ollama is an AI model management tool that allows users to install and use custom large language models locally. The goal is to create a discord bot that will utilize Ollama and chat with it on a Discord server! Also, allow others to create their own models personalized for their own servers!
|
<p><a href="#"><a href="https://ollama.ai/"><img alt="ollama" src="./imgs/ollama-icon.png" width="200px" /></a><img alt="+" src="./imgs/grey-plus.png" width="100px" /></a><a href="https://discord.com/"><img alt="discord" src="./imgs/discord-icon.png" width="190px" /></a></p>
|
||||||
|
<h1>Discord Ollama Integration</h1>
|
||||||
|
<h3><a href="#"></a>Ollama as your Discord AI Assistant</h3>
|
||||||
|
<p><a href="#"></a><a href="https://creativecommons.org/licenses/by-nc/4.0/"><img alt="License" src="https://img.shields.io/badge/License-CC_BY--NC_4.0-darkgreen.svg" /></a>
|
||||||
|
<a href="#"></a><a href="https://github.com/kevinthedang/discord-ollama/releases/latest"><img alt="Release" src="https://img.shields.io/github/v/release/kevinthedang/discord-ollama?logo=github" /></a>
|
||||||
|
<a href="#"></a><a href="https://github.com/kevinthedang/discord-ollama/actions/workflows/build-test.yml"><img alt="Build Status" src="https://github.com/kevinthedang/discord-ollama/actions/workflows/build-test.yml/badge.svg" /></a>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
## About/Goals
|
||||||
|
Ollama is an AI model management tool that allows users to install and use custom large language models locally.
|
||||||
|
The project aims to:
|
||||||
|
* [x] Create a Discord bot that will utilize Ollama and chat to chat with users!
|
||||||
|
* [ ] User Preferences on Chat
|
||||||
|
* [ ] Message Persistance on Channels and Threads
|
||||||
|
* [x] Containerization with Docker
|
||||||
|
* [x] Slash Commands Compatible
|
||||||
|
* [ ] 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!
|
||||||
|
* [ ] Documentation on creating your own LLM
|
||||||
|
* [ ] Documentation on web scrapping and cleaning
|
||||||
|
|
||||||
## Environment Setup
|
## Environment Setup
|
||||||
* Clone this repo using `git clone https://github.com/kevinthedang/discord-ollama.git` or just use [GitHub Desktop](https://desktop.github.com/) to clone the repo.
|
* Clone this repo using `git clone https://github.com/kevinthedang/discord-ollama.git` or just use [GitHub Desktop](https://desktop.github.com/) to clone the repo.
|
||||||
|
|||||||
@@ -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.4
|
image: discord/bot:0.3.5
|
||||||
environment:
|
environment:
|
||||||
CLIENT_TOKEN: ${CLIENT_TOKEN}
|
CLIENT_TOKEN: ${CLIENT_TOKEN}
|
||||||
GUILD_ID: ${GUILD_ID}
|
GUILD_ID: ${GUILD_ID}
|
||||||
|
|||||||
BIN
imgs/discord-icon.png
Normal file
BIN
imgs/discord-icon.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 94 KiB |
BIN
imgs/grey-plus.png
Normal file
BIN
imgs/grey-plus.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 6.5 KiB |
BIN
imgs/ollama-icon.png
Normal file
BIN
imgs/ollama-icon.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 49 KiB |
4
package-lock.json
generated
4
package-lock.json
generated
@@ -1,12 +1,12 @@
|
|||||||
{
|
{
|
||||||
"name": "discord-ollama",
|
"name": "discord-ollama",
|
||||||
"version": "0.3.4",
|
"version": "0.3.5",
|
||||||
"lockfileVersion": 3,
|
"lockfileVersion": 3,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "discord-ollama",
|
"name": "discord-ollama",
|
||||||
"version": "0.3.4",
|
"version": "0.3.5",
|
||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"axios": "^1.6.2",
|
"axios": "^1.6.2",
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "discord-ollama",
|
"name": "discord-ollama",
|
||||||
"version": "0.3.4",
|
"version": "0.3.5",
|
||||||
"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",
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import { Client, GatewayIntentBits } from 'discord.js'
|
|||||||
import { UserMessage, registerEvents } from './utils/events.js'
|
import { UserMessage, registerEvents } from './utils/events.js'
|
||||||
import Events from './events/index.js'
|
import Events from './events/index.js'
|
||||||
import { Ollama } from 'ollama'
|
import { Ollama } from 'ollama'
|
||||||
|
import { Queue } from './queues/queue.js'
|
||||||
|
|
||||||
// Import keys/tokens
|
// Import keys/tokens
|
||||||
import Keys from './keys.js'
|
import Keys from './keys.js'
|
||||||
@@ -23,12 +24,7 @@ const ollama = new Ollama({
|
|||||||
})
|
})
|
||||||
|
|
||||||
// Create Queue managed by Events
|
// Create Queue managed by Events
|
||||||
const messageHistory: [UserMessage] = [
|
const messageHistory: Queue<UserMessage> = new Queue<UserMessage>
|
||||||
{
|
|
||||||
role: 'system',
|
|
||||||
content: 'Your name is Ollama GU'
|
|
||||||
}
|
|
||||||
]
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* register events for bot to listen to in discord
|
* register events for bot to listen to in discord
|
||||||
@@ -45,3 +41,9 @@ await client.login(Keys.clientToken)
|
|||||||
console.error('[Login Error]', error)
|
console.error('[Login Error]', error)
|
||||||
process.exit(1)
|
process.exit(1)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// queue up bots name
|
||||||
|
messageHistory.enqueue({
|
||||||
|
role: 'assistant',
|
||||||
|
content: `My name is ${client.user?.username}`
|
||||||
|
})
|
||||||
@@ -29,12 +29,18 @@ export const Shutoff: SlashCommand = {
|
|||||||
const superUsers: string[] = JSON.parse(Keys.superUser.replace(/'/g, '"'))
|
const superUsers: string[] = JSON.parse(Keys.superUser.replace(/'/g, '"'))
|
||||||
|
|
||||||
// check if admin or false on shutdown
|
// check if admin or false on shutdown
|
||||||
if (interaction.user.tag in superUsers || !(!interaction.options.get('are-you-sure')?.value && interaction.user.tag in superUsers)) {
|
if (interaction.user.tag !in superUsers) {
|
||||||
interaction.reply({
|
interaction.reply({
|
||||||
content: `Shutdown failed:\n\n${interaction.user.tag}, You do not have permission to shutoff **${client.user?.tag}**, otherwise, you just didn't want to.`,
|
content: `Shutdown failed:\n\n${interaction.user.tag}, You do not have permission to shutoff **${client.user?.tag}**.`,
|
||||||
ephemeral: true
|
ephemeral: true
|
||||||
})
|
})
|
||||||
return // stop from shutting down
|
return // stop from shutting down
|
||||||
|
} else if (!interaction.options.get('are-you-sure')?.value) {
|
||||||
|
interaction.reply({
|
||||||
|
content: `Shutdown failed:\n\n${interaction.user.tag}, You didn't want to shutoff **${client.user?.tag}**.`,
|
||||||
|
ephemeral: true
|
||||||
|
})
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
interaction.reply({
|
interaction.reply({
|
||||||
|
|||||||
@@ -18,8 +18,11 @@ 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
|
// push user response
|
||||||
msgHist.push({
|
msgHist.enqueue({
|
||||||
role: 'user',
|
role: 'user',
|
||||||
content: message.content
|
content: message.content
|
||||||
})
|
})
|
||||||
@@ -54,14 +57,17 @@ export default event(Events.MessageCreate, async ({ log, msgHist, tokens, ollama
|
|||||||
// If something bad happened, remove user query and stop
|
// If something bad happened, remove user query and stop
|
||||||
if (response == undefined) { msgHist.pop(); return }
|
if (response == undefined) { msgHist.pop(); return }
|
||||||
|
|
||||||
|
// if queue is full, remove the oldest message
|
||||||
|
if (msgHist.size() === msgHist.getCapacity()) msgHist.dequeue()
|
||||||
|
|
||||||
// successful query, save it as history
|
// successful query, save it as history
|
||||||
msgHist.push({
|
msgHist.enqueue({
|
||||||
role: 'assistant',
|
role: 'assistant',
|
||||||
content: response.message.content
|
content: response.message.content
|
||||||
})
|
})
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
msgHist.pop() // remove message because of failure
|
msgHist.pop() // remove message because of failure
|
||||||
openFile('config.json', 'message-style', true)
|
openFile('config.json', 'message-style', true)
|
||||||
message.reply(`**Response generation failed.**\n\n**Reason:** *${error.message}*`)
|
message.reply(`**Error Occurred:**\n\n**Reason:** *${error.message}*`)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
70
src/queues/queue.ts
Normal file
70
src/queues/queue.ts
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
// Queue interfaces for any queue class to follow
|
||||||
|
interface IQueue<T> {
|
||||||
|
enqueue(item: T): void
|
||||||
|
dequeue(): T | undefined
|
||||||
|
size(): number
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Queue for UserMessages
|
||||||
|
* When the limit for messages is met, we want to clear
|
||||||
|
* out the oldest message in the queue
|
||||||
|
*/
|
||||||
|
export class Queue<T> implements IQueue<T> {
|
||||||
|
private storage: T[] = []
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set up Queue
|
||||||
|
* @param capacity max length of queue
|
||||||
|
*/
|
||||||
|
constructor(private capacity: number = 5) {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Put item in front of queue
|
||||||
|
* @param item object of type T to add into queue
|
||||||
|
*/
|
||||||
|
enqueue(item: T): void {
|
||||||
|
if (this.size() === this.capacity)
|
||||||
|
throw Error('Queue has reached max capacity, you cannot add more items.')
|
||||||
|
this.storage.push(item)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove item at end of queue
|
||||||
|
* @returns object of type T in queue
|
||||||
|
*/
|
||||||
|
dequeue(): T | undefined {
|
||||||
|
return this.storage.shift()
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Size of the queue
|
||||||
|
* @returns length of queue as a int/number
|
||||||
|
*/
|
||||||
|
size(): number {
|
||||||
|
return this.storage.length
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove the front of the queue, typically for errors
|
||||||
|
*/
|
||||||
|
pop(): void {
|
||||||
|
this.storage.pop()
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Geet the queue as an array
|
||||||
|
* @returns a array of T items
|
||||||
|
*/
|
||||||
|
getItems(): T[] {
|
||||||
|
return this.storage
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get capacity of the queue
|
||||||
|
* @returns capacity of queue
|
||||||
|
*/
|
||||||
|
getCapacity(): number {
|
||||||
|
return this.capacity
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
import type { ClientEvents, Awaitable, Client } from 'discord.js'
|
import type { ClientEvents, Awaitable, Client, User } from 'discord.js'
|
||||||
import { Ollama } from 'ollama'
|
import { Ollama } from 'ollama'
|
||||||
|
import { Queue } from '../queues/queue.js'
|
||||||
|
|
||||||
// Export events through here to reduce amount of imports
|
// Export events through here to reduce amount of imports
|
||||||
export { Events } from 'discord.js'
|
export { Events } from 'discord.js'
|
||||||
@@ -33,7 +34,7 @@ export type UserMessage = {
|
|||||||
export interface EventProps {
|
export interface EventProps {
|
||||||
client: Client
|
client: Client
|
||||||
log: LogMethod
|
log: LogMethod
|
||||||
msgHist: { role: string, content: string }[]
|
msgHist: Queue<UserMessage>
|
||||||
tokens: Tokens,
|
tokens: Tokens,
|
||||||
ollama: Ollama
|
ollama: Ollama
|
||||||
}
|
}
|
||||||
@@ -63,7 +64,7 @@ export function event<T extends EventKeys>(key: T, callback: EventCallback<T>):
|
|||||||
export function registerEvents(
|
export function registerEvents(
|
||||||
client: Client,
|
client: Client,
|
||||||
events: Event[],
|
events: Event[],
|
||||||
msgHist: UserMessage[],
|
msgHist: Queue<UserMessage>,
|
||||||
tokens: Tokens,
|
tokens: Tokens,
|
||||||
ollama: Ollama
|
ollama: Ollama
|
||||||
): void {
|
): void {
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import { EmbedBuilder, Message } from 'discord.js'
|
import { EmbedBuilder, Message } from 'discord.js'
|
||||||
import { ChatResponse, Ollama } from 'ollama'
|
import { ChatResponse, Ollama } from 'ollama'
|
||||||
import { UserMessage } from './events.js'
|
import { UserMessage } from './events.js'
|
||||||
|
import { Queue } from '../queues/queue.js'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Method to send replies as normal text on discord like any other user
|
* Method to send replies as normal text on discord like any other user
|
||||||
@@ -15,7 +16,7 @@ export async function embedMessage(
|
|||||||
channel: string,
|
channel: string,
|
||||||
model: string
|
model: string
|
||||||
},
|
},
|
||||||
msgHist: UserMessage[]
|
msgHist: Queue<UserMessage>
|
||||||
) {
|
) {
|
||||||
// bot response
|
// bot response
|
||||||
let response: ChatResponse
|
let response: ChatResponse
|
||||||
@@ -33,7 +34,7 @@ export async function embedMessage(
|
|||||||
// Attempt to query model for message
|
// Attempt to query model for message
|
||||||
response = await ollama.chat({
|
response = await ollama.chat({
|
||||||
model: tokens.model,
|
model: tokens.model,
|
||||||
messages: msgHist,
|
messages: msgHist.getItems(),
|
||||||
options: {
|
options: {
|
||||||
num_thread: 8, // remove if optimization needed further
|
num_thread: 8, // remove if optimization needed further
|
||||||
mirostat: 1,
|
mirostat: 1,
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import { Message } from 'discord.js'
|
import { Message } from 'discord.js'
|
||||||
import { ChatResponse, Ollama } from 'ollama'
|
import { ChatResponse, Ollama } from 'ollama'
|
||||||
import { UserMessage } from './events.js'
|
import { UserMessage } from './events.js'
|
||||||
|
import { Queue } from '../queues/queue.js'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Method to send replies as normal text on discord like any other user
|
* Method to send replies as normal text on discord like any other user
|
||||||
@@ -15,7 +16,7 @@ export async function normalMessage(
|
|||||||
channel: string,
|
channel: string,
|
||||||
model: string
|
model: string
|
||||||
},
|
},
|
||||||
msgHist: UserMessage[]
|
msgHist: Queue<UserMessage>
|
||||||
) {
|
) {
|
||||||
// bot's respnse
|
// bot's respnse
|
||||||
let response: ChatResponse
|
let response: ChatResponse
|
||||||
@@ -25,7 +26,7 @@ export async function normalMessage(
|
|||||||
// Attempt to query model for message
|
// Attempt to query model for message
|
||||||
response = await ollama.chat({
|
response = await ollama.chat({
|
||||||
model: tokens.model,
|
model: tokens.model,
|
||||||
messages: msgHist,
|
messages: msgHist.getItems(),
|
||||||
options: {
|
options: {
|
||||||
num_thread: 8, // remove if optimization needed further
|
num_thread: 8, // remove if optimization needed further
|
||||||
mirostat: 1,
|
mirostat: 1,
|
||||||
|
|||||||
Reference in New Issue
Block a user