From 4124be492d1d64471c84d4cffa4551bd6309919f Mon Sep 17 00:00:00 2001 From: James Shiffer Date: Mon, 29 Jul 2024 05:06:07 +0000 Subject: [PATCH] Fix crash when typing in a non-allowed channel --- discord/bot.ts | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/discord/bot.ts b/discord/bot.ts index b0c840d..70f8f40 100644 --- a/discord/bot.ts +++ b/discord/bot.ts @@ -37,6 +37,7 @@ import { } from './util'; import 'dotenv/config'; +const KNOWN_USERNAMES = ['vinso1445', 'bapazheng', 'f0oby', 'shibe.mp4', '1thinker', 'bapabakshi', 'keliande27', 'gnuwu', 'scoliono', 'adam28405']; const config = {}; interface CommandClient extends Client { @@ -150,9 +151,9 @@ async function onNewMessage(message: Message) message ]; - await message.channel.sendTyping(); - try { + await message.channel.sendTyping(); + const response = await requestLLMResponse(cleanHistoryList); // evaluate response if (!isGoodResponse(response)) { @@ -213,16 +214,23 @@ async function requestLLMResponse(messages) queryParams.append(field, config["llmconf"].llmSettings[field]); } const llmEndpoint = `${process.env.LLM_HOST}/?${queryParams.toString()}`; - const messageList = messages.map((m: Message) => ({ - role: m.author.bot ? "assistant" : "user", - content: m.cleanContent, - })); + const messageList = messages.map((m: Message) => { + let role = 'user'; + if (m.author.id === process.env.CLIENT) { + role = 'assistant'; + } else if (m.author.bot) { + return null; + } else if (KNOWN_USERNAMES.includes(m.author.username)) { + role = m.author.username; + } + return { role, content: m.cleanContent }; + }); const reqBody = [ { "role": "system", "content": config["llmconf"].sys_prompt }, - ...messageList + ...messageList.filter(x => x) ]; logInfo("[bot] Requesting LLM response with message list: " + reqBody.map(m => m.content)); const res = await fetch(llmEndpoint, { @@ -236,7 +244,7 @@ async function requestLLMResponse(messages) const txtRaw: string = txt["raw"][0]; // Depends on chat template used const prefix = "<|start_header_id|>assistant<|end_header_id|>\n\n"; - const suffix = "<|reserved_special_token_"; + const suffix = "<|eot_id|>"; const txtStart = txtRaw.lastIndexOf(prefix); const txtEnd = txtRaw.slice(txtStart + prefix.length); const txtStop = txtEnd.indexOf(suffix) !== -1 ? txtEnd.indexOf(suffix) : txtEnd.length;