hardcoded and mentions

* added options to queries

* removed hard coded vals, added message options

* updated importing

* added check for message mentions

* fix missing botID

* updated token to uid

* added contributer

---------

Co-authored-by: JT2M0L3Y <jtsmoley@icloud.com>
This commit is contained in:
Kevin Dang
2024-01-29 12:49:04 -08:00
committed by Kevin Dang
parent 97acae3d08
commit 9247463480
8 changed files with 166 additions and 75 deletions

View File

@@ -11,6 +11,11 @@ export interface EventProps {
client: Client
log: LogMethod
msgHist: { role: string, content: string }[]
tokens: {
channel: string,
model: string,
botUid: string
}
}
export type EventCallback<T extends EventKeys> = (
props: EventProps,
@@ -27,7 +32,16 @@ export function event<T extends EventKeys>(key: T, callback: EventCallback<T>):
return { key, callback }
}
export function registerEvents(client: Client, events: Event[], msgHist: { role: string, content: string }[]): void {
export function registerEvents(
client: Client,
events: Event[],
msgHist: { role: string, content: string }[],
tokens: {
channel: string,
model: string,
botUid: string
}
): void {
for (const { key, callback } of events) {
client.on(key, (...args) => {
// Create a new log method for this event
@@ -35,7 +49,7 @@ export function registerEvents(client: Client, events: Event[], msgHist: { role:
// Handle Errors, call callback, log errors as needed
try {
callback({ client, log, msgHist }, ...args)
callback({ client, log, msgHist, tokens }, ...args)
} catch (error) {
log('[Uncaught Error]', error)
}

View File

@@ -1,3 +1,5 @@
// Centralized import index
export * from './env.js';
export * from './events.js';
export * from './env.js'
export * from './events.js'
export * from './messageEmbed.js'
export * from './messageNormal.js'

66
src/utils/messageEmbed.ts Normal file
View File

@@ -0,0 +1,66 @@
import { EmbedBuilder, Message } from "discord.js";
import ollama, { ChatResponse } from "ollama";
/**
* Method to send replies as normal text on discord like any other user
* @param message message sent by the user
* @param tokens tokens to run query
* @param msgHist message history between user and model
*/
export async function embedMessage(
message: Message,
tokens: {
channel: string,
model: string
},
msgHist: {
role: string,
content: string
}[]
) {
// bot response
let response: ChatResponse
// initial message to client
const botMessage = new EmbedBuilder()
.setTitle(`Responding to ${message.author.tag}`)
.setDescription('Generating Response . . .')
.setColor('#00FF00')
// send the message
const sentMessage = await message.channel.send({ embeds: [botMessage] })
try {
// Attempt to query model for message
response = await ollama.chat({
model: tokens.model,
messages: msgHist,
options: {
num_thread: 8, // remove if optimization needed further
mirostat: 1,
mirostat_tau: 2.0,
top_k: 70
},
stream: false
})
const newEmbed = new EmbedBuilder()
.setTitle(`Responding to ${message.author.tag}`)
.setDescription(response.message.content)
.setColor('#00FF00')
// edit the message
sentMessage.edit({ embeds: [newEmbed] })
} catch(error: any) {
const errorEmbed = new EmbedBuilder()
.setTitle(`Responding to ${message.author.tag}`)
.setDescription(error.error)
.setColor('#00FF00')
// send back error
sentMessage.edit({ embeds: [errorEmbed] })
}
// Hope there is a response! undefined otherwie
return response!!
}

View File

@@ -0,0 +1,48 @@
import { Message } from "discord.js";
import ollama, { ChatResponse } from "ollama";
/**
* Method to send replies as normal text on discord like any other user
* @param message message sent by the user
* @param tokens tokens to run query
* @param msgHist message history between user and model
*/
export function normalMessage(
message: Message,
tokens: {
channel: string,
model: string
},
msgHist: {
role: string,
content: string
}[]
) {
// bot's respnse
let response: ChatResponse
message.reply('Generating Response . . .').then(async sentMessage => {
try {
// Attempt to query model for message
response = await ollama.chat({
model: tokens.model,
messages: msgHist,
options: {
num_thread: 8, // remove if optimization needed further
mirostat: 1,
mirostat_tau: 2.0,
top_k: 70
},
stream: false
})
// edit the 'generic' response to new message
sentMessage.edit(response.message.content)
} catch(error: any) {
sentMessage.edit(error.error)
}
})
// Hope there is a response, force client to believe
return response!!
}