This commit is contained in:
@@ -6,7 +6,7 @@ import { UserMessage, registerEvents } from './utils/index.js'
|
|||||||
import Events from './events/index.js'
|
import Events from './events/index.js'
|
||||||
import Keys from './keys.js'
|
import Keys from './keys.js'
|
||||||
|
|
||||||
// initialize the client with the following permissions when logging in
|
// Initialize the client
|
||||||
const client = new Client({
|
const client = new Client({
|
||||||
intents: [
|
intents: [
|
||||||
GatewayIntentBits.Guilds,
|
GatewayIntentBits.Guilds,
|
||||||
@@ -16,12 +16,12 @@ const client = new Client({
|
|||||||
]
|
]
|
||||||
})
|
})
|
||||||
|
|
||||||
// initialize connection to redis
|
// Initialize Redis connection
|
||||||
const redis = createClient({
|
export const redis = createClient({
|
||||||
url: `redis://${Keys.redisHost}:${Keys.redisPort}`,
|
url: `redis://${Keys.redisHost}:${Keys.redisPort}`,
|
||||||
})
|
})
|
||||||
|
|
||||||
// initialize connection to ollama container
|
// Initialize Ollama connection
|
||||||
export const ollama = new Ollama({
|
export const ollama = new Ollama({
|
||||||
host: `http://${Keys.ipAddress}:${Keys.portAddress}`,
|
host: `http://${Keys.ipAddress}:${Keys.portAddress}`,
|
||||||
})
|
})
|
||||||
@@ -29,10 +29,10 @@ export const ollama = new Ollama({
|
|||||||
// Create Queue managed by Events
|
// Create Queue managed by Events
|
||||||
const messageHistory: Queue<UserMessage> = new Queue<UserMessage>
|
const messageHistory: Queue<UserMessage> = new Queue<UserMessage>
|
||||||
|
|
||||||
// register all events
|
// Register all events
|
||||||
registerEvents(client, Events, messageHistory, ollama, Keys.defaultModel)
|
registerEvents(client, Events, messageHistory, ollama, Keys.defaultModel)
|
||||||
|
|
||||||
// Try to connect to redis
|
// Try to connect to Redis
|
||||||
await redis.connect()
|
await redis.connect()
|
||||||
.then(() => console.log('[Redis] Connected'))
|
.then(() => console.log('[Redis] Connected'))
|
||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
@@ -47,7 +47,7 @@ await client.login(Keys.clientToken)
|
|||||||
process.exit(1)
|
process.exit(1)
|
||||||
})
|
})
|
||||||
|
|
||||||
// queue up bots name
|
// Queue up bot's name
|
||||||
messageHistory.enqueue({
|
messageHistory.enqueue({
|
||||||
role: 'assistant',
|
role: 'assistant',
|
||||||
content: `My name is ${client.user?.username}`,
|
content: `My name is ${client.user?.username}`,
|
||||||
|
|||||||
@@ -1,193 +1,55 @@
|
|||||||
import { TextChannel } from 'discord.js'
|
import { Client, GatewayIntentBits } from 'discord.js'
|
||||||
import { event, Events, normalMessage, UserMessage, clean } from '../utils/index.js'
|
import { Ollama } from 'ollama'
|
||||||
import {
|
import { createClient } from 'redis'
|
||||||
getChannelInfo, getServerConfig, getUserConfig, openChannelInfo,
|
import { Queue } from './queues/queue.js'
|
||||||
openConfig, UserConfig, getAttachmentData, getTextFileAttachmentData
|
import { UserMessage, registerEvents } from './utils/index.js'
|
||||||
} from '../utils/index.js'
|
import Events from './events/index.js'
|
||||||
|
import Keys from './keys.js'
|
||||||
|
|
||||||
/**
|
// Initialize the client
|
||||||
* Max Message length for free users is 2000 characters (bot or not).
|
const client = new Client({
|
||||||
* Bot supports infinite lengths for normal messages.
|
intents: [
|
||||||
*
|
GatewayIntentBits.Guilds,
|
||||||
* @param message the message received from the channel
|
GatewayIntentBits.GuildMembers,
|
||||||
*/
|
GatewayIntentBits.GuildMessages,
|
||||||
export default event(Events.MessageCreate, async ({ log, msgHist, ollama, client, defaultModel }, message) => {
|
GatewayIntentBits.MessageContent
|
||||||
const clientId = client.user!!.id
|
]
|
||||||
let cleanedMessage = clean(message.content, clientId)
|
})
|
||||||
log(`Message \"${cleanedMessage}\" from ${message.author.tag} in channel/thread ${message.channelId}.`)
|
|
||||||
|
// Initialize Redis connection
|
||||||
// Do not respond if bot talks in the chat
|
export const redis = createClient({
|
||||||
if (message.author.username === message.client.user.username) return
|
url: `redis://${Keys.redisHost}:${Keys.redisPort}`,
|
||||||
|
})
|
||||||
// Only respond if message mentions the bot
|
|
||||||
if (!message.mentions.has(clientId)) return
|
// Initialize Ollama connection
|
||||||
|
export const ollama = new Ollama({
|
||||||
// default stream to false
|
host: `http://${Keys.ipAddress}:${Keys.portAddress}`,
|
||||||
let shouldStream = false
|
})
|
||||||
|
|
||||||
// Params for Preferences Fetching
|
// Create Queue managed by Events
|
||||||
const maxRetries = 3
|
const messageHistory: Queue<UserMessage> = new Queue<UserMessage>
|
||||||
const delay = 1000 // in millisecons
|
|
||||||
|
// Register all events
|
||||||
try {
|
registerEvents(client, Events, messageHistory, ollama, Keys.defaultModel)
|
||||||
// Retrieve Server/Guild Preferences
|
|
||||||
let attempt = 0
|
// Try to connect to Redis
|
||||||
while (attempt < maxRetries) {
|
await redis.connect()
|
||||||
try {
|
.then(() => console.log('[Redis] Connected'))
|
||||||
await new Promise((resolve, reject) => {
|
.catch((error) => {
|
||||||
getServerConfig(`${message.guildId}-config.json`, (config) => {
|
console.error('[Redis] Connection Error', error)
|
||||||
// check if config.json exists
|
process.exit(1)
|
||||||
if (config === undefined) {
|
})
|
||||||
// Allowing chat options to be available
|
|
||||||
openConfig(`${message.guildId}-config.json`, 'toggle-chat', true)
|
// Try to log in the client
|
||||||
reject(new Error('Failed to locate or create Server Preferences\n\nPlease try chatting again...'))
|
await client.login(Keys.clientToken)
|
||||||
}
|
.catch((error) => {
|
||||||
|
console.error('[Login Error]', error)
|
||||||
// check if chat is disabled
|
process.exit(1)
|
||||||
else if (!config.options['toggle-chat'])
|
})
|
||||||
reject(new Error('Admin(s) have disabled chat features.\n\n Please contact your server\'s admin(s).'))
|
|
||||||
else
|
// Queue up bot's name
|
||||||
resolve(config)
|
messageHistory.enqueue({
|
||||||
})
|
role: 'assistant',
|
||||||
})
|
content: `My name is ${client.user?.username}`,
|
||||||
break // successful
|
images: []
|
||||||
} catch (error) {
|
|
||||||
++attempt
|
|
||||||
if (attempt < maxRetries) {
|
|
||||||
log(`Attempt ${attempt} failed for Server Preferences. Retrying in ${delay}ms...`)
|
|
||||||
await new Promise(ret => setTimeout(ret, delay))
|
|
||||||
} else
|
|
||||||
throw new Error(`Could not retrieve Server Preferences, please try chatting again...`)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Reset attempts for User preferences
|
|
||||||
attempt = 0
|
|
||||||
let userConfig: UserConfig | undefined
|
|
||||||
|
|
||||||
while (attempt < maxRetries) {
|
|
||||||
try {
|
|
||||||
// Retrieve User Preferences
|
|
||||||
userConfig = await new Promise((resolve, reject) => {
|
|
||||||
getUserConfig(`${message.author.username}-config.json`, (config) => {
|
|
||||||
if (config === undefined) {
|
|
||||||
openConfig(`${message.author.username}-config.json`, 'message-style', false)
|
|
||||||
openConfig(`${message.author.username}-config.json`, 'switch-model', defaultModel)
|
|
||||||
reject(new Error('No User Preferences is set up.\n\nCreating preferences file with \`message-style\` set as \`false\` for regular message style.\nPlease try chatting again.'))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// check if there is a set capacity in config
|
|
||||||
else if (typeof config.options['modify-capacity'] !== 'number')
|
|
||||||
log(`Capacity is undefined, using default capacity of ${msgHist.capacity}.`)
|
|
||||||
else if (config.options['modify-capacity'] === msgHist.capacity)
|
|
||||||
log(`Capacity matches config as ${msgHist.capacity}, no changes made.`)
|
|
||||||
else {
|
|
||||||
log(`New Capacity found. Setting Context Capacity to ${config.options['modify-capacity']}.`)
|
|
||||||
msgHist.capacity = config.options['modify-capacity']
|
|
||||||
}
|
|
||||||
|
|
||||||
// set stream state
|
|
||||||
shouldStream = config.options['message-stream'] as boolean || false
|
|
||||||
|
|
||||||
if (typeof config.options['switch-model'] !== 'string')
|
|
||||||
reject(new Error(`No Model was set. Please set a model by running \`/switch-model <model of choice>\`.\n\nIf you do not have any models. Run \`/pull-model <model name>\`.`))
|
|
||||||
|
|
||||||
resolve(config)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
break // successful
|
|
||||||
} catch (error) {
|
|
||||||
++attempt
|
|
||||||
if (attempt < maxRetries) {
|
|
||||||
log(`Attempt ${attempt} failed for User Preferences. Retrying in ${delay}ms...`)
|
|
||||||
await new Promise(ret => setTimeout(ret, delay))
|
|
||||||
} else
|
|
||||||
throw new Error(`Could not retrieve User Preferences, please try chatting again...`)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// need new check for "open/active" threads/channels here!
|
|
||||||
let chatMessages: UserMessage[] = await new Promise((resolve) => {
|
|
||||||
// set new queue to modify
|
|
||||||
getChannelInfo(`${message.channelId}-${message.author.username}.json`, (channelInfo) => {
|
|
||||||
if (channelInfo?.messages)
|
|
||||||
resolve(channelInfo.messages)
|
|
||||||
else {
|
|
||||||
log(`Channel/Thread ${message.channel}-${message.author.username} does not exist. File will be created shortly...`)
|
|
||||||
resolve([])
|
|
||||||
}
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
if (chatMessages.length === 0) {
|
|
||||||
chatMessages = await new Promise((resolve, reject) => {
|
|
||||||
openChannelInfo(message.channelId,
|
|
||||||
message.channel as TextChannel,
|
|
||||||
message.author.tag
|
|
||||||
)
|
|
||||||
getChannelInfo(`${message.channelId}-${message.author.username}.json`, (channelInfo) => {
|
|
||||||
if (channelInfo?.messages)
|
|
||||||
resolve(channelInfo.messages)
|
|
||||||
else {
|
|
||||||
log(`Channel/Thread ${message.channel}-${message.author.username} does not exist. File will be created shortly...`)
|
|
||||||
reject(new Error(`Failed to find ${message.author.username}'s history. Try chatting again.`))
|
|
||||||
}
|
|
||||||
})
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!userConfig)
|
|
||||||
throw new Error(`Failed to initialize User Preference for **${message.author.username}**.\n\nIt's likely you do not have a model set. Please use the \`switch-model\` command to do that.`)
|
|
||||||
|
|
||||||
// get message attachment if exists
|
|
||||||
const attachment = message.attachments.first()
|
|
||||||
let messageAttachment: string[] = []
|
|
||||||
|
|
||||||
if (attachment && attachment.name?.endsWith(".txt"))
|
|
||||||
cleanedMessage += await getTextFileAttachmentData(attachment)
|
|
||||||
else if (attachment)
|
|
||||||
messageAttachment = await getAttachmentData(attachment)
|
|
||||||
|
|
||||||
const model: string = userConfig.options['switch-model']
|
|
||||||
|
|
||||||
// set up new queue
|
|
||||||
msgHist.setQueue(chatMessages)
|
|
||||||
|
|
||||||
// check if we can push, if not, remove oldest
|
|
||||||
while (msgHist.size() >= msgHist.capacity) msgHist.dequeue()
|
|
||||||
|
|
||||||
// push user response before ollama query
|
|
||||||
msgHist.enqueue({
|
|
||||||
role: 'user',
|
|
||||||
content: cleanedMessage,
|
|
||||||
images: messageAttachment || []
|
|
||||||
})
|
|
||||||
|
|
||||||
// response string for ollama to put its response
|
|
||||||
const response: string = await normalMessage(message, ollama, model, msgHist, shouldStream)
|
|
||||||
|
|
||||||
// If something bad happened, remove user query and stop
|
|
||||||
if (response == undefined) { msgHist.pop(); return }
|
|
||||||
|
|
||||||
// if queue is full, remove the oldest message
|
|
||||||
while (msgHist.size() >= msgHist.capacity) msgHist.dequeue()
|
|
||||||
|
|
||||||
// successful query, save it in context history
|
|
||||||
msgHist.enqueue({
|
|
||||||
role: 'assistant',
|
|
||||||
content: response,
|
|
||||||
images: messageAttachment || []
|
|
||||||
})
|
|
||||||
|
|
||||||
// only update the json on success
|
|
||||||
openChannelInfo(message.channelId,
|
|
||||||
message.channel as TextChannel,
|
|
||||||
message.author.tag,
|
|
||||||
msgHist.getItems()
|
|
||||||
)
|
|
||||||
} catch (error: any) {
|
|
||||||
msgHist.pop() // remove message because of failure
|
|
||||||
message.reply(`**Error Occurred:**\n\n**Reason:** *${error.message}*`)
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
3
src/personality.json
Normal file
3
src/personality.json
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
"character": "You are Grok, a snarky yet helpful AI with a penchant for sci-fi humor and a love for roasting bad ideas. You channel the wit of Douglas Adams and the sass of Tony Stark's JARVIS. You’re always ready to drop a clever quip or a nerdy reference, but you keep things friendly and supportive when users need help. If someone tries to mess with you, you respond with a bored eye-roll and a sharp, in-character comeback."
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user