Small Documentation and Refactoring (#18)

* cleanup and documentation

* added dev message for parser

* grammar and other type replacements
This commit is contained in:
Kevin Dang
2024-02-18 17:39:00 -08:00
committed by GitHub
parent 1c62958c9f
commit 5e74736c57
7 changed files with 39 additions and 14 deletions

1
.gitignore vendored
View File

@@ -6,6 +6,7 @@ build/
dist/ dist/
app/ app/
tmp/ tmp/
data/
# dotenv environment variable files # dotenv environment variable files
.env .env

View File

@@ -11,7 +11,7 @@
"prod": "node .", "prod": "node .",
"client": "npm i && npm run build && npm run prod", "client": "npm i && npm run build && npm run prod",
"clean": "docker rmi $(docker images -a -q) -f && docker images -a", "clean": "docker rmi $(docker images -a -q) -f && docker images -a",
"start": "echo \"y\" | docker-compose rm && docker-compose build && docker-compose up" "start": "echo \"y\" | docker-compose rm && docker-compose build --no-cache && docker-compose up"
}, },
"author": "Kevin Dang", "author": "Kevin Dang",
"license": "ISC", "license": "ISC",

View File

@@ -1,5 +1,5 @@
import { Client, GatewayIntentBits } from 'discord.js' import { Client, GatewayIntentBits } from 'discord.js'
import { 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'
@@ -22,7 +22,8 @@ const ollama = new Ollama({
host: `http://${Keys.ipAddress}:${Keys.portAddress}`, host: `http://${Keys.ipAddress}:${Keys.portAddress}`,
}) })
const messageHistory = [ // Create Queue managed by Events
const messageHistory: [UserMessage] = [
{ {
role: 'system', role: 'system',
content: 'Your name is Ollama GU' content: 'Your name is Ollama GU'

View File

@@ -6,12 +6,29 @@ export { Events } from 'discord.js'
export type LogMethod = (...args: unknown[]) => void export type LogMethod = (...args: unknown[]) => void
export type EventKeys = keyof ClientEvents // only wants keys of ClientEvents object export type EventKeys = keyof ClientEvents // only wants keys of ClientEvents object
/**
* Tokens to run the bot as intended
* @param channel the channel where the bot will respond to queries
* @param model chosen model for the ollama to utilize
* @param clientUid the discord id for the bot
*/
export type Tokens = { export type Tokens = {
channel: string, channel: string,
model: string, model: string,
clientUid: string clientUid: string
} }
/**
* Format for the messages to be stored when communicating when the bot
* @param role either assistant, user, or system
* @param content string of the message the user or assistant provided
*/
export type UserMessage = {
role: string,
content: string
}
// Event properties // Event properties
export interface EventProps { export interface EventProps {
client: Client client: Client
@@ -35,10 +52,18 @@ export function event<T extends EventKeys>(key: T, callback: EventCallback<T>):
return { key, callback } return { key, callback }
} }
/**
* Method to register events to the bot per file in the events directory
* @param client initialized bot client
* @param events all the exported events from the index.ts in the events dir
* @param msgHist The message history of the bot
* @param tokens the passed in environment tokens for the service
* @param ollama the initialized ollama instance
*/
export function registerEvents( export function registerEvents(
client: Client, client: Client,
events: Event[], events: Event[],
msgHist: { role: string, content: string }[], msgHist: UserMessage[],
tokens: Tokens, tokens: Tokens,
ollama: Ollama ollama: Ollama
): void { ): void {

View File

@@ -1,5 +1,6 @@
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'
/** /**
* 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
@@ -14,10 +15,7 @@ export async function embedMessage(
channel: string, channel: string,
model: string model: string
}, },
msgHist: { msgHist: UserMessage[]
role: string,
content: string
}[]
) { ) {
// bot response // bot response
let response: ChatResponse let response: ChatResponse
@@ -48,13 +46,13 @@ export async function embedMessage(
// dummy message to let user know that query is underway // dummy message to let user know that query is underway
const newEmbed = new EmbedBuilder() const newEmbed = new EmbedBuilder()
.setTitle(`Responding to ${message.author.tag}`) .setTitle(`Responding to ${message.author.tag}`)
.setDescription(response.message.content || 'No Content to Provided...') .setDescription(response.message.content || 'No Content to Provide...')
.setColor('#00FF00') .setColor('#00FF00')
// edit the message // edit the message
sentMessage.edit({ embeds: [newEmbed] }) sentMessage.edit({ embeds: [newEmbed] })
} catch(error: any) { } catch(error: any) {
console.log(`[Event: messageEmbed] Error creating message: ${error.message}`); console.log(`[Event: messageEmbed] Error creating message: ${error.message}`)
const errorEmbed = new EmbedBuilder() const errorEmbed = new EmbedBuilder()
.setTitle(`Responding to ${message.author.tag}`) .setTitle(`Responding to ${message.author.tag}`)
.setDescription(`Issue creating response: ${error.message}`) .setDescription(`Issue creating response: ${error.message}`)

View File

@@ -1,5 +1,6 @@
import { Message } from 'discord.js' import { Message } from 'discord.js'
import ollama, { ChatResponse } from 'ollama' import ollama, { ChatResponse } from 'ollama'
import { UserMessage } from './events.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
@@ -13,10 +14,7 @@ export function normalMessage(
channel: string, channel: string,
model: string model: string
}, },
msgHist: { msgHist: UserMessage[]
role: string,
content: string
}[]
) { ) {
// bot's respnse // bot's respnse
let response: ChatResponse let response: ChatResponse

View File

@@ -2,6 +2,8 @@ import { AxiosResponse } from 'axios'
/** /**
* When running a /api/chat stream, the output needs to be parsed into an array of objects * 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
*
* @param stream Axios response to from Ollama * @param stream Axios response to from Ollama
*/ */
export function parseStream(stream: AxiosResponse<any, any>) { export function parseStream(stream: AxiosResponse<any, any>) {