Docker Container Setup (#15)

* minor package update and env

* added docker scripts

* added working docker compose

* fixed docker container bridge
This commit is contained in:
Kevin Dang
2024-02-07 09:59:06 -08:00
committed by GitHub
parent 89c19990fa
commit ca6b8c3f9c
13 changed files with 124 additions and 337 deletions

View File

@@ -1,6 +1,7 @@
import { Client, GatewayIntentBits } from 'discord.js'
import { registerEvents } from './utils/events.js'
import Events from './events/index.js'
import { Ollama } from 'ollama'
// Import keys/tokens
import Keys from './keys.js'
@@ -16,6 +17,11 @@ const client = new Client({
]
});
// initialize connection to ollama container
const ollama = new Ollama({
host: `http://${Keys.ipAddress}:${Keys.portAddress}`,
})
const messageHistory = [
{
role: 'system',
@@ -30,7 +36,7 @@ const messageHistory = [
* @param client the bot reference
* @param Keys tokens from .env files
*/
registerEvents(client, Events, messageHistory, Keys)
registerEvents(client, Events, messageHistory, Keys, ollama)
// Try to log in the client
await client.login(Keys.clientToken)

View File

@@ -4,7 +4,7 @@ import { embedMessage, event, Events } from '../utils/index.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 }, message) => {
export default event(Events.MessageCreate, async ({ log, msgHist, tokens, ollama }, message) => {
log(`Message created \"${message.content}\" from ${message.author.tag}.`)
// Hard-coded channel to test output there only, in our case "ollama-endpoint"
@@ -23,7 +23,7 @@ export default event(Events.MessageCreate, async ({ log, msgHist, tokens }, mess
})
// Try to query and send embed
const response = await embedMessage(message, tokens, msgHist)
const response = await embedMessage(message, ollama, tokens, msgHist)
// Try to query and send message
// log(normalMessage(message, tokens, msgHist))

View File

@@ -4,8 +4,6 @@ import commands from '../commands/index.js'
// Log when the bot successfully logs in and export it
export default event(Events.ClientReady, ({ log }, client) => {
log(`Logged in as ${client.user.username}.`)
// Register the commands associated with the bot upon loggin in
registerCommands(client, commands)
@@ -14,4 +12,6 @@ export default event(Events.ClientReady, ({ log }, client) => {
name: 'Powered by Ollama',
type: ActivityType.Custom
})
log(`Logged in as ${client.user.username}.`)
})

View File

@@ -5,7 +5,9 @@ export const Keys = {
channel: getEnvVar('CHANNEL_ID'),
model: getEnvVar('MODEL'),
clientUid: getEnvVar('CLIENT_UID'),
guildId: getEnvVar('GUILD_ID')
guildId: getEnvVar('GUILD_ID'),
ipAddress: getEnvVar('OLLAMA_IP'),
portAddress: getEnvVar('OLLAMA_PORT')
} as const // readonly keys
export default Keys

View File

@@ -1,21 +1,24 @@
import type { ClientEvents, Awaitable, Client } from 'discord.js'
import { Ollama } from 'ollama'
// Export events through here to reduce amount of imports
export { Events } from 'discord.js'
export type LogMethod = (...args: unknown[]) => void
export type EventKeys = keyof ClientEvents // only wants keys of ClientEvents object
export type Tokens = {
channel: string,
model: string,
clientUid: string
}
// Event properties
export interface EventProps {
client: Client
log: LogMethod
msgHist: { role: string, content: string }[]
tokens: {
channel: string,
model: string,
clientUid: string
}
tokens: Tokens,
ollama: Ollama
}
export type EventCallback<T extends EventKeys> = (
props: EventProps,
@@ -36,11 +39,8 @@ export function registerEvents(
client: Client,
events: Event[],
msgHist: { role: string, content: string }[],
tokens: {
channel: string,
model: string,
clientUid: string
}
tokens: Tokens,
ollama: Ollama
): void {
for (const { key, callback } of events) {
client.on(key, (...args) => {
@@ -49,7 +49,7 @@ export function registerEvents(
// Handle Errors, call callback, log errors as needed
try {
callback({ client, log, msgHist, tokens }, ...args)
callback({ client, log, msgHist, tokens, ollama }, ...args)
} catch (error) {
log('[Uncaught Error]', error)
}

View File

@@ -1,5 +1,5 @@
import { EmbedBuilder, Message } from 'discord.js'
import ollama, { ChatResponse } from 'ollama'
import { ChatResponse, Ollama } from 'ollama'
/**
* Method to send replies as normal text on discord like any other user
@@ -8,7 +8,8 @@ import ollama, { ChatResponse } from 'ollama'
* @param msgHist message history between user and model
*/
export async function embedMessage(
message: Message,
message: Message,
ollama: Ollama,
tokens: {
channel: string,
model: string
@@ -44,17 +45,19 @@ export async function embedMessage(
stream: false
})
// dummy message to let user know that query is underway
const newEmbed = new EmbedBuilder()
.setTitle(`Responding to ${message.author.tag}`)
.setDescription(response.message.content)
.setDescription(response.message.content || 'No Content to Provided...')
.setColor('#00FF00')
// edit the message
sentMessage.edit({ embeds: [newEmbed] })
} catch(error: any) {
console.log(`[Event: messageEmbed] Error creating message: ${error.message}`);
const errorEmbed = new EmbedBuilder()
.setTitle(`Responding to ${message.author.tag}`)
.setDescription(error.error)
.setDescription(`Issue creating response: ${error.message}`)
.setColor('#00FF00')
// send back error