mirror of
https://github.com/kevinthedang/discord-ollama.git
synced 2025-12-12 11:56:06 -05:00
Pull and Switch Model Revised (#142)
* Update: pull-model only runnable by admins now * Update: switch-model cannot pull models anymore * Update: less technical responses * Update: version increment
This commit is contained in:
@@ -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.2
|
image: kevinthedang/discord-ollama:0.7.3
|
||||||
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.2",
|
"version": "0.7.3",
|
||||||
"lockfileVersion": 3,
|
"lockfileVersion": 3,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "discord-ollama",
|
"name": "discord-ollama",
|
||||||
"version": "0.7.2",
|
"version": "0.7.3",
|
||||||
"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.2",
|
"version": "0.7.3",
|
||||||
"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",
|
||||||
|
|||||||
@@ -3,13 +3,13 @@ import { openConfig, SlashCommand, UserCommand } from '../utils/index.js'
|
|||||||
|
|
||||||
export const Capacity: SlashCommand = {
|
export const Capacity: SlashCommand = {
|
||||||
name: 'modify-capacity',
|
name: 'modify-capacity',
|
||||||
description: 'number of messages bot will hold for context.',
|
description: 'maximum amount messages bot will hold for context.',
|
||||||
|
|
||||||
// set available user options to pass to the command
|
// set available user options to pass to the command
|
||||||
options: [
|
options: [
|
||||||
{
|
{
|
||||||
name: 'context-capacity',
|
name: 'context-capacity',
|
||||||
description: 'a number to set capacity',
|
description: 'number of allowed messages to remember',
|
||||||
type: ApplicationCommandOptionType.Number,
|
type: ApplicationCommandOptionType.Number,
|
||||||
required: true
|
required: true
|
||||||
}
|
}
|
||||||
@@ -25,7 +25,7 @@ export const Capacity: SlashCommand = {
|
|||||||
openConfig(`${interaction.user.username}-config.json`, interaction.commandName, interaction.options.get('context-capacity')?.value)
|
openConfig(`${interaction.user.username}-config.json`, interaction.commandName, interaction.options.get('context-capacity')?.value)
|
||||||
|
|
||||||
interaction.reply({
|
interaction.reply({
|
||||||
content: `Message History Capacity has been set to \`${interaction.options.get('context-capacity')?.value}\``,
|
content: `Max message history is now set to \`${interaction.options.get('context-capacity')?.value}\``,
|
||||||
ephemeral: true
|
ephemeral: true
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import { clearChannelInfo, SlashCommand, UserCommand } from '../utils/index.js'
|
|||||||
|
|
||||||
export const ClearUserChannelHistory: SlashCommand = {
|
export const ClearUserChannelHistory: SlashCommand = {
|
||||||
name: 'clear-user-channel-history',
|
name: 'clear-user-channel-history',
|
||||||
description: 'clears history for user running this command in current channel',
|
description: 'clears history for user in the current channel',
|
||||||
|
|
||||||
// Clear channel history for intended user
|
// Clear channel history for intended user
|
||||||
run: async (client: Client, interaction: CommandInteraction) => {
|
run: async (client: Client, interaction: CommandInteraction) => {
|
||||||
@@ -21,12 +21,12 @@ export const ClearUserChannelHistory: SlashCommand = {
|
|||||||
// check result of clearing history
|
// check result of clearing history
|
||||||
if (successfulWipe)
|
if (successfulWipe)
|
||||||
interaction.reply({
|
interaction.reply({
|
||||||
content: `Channel history in **this channel** successfully cleared for **${interaction.user.username}**.`,
|
content: `History cleared in **this channel** cleared for **${interaction.user.username}**.`,
|
||||||
ephemeral: true
|
ephemeral: true
|
||||||
})
|
})
|
||||||
else
|
else
|
||||||
interaction.reply({
|
interaction.reply({
|
||||||
content: `Channel history could not be found for **${interaction.user.username}** in **this channel**.\n\nPlease chat with **${client.user?.username}** to start a chat history.`,
|
content: `History was not be found for **${interaction.user.username}** in **this channel**.\n\nPlease chat with **${client.user?.username}** to start a chat history.`,
|
||||||
ephemeral: true
|
ephemeral: true
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
import { ChannelType, Client, CommandInteraction, ApplicationCommandOptionType } from 'discord.js'
|
import { Client, CommandInteraction, ApplicationCommandOptionType } from 'discord.js'
|
||||||
import { AdminCommand, openConfig, SlashCommand } from '../utils/index.js'
|
import { AdminCommand, openConfig, SlashCommand } from '../utils/index.js'
|
||||||
|
|
||||||
export const Disable: SlashCommand = {
|
export const Disable: SlashCommand = {
|
||||||
name: 'toggle-chat',
|
name: 'toggle-chat',
|
||||||
description: 'toggle all chat features, Adminstrator Only.',
|
description: 'toggle all chat features. Adminstrator Only.',
|
||||||
|
|
||||||
// set available user options to pass to the command
|
// set available user options to pass to the command
|
||||||
options: [
|
options: [
|
||||||
@@ -24,7 +24,7 @@ export const Disable: SlashCommand = {
|
|||||||
// check if runner is an admin
|
// check if runner is an admin
|
||||||
if (!interaction.memberPermissions?.has('Administrator')) {
|
if (!interaction.memberPermissions?.has('Administrator')) {
|
||||||
interaction.reply({
|
interaction.reply({
|
||||||
content: `${interaction.commandName} is an Administrator Command.\n\nYou, ${interaction.member?.user.username}, are not an Administrator in this server.\nPlease contact an admin to use this command.`,
|
content: `${interaction.commandName} is an admin command.\n\nPlease contact an admin to use this command for you.`,
|
||||||
ephemeral: true
|
ephemeral: true
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
@@ -34,7 +34,7 @@ export const Disable: SlashCommand = {
|
|||||||
openConfig(`${interaction.guildId}-config.json`, interaction.commandName, interaction.options.get('enabled')?.value)
|
openConfig(`${interaction.guildId}-config.json`, interaction.commandName, interaction.options.get('enabled')?.value)
|
||||||
|
|
||||||
interaction.reply({
|
interaction.reply({
|
||||||
content: `Chat features has been \`${interaction.options.get('enabled')?.value ? "enabled" : "disabled" }\``,
|
content: `${client.user?.username} is now **${interaction.options.get('enabled')?.value ? "enabled" : "disabled" }**.`,
|
||||||
ephemeral: true
|
ephemeral: true
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,13 +3,13 @@ import { openConfig, SlashCommand, UserCommand } from '../utils/index.js'
|
|||||||
|
|
||||||
export const MessageStream: SlashCommand = {
|
export const MessageStream: SlashCommand = {
|
||||||
name: 'message-stream',
|
name: 'message-stream',
|
||||||
description: 'change preference on message streaming from ollama. WARNING: can be very slow.',
|
description: 'change preference on message streaming from ollama. WARNING: can be very slow due to Discord limits.',
|
||||||
|
|
||||||
// user option(s) for setting stream
|
// user option(s) for setting stream
|
||||||
options: [
|
options: [
|
||||||
{
|
{
|
||||||
name: 'stream',
|
name: 'stream',
|
||||||
description: 'enable or disable stream preference',
|
description: 'enable or disable message streaming',
|
||||||
type: ApplicationCommandOptionType.Boolean,
|
type: ApplicationCommandOptionType.Boolean,
|
||||||
required: true
|
required: true
|
||||||
}
|
}
|
||||||
@@ -25,7 +25,7 @@ export const MessageStream: SlashCommand = {
|
|||||||
openConfig(`${interaction.user.username}-config.json`, interaction.commandName, interaction.options.get('stream')?.value)
|
openConfig(`${interaction.user.username}-config.json`, interaction.commandName, interaction.options.get('stream')?.value)
|
||||||
|
|
||||||
interaction.reply({
|
interaction.reply({
|
||||||
content: `Message streaming preferences set to: \`${interaction.options.get('stream')?.value}\``,
|
content: `Message streaming is now set to: \`${interaction.options.get('stream')?.value}\``,
|
||||||
ephemeral: true
|
ephemeral: true
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ 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',
|
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: [
|
||||||
@@ -28,18 +28,31 @@ export const PullModel: SlashCommand = {
|
|||||||
const channel = await client.channels.fetch(interaction.channelId)
|
const channel = await client.channels.fetch(interaction.channelId)
|
||||||
if (!channel || !UserCommand.includes(channel.type)) return
|
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 was already pulled
|
// check if model was already pulled
|
||||||
const modelExists: boolean = await ollama.list()
|
const modelExists: boolean = await ollama.list()
|
||||||
.then(response => response.models.some((model: ModelResponse) => model.name.startsWith(modelInput)))
|
.then(response => response.models.some((model: ModelResponse) => model.name.startsWith(modelInput)))
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// call ollama to pull desired model
|
// call ollama to pull desired model
|
||||||
if (!modelExists)
|
if (!modelExists) {
|
||||||
|
interaction.editReply({
|
||||||
|
content: `**${modelInput}** could not be found. Please wait patiently as I try to retrieve it...`
|
||||||
|
})
|
||||||
await ollama.pull({ model: modelInput })
|
await ollama.pull({ model: modelInput })
|
||||||
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// could not resolve pull or model unfound
|
// could not resolve pull or model unfound
|
||||||
interaction.editReply({
|
interaction.editReply({
|
||||||
content: `Could not pull/locate the **${modelInput}** model within the [Ollama Model Library](https://ollama.com/library).\n\nPlease check the model library and try again.`
|
content: `Could not retrieve the **${modelInput}** model. You can find models at [Ollama Model Library](https://ollama.com/library).\n\nPlease check the model library and try again.`
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -47,11 +60,11 @@ export const PullModel: SlashCommand = {
|
|||||||
// successful interaction
|
// successful interaction
|
||||||
if (modelExists)
|
if (modelExists)
|
||||||
interaction.editReply({
|
interaction.editReply({
|
||||||
content: `**${modelInput}** is already in your local model library.`
|
content: `**${modelInput}** is already available.`
|
||||||
})
|
})
|
||||||
else
|
else
|
||||||
interaction.editReply({
|
interaction.editReply({
|
||||||
content: `Successfully added **${modelInput}** into your local model library.`
|
content: `Successfully added **${modelInput}**.`
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -6,16 +6,6 @@ export const Shutoff: SlashCommand = {
|
|||||||
name: 'shutoff',
|
name: 'shutoff',
|
||||||
description: 'shutdown the bot. You will need to manually bring it online again. Administrator Only.',
|
description: 'shutdown the bot. You will need to manually bring it online again. Administrator Only.',
|
||||||
|
|
||||||
// set available user options to pass to the command
|
|
||||||
options: [
|
|
||||||
{
|
|
||||||
name: 'are-you-sure',
|
|
||||||
description: 'true = yes, false = I\'m scared',
|
|
||||||
type: ApplicationCommandOptionType.Boolean,
|
|
||||||
required: true
|
|
||||||
}
|
|
||||||
],
|
|
||||||
|
|
||||||
// Query for message information and set the style
|
// Query for message information and set the style
|
||||||
run: async (client: Client, interaction: CommandInteraction) => {
|
run: async (client: Client, interaction: CommandInteraction) => {
|
||||||
// fetch channel and message
|
// fetch channel and message
|
||||||
@@ -23,7 +13,7 @@ export const Shutoff: SlashCommand = {
|
|||||||
if (!channel || !AdminCommand.includes(channel.type)) return
|
if (!channel || !AdminCommand.includes(channel.type)) return
|
||||||
|
|
||||||
// log this, this will probably be improtant for logging who did this
|
// log this, this will probably be improtant for logging who did this
|
||||||
console.log(`User -> ${interaction.user.tag} attempting to shutdown ${client.user!!.tag}`)
|
console.log(`[Command: shutoff] User ${interaction.user.tag} attempting to shutdown ${client.user!!.tag}`)
|
||||||
|
|
||||||
// check if admin or false on shutdown
|
// check if admin or false on shutdown
|
||||||
if (!interaction.memberPermissions?.has('Administrator')) {
|
if (!interaction.memberPermissions?.has('Administrator')) {
|
||||||
@@ -32,19 +22,14 @@ export const Shutoff: SlashCommand = {
|
|||||||
ephemeral: true
|
ephemeral: true
|
||||||
})
|
})
|
||||||
return // stop from shutting down
|
return // stop from shutting down
|
||||||
} else if (!interaction.options.get('are-you-sure')?.value) {
|
|
||||||
interaction.reply({
|
|
||||||
content: `**Shutdown Aborted:**\n\n${interaction.user.tag}, You didn't want to shutoff **${client.user?.tag}**.`,
|
|
||||||
ephemeral: true
|
|
||||||
})
|
|
||||||
return // chickened out
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Shutoff cleared, do it
|
// Shutoff cleared, do it
|
||||||
interaction.reply({
|
interaction.reply({
|
||||||
content: `${client.user?.tag} is ${interaction.options.get('are-you-sure')?.value ? "shutting down now." : "not shutting down." }`,
|
content: `${client.user?.tag} is shutting down.`,
|
||||||
ephemeral: true
|
ephemeral: true
|
||||||
})
|
})
|
||||||
|
console.log(`[Command: shutoff] ${client.user?.tag} is shutting down.`)
|
||||||
|
|
||||||
// clean up client instance and stop
|
// clean up client instance and stop
|
||||||
client.destroy()
|
client.destroy()
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import { openConfig, UserCommand } from "../utils/index.js";
|
|||||||
|
|
||||||
export const SwitchModel: SlashCommand = {
|
export const SwitchModel: SlashCommand = {
|
||||||
name: 'switch-model',
|
name: 'switch-model',
|
||||||
description: 'switches current model to preferred model to use.',
|
description: 'switches current model to use.',
|
||||||
|
|
||||||
// set available user options to pass to the command
|
// set available user options to pass to the command
|
||||||
options: [
|
options: [
|
||||||
@@ -29,7 +29,7 @@ export const SwitchModel: SlashCommand = {
|
|||||||
if (!channel || !UserCommand.includes(channel.type)) return
|
if (!channel || !UserCommand.includes(channel.type)) return
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Phase 1: Set the model
|
// Phase 1: Switch to the model
|
||||||
let switchSuccess = false
|
let switchSuccess = false
|
||||||
await ollama.list()
|
await ollama.list()
|
||||||
.then(response => {
|
.then(response => {
|
||||||
@@ -47,28 +47,20 @@ export const SwitchModel: SlashCommand = {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
// todo: problem can be here if async messes up
|
// todo: problem can be here if async messes up
|
||||||
if (switchSuccess) return
|
if (switchSuccess) {
|
||||||
|
// set model now that it exists
|
||||||
|
openConfig(`${interaction.user.username}-config.json`, interaction.commandName, modelInput)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// Phase 2: Try to get it regardless
|
// Phase 2: Notify user of failure to find model.
|
||||||
interaction.editReply({
|
interaction.editReply({
|
||||||
content: `Could not find **${modelInput}** in local model library, trying to pull it now...\n\nThis could take a few moments... Please be patient!`
|
content: `Could not find **${modelInput}** in local model library.\n\nPlease contact an server admin for access to this model.`
|
||||||
})
|
})
|
||||||
|
|
||||||
await ollama.pull({
|
|
||||||
model: modelInput
|
|
||||||
})
|
|
||||||
|
|
||||||
// set model now that it exists
|
|
||||||
openConfig(`${interaction.user.username}-config.json`, interaction.commandName, modelInput)
|
|
||||||
|
|
||||||
// We got the model!
|
|
||||||
interaction.editReply({
|
|
||||||
content: `Successfully added and set **${modelInput}** as your preferred model.`
|
|
||||||
})
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// could not resolve user model switch
|
// could not resolve user model switch
|
||||||
interaction.editReply({
|
interaction.editReply({
|
||||||
content: `Unable to switch user preferred model to **${modelInput}**.\n\n${error}\n\nPossible solution is to run \`/pull-model ${modelInput}\` and try again.`
|
content: `Unable to switch user preferred model to **${modelInput}**.\n\n${error}\n\nPossible solution is to request an server admin run \`/pull-model ${modelInput}\` and try again.`
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ export const ThreadCreate: SlashCommand = {
|
|||||||
})
|
})
|
||||||
|
|
||||||
// Send a message in the thread
|
// Send a message in the thread
|
||||||
thread.send(`Hello ${interaction.user} and others! \n\nIt's nice to meet you. Please talk to me by typing **@${client.user?.username}** with your prompt.`)
|
thread.send(`Hello ${interaction.user} and others! \n\nIt's nice to meet you. Please talk to me by typing **@${client.user?.username}** with your message.`)
|
||||||
|
|
||||||
// handle storing this chat channel
|
// handle storing this chat channel
|
||||||
openChannelInfo(thread.id,
|
openChannelInfo(thread.id,
|
||||||
@@ -27,7 +27,7 @@ export const ThreadCreate: SlashCommand = {
|
|||||||
|
|
||||||
// user only reply
|
// user only reply
|
||||||
return interaction.reply({
|
return interaction.reply({
|
||||||
content: `I can help you in thread **${thread.id}** below.`,
|
content: `I can help you in <#${thread.id}> below.`,
|
||||||
ephemeral: true
|
ephemeral: true
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ export const PrivateThreadCreate: SlashCommand = {
|
|||||||
|
|
||||||
// user only reply
|
// user only reply
|
||||||
return interaction.reply({
|
return interaction.reply({
|
||||||
content: `I can help you in thread **${thread.id}**. Please refer to the private channel below this one.`,
|
content: `I can help you in <#${thread.id}>.`,
|
||||||
ephemeral: true
|
ephemeral: true
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user