mirror of
https://github.com/kevinthedang/discord-ollama.git
synced 2026-06-20 13:57:24 -04:00
Summary Command (#194)
This commit is contained in:
@@ -18,7 +18,7 @@ The project aims to:
|
||||
* [x] Message Persistance
|
||||
* [x] Containerization with Docker
|
||||
* [x] Slash Commands Compatible
|
||||
* [ ] Summary Command
|
||||
* [x] Summary Command
|
||||
* [ ] Model Info Command
|
||||
* [ ] List Models Command
|
||||
* [x] Pull Model Command
|
||||
|
||||
@@ -7,7 +7,7 @@ services:
|
||||
build: ./ # find docker file in designated path
|
||||
container_name: discord
|
||||
restart: always # rebuild container always
|
||||
image: kevinthedang/discord-ollama:0.8.7
|
||||
image: kevinthedang/discord-ollama:0.9.0
|
||||
environment:
|
||||
CLIENT_TOKEN: ${CLIENT_TOKEN}
|
||||
OLLAMA_IP: ${OLLAMA_IP}
|
||||
|
||||
4
package-lock.json
generated
4
package-lock.json
generated
@@ -1,12 +1,12 @@
|
||||
{
|
||||
"name": "discord-ollama",
|
||||
"version": "0.8.7",
|
||||
"version": "0.9.0",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "discord-ollama",
|
||||
"version": "0.8.7",
|
||||
"version": "0.9.0",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"discord.js": "^14.20.0",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "discord-ollama",
|
||||
"version": "0.8.7",
|
||||
"version": "0.9.0",
|
||||
"description": "Ollama Integration into discord",
|
||||
"main": "build/index.js",
|
||||
"exports": "./build/index.js",
|
||||
|
||||
@@ -9,6 +9,7 @@ import { ClearUserChannelHistory } from './cleanUserChannelHistory.js'
|
||||
import { PullModel } from './pullModel.js'
|
||||
import { SwitchModel } from './switchModel.js'
|
||||
import { DeleteModel } from './deleteModel.js'
|
||||
import { Summary } from './summary.js'
|
||||
|
||||
export default [
|
||||
ThreadCreate,
|
||||
@@ -20,5 +21,6 @@ export default [
|
||||
ClearUserChannelHistory,
|
||||
PullModel,
|
||||
SwitchModel,
|
||||
DeleteModel
|
||||
DeleteModel,
|
||||
Summary
|
||||
] as SlashCommand[]
|
||||
50
src/commands/summary.ts
Normal file
50
src/commands/summary.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
import { Client, CommandInteraction, Message, MessageFlags, SendableChannels } from "discord.js";
|
||||
import { accessChannelContext, normalMessage, SlashCommand, summarizeContextHistory, UserCommand, UserMessage } from "../utils/index.js";
|
||||
import { ollama } from "../client.js"
|
||||
|
||||
export const Summary: SlashCommand = {
|
||||
name: 'summary',
|
||||
description: 'provides a summary of the chat history.',
|
||||
|
||||
// Generate Summary from additional context
|
||||
run: async (client: Client, interaction: CommandInteraction) => {
|
||||
// fetch channel
|
||||
const channel = await client.channels.fetch(interaction.channelId)
|
||||
if (!channel || !UserCommand.includes(channel.type)) return
|
||||
|
||||
// Defer
|
||||
await interaction.deferReply({ flags: MessageFlags.Ephemeral })
|
||||
|
||||
// create summary using context
|
||||
let channelContext: UserMessage[] = await new Promise((resolve) => {
|
||||
accessChannelContext(interaction.channelId, (additionalContext) => {
|
||||
if (additionalContext?.messages)
|
||||
resolve(additionalContext.messages)
|
||||
else
|
||||
resolve([])
|
||||
})
|
||||
})
|
||||
|
||||
if (channelContext.length === 0) {
|
||||
interaction.reply({
|
||||
content: `There are no recent chat messages in this channel.`,
|
||||
flags: MessageFlags.Ephemeral
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// Push summarize prompt
|
||||
channelContext.push({
|
||||
role: "user",
|
||||
content: "Please Summarize everything from the prior messages, provide in bullet point fashion on what people have said prior. For example: \"Someone mentioned/talked about ...\"",
|
||||
images: []
|
||||
})
|
||||
|
||||
// todo: instead of a default of codellama, we can user default model somehow. Look into later.
|
||||
const response: string = await summarizeContextHistory(ollama, "codellama", channelContext)
|
||||
|
||||
console.log(response)
|
||||
|
||||
interaction.editReply({ content: response })
|
||||
}
|
||||
}
|
||||
@@ -89,6 +89,21 @@ export async function addToChannelContext(filename: string, channel : TextChanne
|
||||
}
|
||||
}
|
||||
|
||||
export async function accessChannelContext(filename: string, callback: (config: Channel | undefined) => void): Promise<void> {
|
||||
const fullFileName = `data/${filename}-context.json`
|
||||
if (fs.existsSync(fullFileName)) {
|
||||
fs.readFile(fullFileName, 'utf8', (error, data) => {
|
||||
if (error) {
|
||||
callback(undefined)
|
||||
return // something went wrong... stop
|
||||
}
|
||||
callback(JSON.parse(data))
|
||||
})
|
||||
} else {
|
||||
callback(undefined) // file not found
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to open the channel history
|
||||
*
|
||||
|
||||
@@ -90,6 +90,38 @@ export async function normalMessage(
|
||||
return result
|
||||
}
|
||||
|
||||
export async function summarizeContextHistory(
|
||||
ollama: Ollama,
|
||||
model: string,
|
||||
msgHist: UserMessage[],
|
||||
): Promise<string> {
|
||||
// todo: this stuff to create a response in a simpler way!
|
||||
try {
|
||||
const params: ChatParams = {
|
||||
model: model,
|
||||
ollama: ollama,
|
||||
msgHist: msgHist
|
||||
}
|
||||
const response: ChatResponse = await blockResponse(params)
|
||||
let result = response.message.content
|
||||
|
||||
if (hasThinking(result))
|
||||
result = result.replace(/<think>[\s\S]*?<\/think>/g, '').trim()
|
||||
|
||||
return result
|
||||
} catch (error: any) {
|
||||
console.log(`[Util: messageNormal] Error creating message: ${error.message}`)
|
||||
if (error.message.includes('fetch failed'))
|
||||
error.message = 'Missing ollama service on machine'
|
||||
else if (error.message.includes('try pulling it first'))
|
||||
error.message = `You do not have the ${model} downloaded. Ask an admin to pull it using the \`pull-model\` command.`
|
||||
return `**Response generation failed.**\n\nReason: ${error.message}`
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Region: Helpers
|
||||
function hasThinking(message: string): boolean {
|
||||
return /<think>[\s\S]*?<\/think>/i.test(message)
|
||||
}
|
||||
// End Region: Helpers
|
||||
|
||||
@@ -27,7 +27,7 @@ describe('Commands Existence', () => {
|
||||
// test specific commands in the object
|
||||
it('references specific commands', () => {
|
||||
const commandsString = commands.map(e => e.name).join(', ')
|
||||
const expectedCommands = ['thread', 'private-thread', 'message-stream', 'toggle-chat', 'shutoff', 'modify-capacity', 'clear-user-channel-history', 'pull-model', 'switch-model', 'delete-model']
|
||||
const expectedCommands = ['thread', 'private-thread', 'message-stream', 'toggle-chat', 'shutoff', 'modify-capacity', 'clear-user-channel-history', 'pull-model', 'switch-model', 'delete-model', 'summary']
|
||||
expect(commandsString).toBe(expectedCommands.join(', '))
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user