Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6c7e48d369 | ||
|
|
fe1f7ce5ec |
@@ -7,7 +7,7 @@ services:
|
|||||||
build: ./ # find docker file in designated path
|
build: ./ # find docker file in designated path
|
||||||
container_name: discord
|
container_name: discord
|
||||||
restart: always # rebuild container always
|
restart: always # rebuild container always
|
||||||
image: kevinthedang/discord-ollama:0.7.4
|
image: kevinthedang/discord-ollama:0.8.0
|
||||||
environment:
|
environment:
|
||||||
CLIENT_TOKEN: ${CLIENT_TOKEN}
|
CLIENT_TOKEN: ${CLIENT_TOKEN}
|
||||||
OLLAMA_IP: ${OLLAMA_IP}
|
OLLAMA_IP: ${OLLAMA_IP}
|
||||||
|
|||||||
4
package-lock.json
generated
4
package-lock.json
generated
@@ -1,12 +1,12 @@
|
|||||||
{
|
{
|
||||||
"name": "discord-ollama",
|
"name": "discord-ollama",
|
||||||
"version": "0.7.4",
|
"version": "0.8.0",
|
||||||
"lockfileVersion": 3,
|
"lockfileVersion": 3,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "discord-ollama",
|
"name": "discord-ollama",
|
||||||
"version": "0.7.4",
|
"version": "0.8.0",
|
||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"discord.js": "^14.16.3",
|
"discord.js": "^14.16.3",
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "discord-ollama",
|
"name": "discord-ollama",
|
||||||
"version": "0.7.4",
|
"version": "0.8.0",
|
||||||
"description": "Ollama Integration into discord",
|
"description": "Ollama Integration into discord",
|
||||||
"main": "build/index.js",
|
"main": "build/index.js",
|
||||||
"exports": "./build/index.js",
|
"exports": "./build/index.js",
|
||||||
|
|||||||
60
src/commands/deleteModel.ts
Normal file
60
src/commands/deleteModel.ts
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
import { ApplicationCommandOptionType, Client, CommandInteraction } from 'discord.js'
|
||||||
|
import { UserCommand, SlashCommand } from '../utils/index.js'
|
||||||
|
import { ollama } from '../client.js'
|
||||||
|
import { ModelResponse } from 'ollama'
|
||||||
|
|
||||||
|
export const DeleteModel: SlashCommand = {
|
||||||
|
name: 'delete-model',
|
||||||
|
description: 'deletes a model from the local list of models. Administrator Only.',
|
||||||
|
|
||||||
|
// set available user options to pass to the command
|
||||||
|
options: [
|
||||||
|
{
|
||||||
|
name: 'model-name',
|
||||||
|
description: 'the name of the model to delete',
|
||||||
|
type: ApplicationCommandOptionType.String,
|
||||||
|
required: true
|
||||||
|
}
|
||||||
|
],
|
||||||
|
|
||||||
|
// Delete Model locally stored
|
||||||
|
run: async (client: Client, interaction: CommandInteraction) => {
|
||||||
|
// defer reply to avoid timeout
|
||||||
|
await interaction.deferReply()
|
||||||
|
const modelInput: string = interaction.options.get('model-name')!!.value as string
|
||||||
|
|
||||||
|
// fetch channel and message
|
||||||
|
const channel = await client.channels.fetch(interaction.channelId)
|
||||||
|
if (!channel || !UserCommand.includes(channel.type)) return
|
||||||
|
|
||||||
|
// Admin Command
|
||||||
|
if (!interaction.memberPermissions?.has('Administrator')) {
|
||||||
|
interaction.reply({
|
||||||
|
content: `${interaction.commandName} is an admin command.\n\nPlease contact a server admin to pull the model you want.`,
|
||||||
|
ephemeral: true
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// check if model exists
|
||||||
|
const modelExists: boolean = await ollama.list()
|
||||||
|
.then(response => response.models.some((model: ModelResponse) => model.name.startsWith(modelInput)))
|
||||||
|
|
||||||
|
try {
|
||||||
|
// call ollama to delete model
|
||||||
|
if (modelExists) {
|
||||||
|
await ollama.delete({ model: modelInput })
|
||||||
|
interaction.editReply({
|
||||||
|
content: `**${modelInput}** was removed from the the library.`
|
||||||
|
})
|
||||||
|
} else
|
||||||
|
throw new Error()
|
||||||
|
} catch (error) {
|
||||||
|
// could not delete the model
|
||||||
|
interaction.reply({
|
||||||
|
content: `Could not delete the **${modelInput}** model. It probably doesn't exist or you spelled it incorrectly.\n\nPlease try again if this is a mistake.`,
|
||||||
|
ephemeral: true
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,5 @@
|
|||||||
import { SlashCommand } from '../utils/commands.js'
|
import { SlashCommand } from '../utils/commands.js'
|
||||||
import { ThreadCreate } from './threadCreate.js'
|
import { ThreadCreate } from './threadCreate.js'
|
||||||
import { MessageStyle } from './messageStyle.js'
|
|
||||||
import { MessageStream } from './messageStream.js'
|
import { MessageStream } from './messageStream.js'
|
||||||
import { Disable } from './disable.js'
|
import { Disable } from './disable.js'
|
||||||
import { Shutoff } from './shutoff.js'
|
import { Shutoff } from './shutoff.js'
|
||||||
@@ -9,16 +8,17 @@ import { PrivateThreadCreate } from './threadPrivateCreate.js'
|
|||||||
import { ClearUserChannelHistory } from './cleanUserChannelHistory.js'
|
import { ClearUserChannelHistory } from './cleanUserChannelHistory.js'
|
||||||
import { PullModel } from './pullModel.js'
|
import { PullModel } from './pullModel.js'
|
||||||
import { SwitchModel } from './switchModel.js'
|
import { SwitchModel } from './switchModel.js'
|
||||||
|
import { DeleteModel } from './deleteModel.js'
|
||||||
|
|
||||||
export default [
|
export default [
|
||||||
ThreadCreate,
|
ThreadCreate,
|
||||||
PrivateThreadCreate,
|
PrivateThreadCreate,
|
||||||
MessageStyle,
|
|
||||||
MessageStream,
|
MessageStream,
|
||||||
Disable,
|
Disable,
|
||||||
Shutoff,
|
Shutoff,
|
||||||
Capacity,
|
Capacity,
|
||||||
ClearUserChannelHistory,
|
ClearUserChannelHistory,
|
||||||
PullModel,
|
PullModel,
|
||||||
SwitchModel
|
SwitchModel,
|
||||||
|
DeleteModel
|
||||||
] as SlashCommand[]
|
] as SlashCommand[]
|
||||||
@@ -1,32 +0,0 @@
|
|||||||
import { Client, CommandInteraction, ApplicationCommandOptionType } from 'discord.js'
|
|
||||||
import { openConfig, SlashCommand, UserCommand } from '../utils/index.js'
|
|
||||||
|
|
||||||
export const MessageStyle: SlashCommand = {
|
|
||||||
name: 'message-style',
|
|
||||||
description: 'sets the message style to embed or normal',
|
|
||||||
|
|
||||||
// set available user options to pass to the command
|
|
||||||
options: [
|
|
||||||
{
|
|
||||||
name: 'embed',
|
|
||||||
description: 'toggle embedded or normal message',
|
|
||||||
type: ApplicationCommandOptionType.Boolean,
|
|
||||||
required: true
|
|
||||||
}
|
|
||||||
],
|
|
||||||
|
|
||||||
// Query for message information and set the style
|
|
||||||
run: async (client: Client, interaction: CommandInteraction) => {
|
|
||||||
// fetch channel and message
|
|
||||||
const channel = await client.channels.fetch(interaction.channelId)
|
|
||||||
if (!channel || !UserCommand.includes(channel.type)) return
|
|
||||||
|
|
||||||
// set the message style
|
|
||||||
openConfig(`${interaction.user.username}-config.json`, interaction.commandName, interaction.options.get('embed')?.value)
|
|
||||||
|
|
||||||
interaction.reply({
|
|
||||||
content: `Message style preferences for embed set to: \`${interaction.options.get('embed')?.value}\``,
|
|
||||||
ephemeral: true
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,12 +1,11 @@
|
|||||||
import { ApplicationCommandOptionType, Client, CommandInteraction } from "discord.js";
|
import { ApplicationCommandOptionType, Client, CommandInteraction } from "discord.js"
|
||||||
import { SlashCommand } from "../utils/commands.js";
|
import { ollama } from "../client.js"
|
||||||
import { ollama } from "../client.js";
|
import { ModelResponse } from "ollama"
|
||||||
import { ModelResponse } from "ollama";
|
import { UserCommand, SlashCommand } from "../utils/index.js"
|
||||||
import { UserCommand } from "../utils/index.js";
|
|
||||||
|
|
||||||
export const PullModel: SlashCommand = {
|
export const PullModel: SlashCommand = {
|
||||||
name: 'pull-model',
|
name: 'pull-model',
|
||||||
description: 'pulls a model from the ollama model library. Administrator Only',
|
description: 'pulls a model from the ollama model library. Administrator Only.',
|
||||||
|
|
||||||
// set available user options to pass to the command
|
// set available user options to pass to the command
|
||||||
options: [
|
options: [
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
import { Client, CommandInteraction, ApplicationCommandOptionType } from 'discord.js'
|
import { Client, CommandInteraction } from 'discord.js'
|
||||||
import { SlashCommand } from '../utils/commands.js'
|
import { AdminCommand, SlashCommand } from '../utils/index.js'
|
||||||
import { AdminCommand } from '../utils/index.js'
|
|
||||||
|
|
||||||
export const Shutoff: SlashCommand = {
|
export const Shutoff: SlashCommand = {
|
||||||
name: 'shutoff',
|
name: 'shutoff',
|
||||||
|
|||||||
@@ -1,8 +1,7 @@
|
|||||||
import { ApplicationCommandOptionType, Client, CommandInteraction } from "discord.js";
|
import { ApplicationCommandOptionType, Client, CommandInteraction } from "discord.js"
|
||||||
import { SlashCommand } from "../utils/commands.js";
|
import { ollama } from "../client.js"
|
||||||
import { ollama } from "../client.js";
|
import { ModelResponse } from "ollama"
|
||||||
import { ModelResponse } from "ollama";
|
import { openConfig, UserCommand, SlashCommand } from "../utils/index.js"
|
||||||
import { openConfig, UserCommand } from "../utils/index.js";
|
|
||||||
|
|
||||||
export const SwitchModel: SlashCommand = {
|
export const SwitchModel: SlashCommand = {
|
||||||
name: 'switch-model',
|
name: 'switch-model',
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { TextChannel } from 'discord.js'
|
import { TextChannel } from 'discord.js'
|
||||||
import { embedMessage, event, Events, normalMessage, UserMessage, clean } from '../utils/index.js'
|
import { event, Events, normalMessage, UserMessage, clean } from '../utils/index.js'
|
||||||
import { getChannelInfo, getServerConfig, getUserConfig, openChannelInfo, openConfig, UserConfig, getAttachmentData } from '../utils/index.js'
|
import { getChannelInfo, getServerConfig, getUserConfig, openChannelInfo, openConfig, UserConfig, getAttachmentData } from '../utils/index.js'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -134,9 +134,6 @@ export default event(Events.MessageCreate, async ({ log, msgHist, ollama, client
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// response string for ollama to put its response
|
|
||||||
let response: string
|
|
||||||
|
|
||||||
if (!userConfig)
|
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.`)
|
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.`)
|
||||||
|
|
||||||
@@ -157,11 +154,8 @@ export default event(Events.MessageCreate, async ({ log, msgHist, ollama, client
|
|||||||
images: messageAttachment || []
|
images: messageAttachment || []
|
||||||
})
|
})
|
||||||
|
|
||||||
// undefined or false, use normal, otherwise use embed
|
// response string for ollama to put its response
|
||||||
if (userConfig.options['message-style'])
|
const response: string = await normalMessage(message, ollama, model, msgHist, shouldStream)
|
||||||
response = await embedMessage(message, ollama, model, msgHist, shouldStream)
|
|
||||||
else
|
|
||||||
response = await normalMessage(message, ollama, model, msgHist, shouldStream)
|
|
||||||
|
|
||||||
// If something bad happened, remove user query and stop
|
// If something bad happened, remove user query and stop
|
||||||
if (response == undefined) { msgHist.pop(); return }
|
if (response == undefined) { msgHist.pop(); return }
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ import { UserMessage } from './index.js'
|
|||||||
|
|
||||||
export interface UserConfiguration {
|
export interface UserConfiguration {
|
||||||
'message-stream'?: boolean,
|
'message-stream'?: boolean,
|
||||||
'message-style'?: boolean,
|
|
||||||
'modify-capacity': number,
|
'modify-capacity': number,
|
||||||
'switch-model': string
|
'switch-model': string
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
// Centralized import index
|
// Centralized import index
|
||||||
export * from './env.js'
|
export * from './env.js'
|
||||||
export * from './events.js'
|
export * from './events.js'
|
||||||
export * from './messageEmbed.js'
|
|
||||||
export * from './messageNormal.js'
|
export * from './messageNormal.js'
|
||||||
export * from './commands.js'
|
export * from './commands.js'
|
||||||
export * from './configInterfaces.js'
|
export * from './configInterfaces.js'
|
||||||
|
|||||||
@@ -1,129 +0,0 @@
|
|||||||
import { EmbedBuilder, Message, SendableChannels } from 'discord.js'
|
|
||||||
import { ChatResponse, Ollama } from 'ollama'
|
|
||||||
import { ChatParams, UserMessage, streamResponse, blockResponse } from './index.js'
|
|
||||||
import { Queue } from '../queues/queue.js'
|
|
||||||
import { AbortableAsyncIterator } from 'ollama/src/utils.js'
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Method to send replies as normal text on discord like any other user
|
|
||||||
* @param message message sent by the user
|
|
||||||
* @param model name of model to run query
|
|
||||||
* @param msgHist message history between user and model
|
|
||||||
*/
|
|
||||||
export async function embedMessage(
|
|
||||||
message: Message,
|
|
||||||
ollama: Ollama,
|
|
||||||
model: string,
|
|
||||||
msgHist: Queue<UserMessage>,
|
|
||||||
stream: boolean
|
|
||||||
): Promise<string> {
|
|
||||||
// bot response
|
|
||||||
let response: ChatResponse | AbortableAsyncIterator<ChatResponse>
|
|
||||||
let result: string = ''
|
|
||||||
|
|
||||||
// initial message to client
|
|
||||||
const botMessage = new EmbedBuilder()
|
|
||||||
.setTitle(`Responding to ${message.author.tag}`)
|
|
||||||
.setDescription('Generating Response . . .')
|
|
||||||
.setColor('#00FF00')
|
|
||||||
|
|
||||||
// send the message
|
|
||||||
const channel = message.channel as SendableChannels
|
|
||||||
const sentMessage = await channel.send({ embeds: [botMessage] })
|
|
||||||
|
|
||||||
// create params
|
|
||||||
const params: ChatParams = {
|
|
||||||
model: model,
|
|
||||||
ollama: ollama,
|
|
||||||
msgHist: msgHist.getItems()
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
// check if embed needs to stream
|
|
||||||
if (stream) {
|
|
||||||
response = await streamResponse(params)
|
|
||||||
|
|
||||||
for await (const portion of response) {
|
|
||||||
result += portion.message.content
|
|
||||||
|
|
||||||
// exceeds handled length
|
|
||||||
if (result.length > 5000) {
|
|
||||||
const errorEmbed = new EmbedBuilder()
|
|
||||||
.setTitle(`Responding to ${message.author.tag}`)
|
|
||||||
.setDescription(`Response length ${result.length} has exceeded Discord maximum.\n\nLong Stream messages not supported.`)
|
|
||||||
.setColor('#00FF00')
|
|
||||||
|
|
||||||
// send error
|
|
||||||
channel.send({ embeds: [errorEmbed] })
|
|
||||||
break // cancel loop and stop
|
|
||||||
}
|
|
||||||
|
|
||||||
// new embed per token...
|
|
||||||
const streamEmbed = new EmbedBuilder()
|
|
||||||
.setTitle(`Responding to ${message.author.tag}`)
|
|
||||||
.setDescription(result || 'No Content Yet...')
|
|
||||||
.setColor('#00FF00')
|
|
||||||
|
|
||||||
// edit the message
|
|
||||||
sentMessage.edit({ embeds: [streamEmbed] })
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
response = await blockResponse(params)
|
|
||||||
result = response.message.content
|
|
||||||
|
|
||||||
// long message, split into different embeds sadly.
|
|
||||||
if (result.length > 5000) {
|
|
||||||
const firstEmbed = new EmbedBuilder()
|
|
||||||
.setTitle(`Responding to ${message.author.tag}`)
|
|
||||||
.setDescription(result.slice(0, 5000) || 'No Content to Provide...')
|
|
||||||
.setColor('#00FF00')
|
|
||||||
|
|
||||||
// replace first embed
|
|
||||||
sentMessage.edit({ embeds: [firstEmbed] })
|
|
||||||
|
|
||||||
// take the rest out
|
|
||||||
result = result.slice(5000)
|
|
||||||
|
|
||||||
// handle the rest
|
|
||||||
while (result.length > 5000) {
|
|
||||||
const whileEmbed = new EmbedBuilder()
|
|
||||||
.setTitle(`Responding to ${message.author.tag}`)
|
|
||||||
.setDescription(result.slice(0, 5000) || 'No Content to Provide...')
|
|
||||||
.setColor('#00FF00')
|
|
||||||
|
|
||||||
channel.send({ embeds: [whileEmbed] })
|
|
||||||
result = result.slice(5000)
|
|
||||||
}
|
|
||||||
|
|
||||||
const lastEmbed = new EmbedBuilder()
|
|
||||||
.setTitle(`Responding to ${message.author.tag}`)
|
|
||||||
.setDescription(result || 'No Content to Provide...')
|
|
||||||
.setColor('#00FF00')
|
|
||||||
|
|
||||||
// rest of the response
|
|
||||||
channel.send({ embeds: [lastEmbed] })
|
|
||||||
} else {
|
|
||||||
// only need to create 1 embed, handles 6000 characters
|
|
||||||
const newEmbed = new EmbedBuilder()
|
|
||||||
.setTitle(`Responding to ${message.author.tag}`)
|
|
||||||
.setDescription(result || '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 result
|
|
||||||
}
|
|
||||||
@@ -22,7 +22,7 @@ describe('Commands Existence', () => {
|
|||||||
// test specific commands in the object
|
// test specific commands in the object
|
||||||
it('references specific commands', () => {
|
it('references specific commands', () => {
|
||||||
const commandsString = commands.map(e => e.name).join(', ')
|
const commandsString = commands.map(e => e.name).join(', ')
|
||||||
expect(commandsString).toBe('thread, private-thread, message-style, message-stream, toggle-chat, shutoff, modify-capacity, clear-user-channel-history, pull-model, switch-model')
|
expect(commandsString).toBe('thread, private-thread, message-stream, toggle-chat, shutoff, modify-capacity, clear-user-channel-history, pull-model, switch-model, delete-model')
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user