From 32dd7054981ea69f7da694905ea613f6a653bb4a Mon Sep 17 00:00:00 2001 From: Wroclaw Date: Sun, 20 Aug 2023 15:12:26 +0200 Subject: [PATCH 1/2] Use Iterable when converting to OpenAI messages --- src/execution.ts | 4 +++- src/toOpenAIMessages.ts | 13 +++++++------ 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/execution.ts b/src/execution.ts index 57a5463..9573796 100644 --- a/src/execution.ts +++ b/src/execution.ts @@ -274,7 +274,9 @@ async function executeFromQueue(channel: string) { await message.deferReply(); } - OpenAImessages = toOpenAIMessages(messages); + messages.sort((a, b) => b.createdTimestamp - a.createdTimestamp); + + OpenAImessages = toOpenAIMessages(messages.values()); let generatedMessage: ChatCompletionResponseMessage | undefined = undefined; let answer: Awaited>; diff --git a/src/toOpenAIMessages.ts b/src/toOpenAIMessages.ts index 598238d..e868b45 100644 --- a/src/toOpenAIMessages.ts +++ b/src/toOpenAIMessages.ts @@ -92,17 +92,18 @@ function getAuthorUsername(message: DiscordMessage): string | undefined { } /** - * Converts the Collection of Discord Messages to array of OpenAI Messages to send - * @param messages the collection to convert + * Converts the Iterable of Discord Messages to array of OpenAI Messages to send + * first message in the interable will be the last of the returned array (reverse order) + * @param messages the iterable to convert * @returns the converted messages */ -export default function toOpenAIMessages(messages: Collection): OpenAIMessage[] { +export default function toOpenAIMessages( + messages: Iterable, +): OpenAIMessage[] { const rvalue: OpenAIMessage[] = []; let tokenCount = 0; - messages.sort((a, b) => b.createdTimestamp - a.createdTimestamp); - - for (const message of messages.values()) { + for (const message of messages) { const content = formatMessage(message); // FIXME: tokens are not being counted properly (it's lower than it is) but it's enough for me for now. tokenCount += countTokens(content); From c9f7e3a62e9b7e56aa07079fffec0f1b8d273556 Mon Sep 17 00:00:00 2001 From: Wroclaw Date: Sun, 20 Aug 2023 15:41:39 +0200 Subject: [PATCH 2/2] Use the request message as always newest in the model history This may fix #5 but I am not sure --- src/execution.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/execution.ts b/src/execution.ts index 9573796..fec4160 100644 --- a/src/execution.ts +++ b/src/execution.ts @@ -267,6 +267,7 @@ async function executeFromQueue(channel: string) { messages.forEach(m => { Moderation.checkMessageNoReturn(m); }); + const questionMessageId: string = message instanceof DiscordApi.Message ? message.id : ''; if (message instanceof DiscordApi.Message) { channelQueue.startTyping(); } @@ -274,7 +275,11 @@ async function executeFromQueue(channel: string) { await message.deferReply(); } - messages.sort((a, b) => b.createdTimestamp - a.createdTimestamp); + messages.sort((a, b) => { + if (a.id === questionMessageId) return -1; + if (b.id === questionMessageId) return 1; + return b.createdTimestamp - a.createdTimestamp; + }); OpenAImessages = toOpenAIMessages(messages.values()); let generatedMessage: ChatCompletionResponseMessage | undefined = undefined;