mirror of
https://github.com/kevinthedang/discord-ollama.git
synced 2025-12-12 03:46:08 -05:00
Small Documentation and Refactoring (#18)
* cleanup and documentation * added dev message for parser * grammar and other type replacements
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -6,6 +6,7 @@ build/
|
||||
dist/
|
||||
app/
|
||||
tmp/
|
||||
data/
|
||||
|
||||
# dotenv environment variable files
|
||||
.env
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
"prod": "node .",
|
||||
"client": "npm i && npm run build && npm run prod",
|
||||
"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",
|
||||
"license": "ISC",
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
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 { Ollama } from 'ollama'
|
||||
|
||||
@@ -22,7 +22,8 @@ const ollama = new Ollama({
|
||||
host: `http://${Keys.ipAddress}:${Keys.portAddress}`,
|
||||
})
|
||||
|
||||
const messageHistory = [
|
||||
// Create Queue managed by Events
|
||||
const messageHistory: [UserMessage] = [
|
||||
{
|
||||
role: 'system',
|
||||
content: 'Your name is Ollama GU'
|
||||
|
||||
@@ -6,12 +6,29 @@ export { Events } from 'discord.js'
|
||||
|
||||
export type LogMethod = (...args: unknown[]) => void
|
||||
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 = {
|
||||
channel: string,
|
||||
model: 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
|
||||
export interface EventProps {
|
||||
client: Client
|
||||
@@ -35,10 +52,18 @@ export function event<T extends EventKeys>(key: T, callback: EventCallback<T>):
|
||||
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(
|
||||
client: Client,
|
||||
events: Event[],
|
||||
msgHist: { role: string, content: string }[],
|
||||
msgHist: UserMessage[],
|
||||
tokens: Tokens,
|
||||
ollama: Ollama
|
||||
): void {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { EmbedBuilder, Message } from 'discord.js'
|
||||
import { ChatResponse, Ollama } from 'ollama'
|
||||
import { UserMessage } from './events.js'
|
||||
|
||||
/**
|
||||
* Method to send replies as normal text on discord like any other user
|
||||
@@ -14,10 +15,7 @@ export async function embedMessage(
|
||||
channel: string,
|
||||
model: string
|
||||
},
|
||||
msgHist: {
|
||||
role: string,
|
||||
content: string
|
||||
}[]
|
||||
msgHist: UserMessage[]
|
||||
) {
|
||||
// bot response
|
||||
let response: ChatResponse
|
||||
@@ -48,13 +46,13 @@ export async function embedMessage(
|
||||
// dummy message to let user know that query is underway
|
||||
const newEmbed = new EmbedBuilder()
|
||||
.setTitle(`Responding to ${message.author.tag}`)
|
||||
.setDescription(response.message.content || 'No Content to Provided...')
|
||||
.setDescription(response.message.content || 'No Content to Provide...')
|
||||
.setColor('#00FF00')
|
||||
|
||||
// edit the message
|
||||
sentMessage.edit({ embeds: [newEmbed] })
|
||||
} 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()
|
||||
.setTitle(`Responding to ${message.author.tag}`)
|
||||
.setDescription(`Issue creating response: ${error.message}`)
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { Message } from 'discord.js'
|
||||
import ollama, { ChatResponse } from 'ollama'
|
||||
import { UserMessage } from './events.js'
|
||||
|
||||
/**
|
||||
* Method to send replies as normal text on discord like any other user
|
||||
@@ -13,10 +14,7 @@ export function normalMessage(
|
||||
channel: string,
|
||||
model: string
|
||||
},
|
||||
msgHist: {
|
||||
role: string,
|
||||
content: string
|
||||
}[]
|
||||
msgHist: UserMessage[]
|
||||
) {
|
||||
// bot's respnse
|
||||
let response: ChatResponse
|
||||
|
||||
@@ -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
|
||||
* This method is used for development purposes and testing
|
||||
*
|
||||
* @param stream Axios response to from Ollama
|
||||
*/
|
||||
export function parseStream(stream: AxiosResponse<any, any>) {
|
||||
|
||||
Reference in New Issue
Block a user