Fix crash when typing in a non-allowed channel

This commit is contained in:
James Shiffer 2024-07-29 05:06:07 +00:00
parent c55e613a4a
commit 4124be492d

View File

@ -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;