mirror of
https://github.com/kevinthedang/discord-ollama.git
synced 2025-12-12 19:56:06 -05:00
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
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { Message } from 'discord.js'
|
||||
import { ChatResponse, Ollama } from 'ollama'
|
||||
import { UserMessage } from './events.js'
|
||||
import { ChatParams, UserMessage, streamResponse, blockResponse } from './index.js'
|
||||
import { Queue } from '../queues/queue.js'
|
||||
|
||||
/**
|
||||
@@ -16,38 +16,49 @@ export async function normalMessage(
|
||||
channel: string,
|
||||
model: string
|
||||
},
|
||||
msgHist: Queue<UserMessage>
|
||||
) {
|
||||
msgHist: Queue<UserMessage>,
|
||||
stream: boolean
|
||||
): Promise<string> {
|
||||
// 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 => {
|
||||
try {
|
||||
// Attempt to query model for message
|
||||
response = await ollama.chat({
|
||||
const params: ChatParams = {
|
||||
model: tokens.model,
|
||||
messages: msgHist.getItems(),
|
||||
options: {
|
||||
num_thread: 8, // remove if optimization needed further
|
||||
mirostat: 1,
|
||||
mirostat_tau: 2.0,
|
||||
top_k: 70
|
||||
},
|
||||
stream: false
|
||||
})
|
||||
ollama: ollama,
|
||||
msgHist: msgHist.getItems()
|
||||
}
|
||||
|
||||
// check if message length > discord max for normal messages
|
||||
if (response.message.content.length > 2000) {
|
||||
sentMessage.edit(response.message.content.slice(0, 2000))
|
||||
message.channel.send(response.message.content.slice(2000))
|
||||
} else // edit the 'generic' response to new message
|
||||
sentMessage.edit(response.message.content)
|
||||
// run query based on stream preference, true = stream, false = block
|
||||
if (stream) {
|
||||
response = await streamResponse(params)
|
||||
for await (const portion of response) {
|
||||
// append token to message
|
||||
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) {
|
||||
console.log(`[Util: messageNormal] Error creating message: ${error.message}`)
|
||||
sentMessage.edit(`**Response generation failed.**\n\nReason: ${error.message}`)
|
||||
}
|
||||
})
|
||||
|
||||
// Hope there is a response, force client to believe
|
||||
return response!!
|
||||
}
|
||||
// return the string representation of response
|
||||
return result
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user