2 Commits

Author SHA1 Message Date
Kevin Dang
b5194fa645 Discord Administrator Role Permissions (#54)
* add: admin check for disable

* update: shutoff uses memberPerms now

* rm: superUser env variable

* update: version increment

* rm: admin env in docker and workflow
2024-04-25 11:26:22 -07:00
Kevin Dang
d67106c03e Chat Stream Integration (#52)
* rm: axios dependency

* add: stream parsing for normal style

* fix: empty string problem

* add: stream for embedded prompts

* update: version increment
2024-04-21 14:40:30 -07:00
15 changed files with 158 additions and 192 deletions

View File

@@ -22,6 +22,3 @@ DISCORD_IP = IP_ADDRESS
# subnet address, ex. 172.18.0.0 as we use /16. # subnet address, ex. 172.18.0.0 as we use /16.
SUBNET_ADDRESS = ADDRESS SUBNET_ADDRESS = ADDRESS
# list of admins to handle admin commands for the bot, use single quotes
ADMINS=['username1', 'username2', 'username3', ...]

View File

@@ -37,7 +37,6 @@ jobs:
echo CLIENT_UID = ${{ secrets.CLIENT_UID }} >> .env echo CLIENT_UID = ${{ secrets.CLIENT_UID }} >> .env
echo OLLAMA_IP = ${{ secrets.OLLAMA_IP }} >> .env echo OLLAMA_IP = ${{ secrets.OLLAMA_IP }} >> .env
echo OLLAMA_PORT = ${{ secrets.OLLAMA_PORT }} >> .env echo OLLAMA_PORT = ${{ secrets.OLLAMA_PORT }} >> .env
echo ADMINS = ${{ secrets.ADMINS }} >> .env
# set -e ensures if nohup fails, this section fails # set -e ensures if nohup fails, this section fails
- name: Startup Discord Bot Client - name: Startup Discord Bot Client
@@ -68,7 +67,6 @@ jobs:
echo CLIENT_UID = ${{ secrets.CLIENT_UID }} >> .env echo CLIENT_UID = ${{ secrets.CLIENT_UID }} >> .env
echo OLLAMA_IP = ${{ secrets.OLLAMA_IP }} >> .env echo OLLAMA_IP = ${{ secrets.OLLAMA_IP }} >> .env
echo OLLAMA_PORT = ${{ secrets.OLLAMA_PORT }} >> .env echo OLLAMA_PORT = ${{ secrets.OLLAMA_PORT }} >> .env
echo ADMINS = ${{ secrets.ADMINS }} >> .env
- name: Setup Docker Network and Images - name: Setup Docker Network and Images
run: | run: |

View File

@@ -8,7 +8,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: discord/bot:0.4.0 image: discord/bot:0.4.3
environment: environment:
CLIENT_TOKEN: ${CLIENT_TOKEN} CLIENT_TOKEN: ${CLIENT_TOKEN}
GUILD_ID: ${GUILD_ID} GUILD_ID: ${GUILD_ID}
@@ -17,7 +17,6 @@ services:
CLIENT_UID: ${CLIENT_UID} CLIENT_UID: ${CLIENT_UID}
OLLAMA_IP: ${OLLAMA_IP} OLLAMA_IP: ${OLLAMA_IP}
OLLAMA_PORT: ${OLLAMA_PORT} OLLAMA_PORT: ${OLLAMA_PORT}
ADMINS: ${ADMINS}
networks: networks:
ollama-net: ollama-net:
ipv4_address: ${DISCORD_IP} ipv4_address: ${DISCORD_IP}

95
package-lock.json generated
View File

@@ -1,15 +1,14 @@
{ {
"name": "discord-ollama", "name": "discord-ollama",
"version": "0.4.0", "version": "0.4.3",
"lockfileVersion": 3, "lockfileVersion": 3,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "discord-ollama", "name": "discord-ollama",
"version": "0.4.0", "version": "0.4.3",
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"axios": "^1.6.2",
"discord.js": "^14.14.1", "discord.js": "^14.14.1",
"dotenv": "^16.3.1", "dotenv": "^16.3.1",
"ollama": "^0.5.0" "ollama": "^0.5.0"
@@ -661,21 +660,6 @@
"integrity": "sha512-58S9QDqG0Xx27YwPSt9fJxivjYl432YCwfDMfZ+71RAqUrZef7LrKQZ3LHLOwCS4FLNBplP533Zx895SeOCHvA==", "integrity": "sha512-58S9QDqG0Xx27YwPSt9fJxivjYl432YCwfDMfZ+71RAqUrZef7LrKQZ3LHLOwCS4FLNBplP533Zx895SeOCHvA==",
"dev": true "dev": true
}, },
"node_modules/asynckit": {
"version": "0.4.0",
"resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz",
"integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q=="
},
"node_modules/axios": {
"version": "1.6.8",
"resolved": "https://registry.npmjs.org/axios/-/axios-1.6.8.tgz",
"integrity": "sha512-v/ZHtJDU39mDpyBoFVkETcd/uNdxrWRrg3bKpOKzXFA6Bvqopts6ALSMU3y6ijYxbw2B+wPrIv46egTzJXCLGQ==",
"dependencies": {
"follow-redirects": "^1.15.6",
"form-data": "^4.0.0",
"proxy-from-env": "^1.1.0"
}
},
"node_modules/balanced-match": { "node_modules/balanced-match": {
"version": "1.0.2", "version": "1.0.2",
"resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz", "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz",
@@ -740,17 +724,6 @@
"fsevents": "~2.3.2" "fsevents": "~2.3.2"
} }
}, },
"node_modules/combined-stream": {
"version": "1.0.8",
"resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz",
"integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==",
"dependencies": {
"delayed-stream": "~1.0.0"
},
"engines": {
"node": ">= 0.8"
}
},
"node_modules/concat-map": { "node_modules/concat-map": {
"version": "0.0.1", "version": "0.0.1",
"resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz",
@@ -780,14 +753,6 @@
} }
} }
}, },
"node_modules/delayed-stream": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz",
"integrity": "sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==",
"engines": {
"node": ">=0.4.0"
}
},
"node_modules/diff": { "node_modules/diff": {
"version": "4.0.2", "version": "4.0.2",
"resolved": "https://registry.npmjs.org/diff/-/diff-4.0.2.tgz", "resolved": "https://registry.npmjs.org/diff/-/diff-4.0.2.tgz",
@@ -892,38 +857,6 @@
"node": ">=8" "node": ">=8"
} }
}, },
"node_modules/follow-redirects": {
"version": "1.15.6",
"resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.6.tgz",
"integrity": "sha512-wWN62YITEaOpSK584EZXJafH1AGpO8RVgElfkuXbTOrPX4fIfOyEpW/CsiNd8JdYrAoOvafRTOEnvsO++qCqFA==",
"funding": [
{
"type": "individual",
"url": "https://github.com/sponsors/RubenVerborgh"
}
],
"engines": {
"node": ">=4.0"
},
"peerDependenciesMeta": {
"debug": {
"optional": true
}
}
},
"node_modules/form-data": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz",
"integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==",
"dependencies": {
"asynckit": "^0.4.0",
"combined-stream": "^1.0.8",
"mime-types": "^2.1.12"
},
"engines": {
"node": ">= 6"
}
},
"node_modules/fsevents": { "node_modules/fsevents": {
"version": "2.3.3", "version": "2.3.3",
"resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.3.tgz", "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.3.tgz",
@@ -1052,25 +985,6 @@
"integrity": "sha512-s8UhlNe7vPKomQhC1qFelMokr/Sc3AgNbso3n74mVPA5LTZwkB9NlXf4XPamLxJE8h0gh73rM94xvwRT2CVInw==", "integrity": "sha512-s8UhlNe7vPKomQhC1qFelMokr/Sc3AgNbso3n74mVPA5LTZwkB9NlXf4XPamLxJE8h0gh73rM94xvwRT2CVInw==",
"dev": true "dev": true
}, },
"node_modules/mime-db": {
"version": "1.52.0",
"resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz",
"integrity": "sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==",
"engines": {
"node": ">= 0.6"
}
},
"node_modules/mime-types": {
"version": "2.1.35",
"resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.35.tgz",
"integrity": "sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==",
"dependencies": {
"mime-db": "1.52.0"
},
"engines": {
"node": ">= 0.6"
}
},
"node_modules/minimatch": { "node_modules/minimatch": {
"version": "3.1.2", "version": "3.1.2",
"resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.1.2.tgz", "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.1.2.tgz",
@@ -1161,11 +1075,6 @@
"url": "https://github.com/sponsors/jonschlinkert" "url": "https://github.com/sponsors/jonschlinkert"
} }
}, },
"node_modules/proxy-from-env": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz",
"integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg=="
},
"node_modules/pstree.remy": { "node_modules/pstree.remy": {
"version": "1.1.8", "version": "1.1.8",
"resolved": "https://registry.npmjs.org/pstree.remy/-/pstree.remy-1.1.8.tgz", "resolved": "https://registry.npmjs.org/pstree.remy/-/pstree.remy-1.1.8.tgz",

View File

@@ -1,6 +1,6 @@
{ {
"name": "discord-ollama", "name": "discord-ollama",
"version": "0.4.0", "version": "0.4.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",
@@ -24,7 +24,6 @@
"author": "Kevin Dang", "author": "Kevin Dang",
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"axios": "^1.6.2",
"discord.js": "^14.14.1", "discord.js": "^14.14.1",
"dotenv": "^16.3.1", "dotenv": "^16.3.1",
"ollama": "^0.5.0" "ollama": "^0.5.0"

View File

@@ -4,7 +4,7 @@ import { openFile } from '../utils/jsonHandler.js'
export const Disable: SlashCommand = { export const Disable: SlashCommand = {
name: 'toggle-chat', name: 'toggle-chat',
description: 'toggle all chat features, slash commands will still work.', 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: [
@@ -22,6 +22,15 @@ export const Disable: SlashCommand = {
const channel = await client.channels.fetch(interaction.channelId) const channel = await client.channels.fetch(interaction.channelId)
if (!channel || channel.type !== ChannelType.GuildText) return if (!channel || channel.type !== ChannelType.GuildText) return
// check if runner is an admin
if (!interaction.memberPermissions?.has('Administrator')) {
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.`,
ephemeral: true
})
return
}
// set state of bot chat features // set state of bot chat features
openFile('config.json', interaction.commandName, interaction.options.get('enabled')?.value) openFile('config.json', interaction.commandName, interaction.options.get('enabled')?.value)

View File

@@ -1,10 +1,9 @@
import { ChannelType, Client, CommandInteraction, ApplicationCommandOptionType } from 'discord.js' import { ChannelType, Client, CommandInteraction, ApplicationCommandOptionType } from 'discord.js'
import { SlashCommand } from '../utils/commands.js' import { SlashCommand } from '../utils/commands.js'
import Keys from '../keys.js'
export const Shutoff: SlashCommand = { export const Shutoff: SlashCommand = {
name: 'shutoff', name: 'shutoff',
description: 'shutdown the bot. You will need to manually bring it online again.', description: 'shutdown the bot. You will need to manually bring it online again. Administrator Only.',
// set available user options to pass to the command // set available user options to pass to the command
options: [ options: [
@@ -25,24 +24,22 @@ export const Shutoff: SlashCommand = {
// 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(`User -> ${interaction.user.tag} attempting to shutdown ${client.user!!.tag}`)
// create list of superUsers based on string parse
const superUsers: string[] = JSON.parse(Keys.superUser.replace(/'/g, '"'))
// check if admin or false on shutdown // check if admin or false on shutdown
if (interaction.user.tag !in superUsers) { if (!interaction.memberPermissions?.has('Administrator')) {
interaction.reply({ interaction.reply({
content: `Shutdown failed:\n\n${interaction.user.tag}, You do not have permission to shutoff **${client.user?.tag}**.`, content: `**Shutdown Aborted:**\n\n${interaction.user.tag}, You do not have permission to shutoff **${client.user?.tag}**.`,
ephemeral: true ephemeral: true
}) })
return // stop from shutting down return // stop from shutting down
} else if (!interaction.options.get('are-you-sure')?.value) { } else if (!interaction.options.get('are-you-sure')?.value) {
interaction.reply({ interaction.reply({
content: `Shutdown failed:\n\n${interaction.user.tag}, You didn't want to shutoff **${client.user?.tag}**.`, content: `**Shutdown Aborted:**\n\n${interaction.user.tag}, You didn't want to shutoff **${client.user?.tag}**.`,
ephemeral: true ephemeral: true
}) })
return return // chickened out
} }
// 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 ${interaction.options.get('are-you-sure')?.value ? "shutting down now." : "not shutting down." }`,
ephemeral: true ephemeral: true

View File

@@ -19,6 +19,8 @@ export default event(Events.MessageCreate, async ({ log, msgHist, tokens, ollama
// Only respond if message mentions the bot // Only respond if message mentions the bot
if (!message.mentions.has(tokens.clientUid)) return if (!message.mentions.has(tokens.clientUid)) return
let shouldStream = false
// Try to query and send embed // Try to query and send embed
try { try {
const config: Configuration = await new Promise((resolve, reject) => { const config: Configuration = await new Promise((resolve, reject) => {
@@ -45,11 +47,14 @@ export default event(Events.MessageCreate, async ({ log, msgHist, tokens, ollama
msgHist.capacity = config.options['modify-capacity'] msgHist.capacity = config.options['modify-capacity']
} }
// set stream state
shouldStream = config.options['message-stream'] as boolean || false
resolve(config) resolve(config)
}) })
}) })
let response: ChatResponse let response: string
// check if we can push, if not, remove oldest // check if we can push, if not, remove oldest
while (msgHist.size() >= msgHist.capacity) msgHist.dequeue() while (msgHist.size() >= msgHist.capacity) msgHist.dequeue()
@@ -62,9 +67,9 @@ export default event(Events.MessageCreate, async ({ log, msgHist, tokens, ollama
// undefined or false, use normal, otherwise use embed // undefined or false, use normal, otherwise use embed
if (config.options['message-style']) if (config.options['message-style'])
response = await embedMessage(message, ollama, tokens, msgHist) response = await embedMessage(message, ollama, tokens, msgHist, shouldStream)
else else
response = await normalMessage(message, ollama, tokens, msgHist) response = await normalMessage(message, ollama, tokens, 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 }
@@ -75,7 +80,7 @@ export default event(Events.MessageCreate, async ({ log, msgHist, tokens, ollama
// successful query, save it in context history // successful query, save it in context history
msgHist.enqueue({ msgHist.enqueue({
role: 'assistant', role: 'assistant',
content: response.message.content content: response
}) })
} catch (error: any) { } catch (error: any) {
msgHist.pop() // remove message because of failure msgHist.pop() // remove message because of failure

View File

@@ -8,7 +8,6 @@ export const Keys = {
guildId: getEnvVar('GUILD_ID'), guildId: getEnvVar('GUILD_ID'),
ipAddress: getEnvVar('OLLAMA_IP'), ipAddress: getEnvVar('OLLAMA_IP'),
portAddress: getEnvVar('OLLAMA_PORT'), portAddress: getEnvVar('OLLAMA_PORT'),
superUser: getEnvVar('ADMINS')
} as const // readonly keys } as const // readonly keys
export default Keys export default Keys

View File

@@ -20,6 +20,18 @@ export type Tokens = {
clientUid: string clientUid: string
} }
/**
* Parameters to run the chat query
* @param model the model to run
* @param ollama ollama api client
* @param msgHist message history
*/
export type ChatParams = {
model: string,
ollama: Ollama,
msgHist: UserMessage[]
}
/** /**
* Format for the messages to be stored when communicating when the bot * Format for the messages to be stored when communicating when the bot
* @param role either assistant, user, or system * @param role either assistant, user, or system

View File

@@ -3,4 +3,5 @@ export * from './env.js'
export * from './events.js' export * from './events.js'
export * from './messageEmbed.js' export * from './messageEmbed.js'
export * from './messageNormal.js' export * from './messageNormal.js'
export * from './commands.js' export * from './commands.js'
export * from './streamHandler.js'

View File

@@ -1,6 +1,6 @@
import { EmbedBuilder, Message } from 'discord.js' import { EmbedBuilder, Message } from 'discord.js'
import { ChatResponse, Ollama } from 'ollama' import { ChatResponse, Ollama } from 'ollama'
import { UserMessage } from './events.js' import { ChatParams, UserMessage, streamResponse, blockResponse } from './index.js'
import { Queue } from '../queues/queue.js' import { Queue } from '../queues/queue.js'
/** /**
@@ -16,10 +16,12 @@ export async function embedMessage(
channel: string, channel: string,
model: string model: string
}, },
msgHist: Queue<UserMessage> msgHist: Queue<UserMessage>,
) { stream: boolean
): Promise<string> {
// bot response // bot response
let response: ChatResponse let response: ChatResponse | AsyncGenerator<ChatResponse, any, unknown>
let result: string = ''
// initial message to client // initial message to client
const botMessage = new EmbedBuilder() const botMessage = new EmbedBuilder()
@@ -30,28 +32,43 @@ export async function embedMessage(
// send the message // send the message
const sentMessage = await message.channel.send({ embeds: [botMessage] }) const sentMessage = await message.channel.send({ embeds: [botMessage] })
try { // create params
// Attempt to query model for message const params: ChatParams = {
response = await ollama.chat({ model: tokens.model,
model: tokens.model, ollama: ollama,
messages: msgHist.getItems(), msgHist: msgHist.getItems()
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 try {
const newEmbed = new EmbedBuilder() // check if embed needs to stream
if (stream) {
response = await streamResponse(params)
for await (const portion of response) {
result += portion.message.content
// new embed per token...
const newEmbed = new EmbedBuilder()
.setTitle(`Responding to ${message.author.tag}`)
.setDescription(result || 'No Content Yet...')
.setColor('#00FF00')
// edit the message
sentMessage.edit({ embeds: [newEmbed] })
}
} else {
response = await blockResponse(params)
result = response.message.content
// only need to create 1 embed again
const newEmbed = new EmbedBuilder()
.setTitle(`Responding to ${message.author.tag}`) .setTitle(`Responding to ${message.author.tag}`)
.setDescription(response.message.content || 'No Content to Provide...') .setDescription(result || 'No Content to Provide...')
.setColor('#00FF00') .setColor('#00FF00')
// edit the message // edit the message
sentMessage.edit({ embeds: [newEmbed] }) sentMessage.edit({ embeds: [newEmbed] })
}
} catch(error: any) { } catch(error: any) {
console.log(`[Util: messageEmbed] Error creating message: ${error.message}`) console.log(`[Util: messageEmbed] Error creating message: ${error.message}`)
const errorEmbed = new EmbedBuilder() const errorEmbed = new EmbedBuilder()
@@ -64,5 +81,5 @@ export async function embedMessage(
} }
// Hope there is a response! undefined otherwie // Hope there is a response! undefined otherwie
return response!! return result
} }

View File

@@ -1,6 +1,6 @@
import { Message } from 'discord.js' import { Message } from 'discord.js'
import { ChatResponse, Ollama } from 'ollama' import { ChatResponse, Ollama } from 'ollama'
import { UserMessage } from './events.js' import { ChatParams, UserMessage, streamResponse, blockResponse } from './index.js'
import { Queue } from '../queues/queue.js' import { Queue } from '../queues/queue.js'
/** /**
@@ -16,38 +16,49 @@ export async function normalMessage(
channel: string, channel: string,
model: string model: string
}, },
msgHist: Queue<UserMessage> msgHist: Queue<UserMessage>,
) { stream: boolean
): Promise<string> {
// bot's respnse // bot's respnse
let response: ChatResponse let response: ChatResponse | AsyncGenerator<ChatResponse, any, unknown>
let result: string = ''
await message.channel.send('Generating Response . . .').then(async sentMessage => { await message.channel.send('Generating Response . . .').then(async sentMessage => {
try { try {
// Attempt to query model for message const params: ChatParams = {
response = await ollama.chat({
model: tokens.model, model: tokens.model,
messages: msgHist.getItems(), ollama: ollama,
options: { msgHist: msgHist.getItems()
num_thread: 8, // remove if optimization needed further }
mirostat: 1,
mirostat_tau: 2.0,
top_k: 70
},
stream: false
})
// check if message length > discord max for normal messages // run query based on stream preference, true = stream, false = block
if (response.message.content.length > 2000) { if (stream) {
sentMessage.edit(response.message.content.slice(0, 2000)) response = await streamResponse(params)
message.channel.send(response.message.content.slice(2000)) for await (const portion of response) {
} else // edit the 'generic' response to new message // append token to message
sentMessage.edit(response.message.content) result += portion.message.content
// resent current output, THIS WILL BE SLOW due to discord limits!
sentMessage.edit(result || 'No Content Yet...')
}
}
else {
response = await blockResponse(params)
result = response.message.content
// check if message length > discord max for normal messages
if (result.length > 2000) {
sentMessage.edit(result.slice(0, 2000))
message.channel.send(result.slice(2000))
} else // edit the 'generic' response to new message
sentMessage.edit(result)
}
} catch(error: any) { } catch(error: any) {
console.log(`[Util: messageNormal] Error creating message: ${error.message}`) console.log(`[Util: messageNormal] Error creating message: ${error.message}`)
sentMessage.edit(`**Response generation failed.**\n\nReason: ${error.message}`) sentMessage.edit(`**Response generation failed.**\n\nReason: ${error.message}`)
} }
}) })
// Hope there is a response, force client to believe // return the string representation of response
return response!! return result
} }

View File

@@ -0,0 +1,40 @@
import { ChatResponse } from "ollama"
import { ChatParams } from "./index.js"
/**
* Method to query the Ollama client for async generation
* @param params
* @returns Asyn
*/
export async function streamResponse(params: ChatParams): Promise<AsyncGenerator<ChatResponse, any, unknown>> {
return await params.ollama.chat({
model: params.model,
messages: params.msgHist,
options: {
num_thread: 8, // remove if optimization needed further
mirostat: 1,
mirostat_tau: 2.0,
top_k: 70
},
stream: true
})
}
/**
* Method to query the Ollama client for a block response
* @param params parameters to query the client
* @returns ChatResponse generated by the Ollama client
*/
export async function blockResponse(params: ChatParams): Promise<ChatResponse> {
return await params.ollama.chat({
model: params.model,
messages: params.msgHist,
options: {
num_thread: 8, // remove if optimization needed further
mirostat: 1,
mirostat_tau: 2.0,
top_k: 70
},
stream: false
})
}

View File

@@ -1,27 +0,0 @@
import { AxiosResponse } from 'axios'
/**
* When running a /api/chat stream, the output needs to be parsed into an array of objects
* This method is used for development purposes and testing
*
* This will not work as intended with the inclusion of ollama-js, needs to be modified to work with it
*
* @param stream Axios response to from Ollama
*/
export function parseStream(stream: AxiosResponse<any, any>) {
// split string by newline
const keywordObjects: string[] = stream.data.trim().split('\n')
// parse string and load them into objects
const keywordsArray: {
model: string,
created_at: string,
message: {
role: string,
content: string
},
done: boolean
}[] = keywordObjects.map((keywordString) => JSON.parse(keywordString))
return keywordsArray
}