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

@@ -1,90 +1,39 @@
import { event, Events } from '../utils/index.js'
import { EmbedBuilder } from 'discord.js'
import ollama from 'ollama'
import Axios from 'axios'
import { embedMessage, event, Events } from '../utils/index.js'
/**
* Max Message length for free users is 2000 characters (bot or not).
* @param message the message received from the channel
*/
export default event(Events.MessageCreate, async ({ log, msgHist }, message) => {
export default event(Events.MessageCreate, async ({ log, msgHist, tokens }, message) => {
log(`Message created \"${message.content}\" from ${message.author.tag}.`)
// Hard-coded channel to test output there only, in our case "ollama-endpoint"
if (message.channelId != '1188262786497785896') return
if (message.channelId != tokens.channel) return
// Do not respond if bot talks in the chat
if (message.author.tag === message.client.user.tag) return
// Only respond if message mentions the bot
if (!message.mentions.has(tokens.botUid)) return
// push user response
msgHist.push({
role: 'user',
content: message.content
})
const botMessage = new EmbedBuilder()
.setTitle(`Response to ${message.author.tag}`)
.setDescription('Generating Response . . .')
.setColor('#00FF00')
// Try to query and send embed
const response = await embedMessage(message, tokens, msgHist)
const sentMessage = await message.channel.send({ embeds: [botMessage] })
// Try to query and send message
// log(normalMessage(message, tokens, msgHist))
const request = async () => {
try {
// change this when using an actual hosted server or use ollama.js
const response = await ollama.chat({
model: 'llama2',
messages: msgHist,
stream: false
})
// If something bad happened, remove user query and stop
if (response == undefined) { msgHist.pop(); return }
const embed = new EmbedBuilder()
.setTitle(`Response to ${message.author.tag}`)
.setDescription(response.message.content)
.setColor('#00FF00')
sentMessage.edit({ embeds: [embed] })
// push bot response
msgHist.push({
role: 'assistant',
content: response.message.content
})
} catch (error) {
message.edit(error as string)
log(error)
}
}
// Attempt to call ollama's endpoint
request()
// Reply with something to prompt that ollama is working, version without embed
message.reply("Generating Response . . .").then(sentMessage => {
// Request made to API
const request = async () => {
try {
// change this when using an actual hosted server or use ollama.js
const response = await Axios.post('http://127.0.0.1:11434/api/chat', {
model: 'llama2',
messages: msgHist,
stream: false
})
sentMessage.edit(response.data.message.content)
// push bot response
// msgHist.push({
// role: 'assistant',
// content: response.data.message.content
// })
} catch (error) {
message.edit(error as string)
log(error)
}
}
// Attempt to call ollama's endpoint
request()
// successful query, save it as history
msgHist.push({
role: 'assistant',
content: response.message.content
})
})