* added message style command * docker setup scripts * reformat messageStyle.ts * fix: register unregister on deploy * add: messageStream preference * add: json config handler * update: messageCreate gets config * update: shifted chat to config callback * fix: naming conventions based on discord * update: setup in docs now * add: static docker ips * version increment * add: bot message for no config * fix: no config case * add: clarification for subnetting * update: version increment in lock file --------- Co-authored-by: JT2M0L3Y <jtsmoley@icloud.com>
67 lines
2.1 KiB
TypeScript
67 lines
2.1 KiB
TypeScript
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
|
|
* @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,
|
|
ollama: Ollama,
|
|
tokens: {
|
|
channel: string,
|
|
model: string
|
|
},
|
|
msgHist: UserMessage[]
|
|
) {
|
|
// 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
|
|
})
|
|
|
|
// 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 Provide...')
|
|
.setColor('#00FF00')
|
|
|
|
// edit the message
|
|
sentMessage.edit({ embeds: [newEmbed] })
|
|
} catch(error: any) {
|
|
console.log(`[Util: messageEmbed] Error creating message: ${error.message}`)
|
|
const errorEmbed = new EmbedBuilder()
|
|
.setTitle(`Responding to ${message.author.tag}`)
|
|
.setDescription(`**Response generation failed.**\n\nReason: ${error.message}`)
|
|
.setColor('#00FF00')
|
|
|
|
// send back error
|
|
sentMessage.edit({ embeds: [errorEmbed] })
|
|
}
|
|
|
|
// Hope there is a response! undefined otherwie
|
|
return response!!
|
|
} |