From 52f16c9d0adbd75638628c83ab61f9338acfe04f Mon Sep 17 00:00:00 2001 From: Wroclaw Date: Thu, 28 Sep 2023 11:01:35 +0200 Subject: [PATCH 1/2] toOpenAIMessages: also return the username to user id map This will be used for fetching user descriptions very soon --- src/execution.ts | 2 +- src/toOpenAIMessages.ts | 20 +++++++++++++------- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/execution.ts b/src/execution.ts index 3b8e2fd..dada0a4 100644 --- a/src/execution.ts +++ b/src/execution.ts @@ -237,7 +237,7 @@ async function executeFromQueue(channel: string) { return b.createdTimestamp - a.createdTimestamp; }); - OpenAImessages = toOpenAIMessages(messages.values()); + [OpenAImessages] = toOpenAIMessages(messages.values()); let generatedMessage: ChatCompletionMessage | undefined = undefined; let answer: Awaited>; diff --git a/src/toOpenAIMessages.ts b/src/toOpenAIMessages.ts index f93d235..ccb8421 100644 --- a/src/toOpenAIMessages.ts +++ b/src/toOpenAIMessages.ts @@ -95,28 +95,34 @@ function getAuthorUsername(message: DiscordMessage): string | undefined { /** * 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) + * (first should be newest) * @param messages the iterable to convert - * @returns the converted messages + * @returns [0] the converted messages + * @returns [1] username mappings to id */ export default function toOpenAIMessages( messages: Iterable, -): OpenAIMessage[] { - const rvalue: OpenAIMessage[] = []; +): [OpenAIMessage[], Map] { + const rmessages: OpenAIMessage[] = []; + const rUserMap = new Map(); let tokenCount = 0; for (const message of messages) { + // FIXME: multiple users could have the same name here. 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); if (tokenCount > config.readLimits.tokens) break; - rvalue.push({ + const name = getAuthorUsername(message); + rmessages.push({ role: message.author.id === message.client.user.id ? "assistant" : "user", content: content, - name: getAuthorUsername(message), + name: name, }); + if (name && rUserMap.has(name)) rUserMap.set(name, message.author.id); } - rvalue.push(...config.systemPrompt([...messages][0]).reverse()); + rmessages.push(...config.systemPrompt([...messages][0]).reverse()); - return rvalue.reverse(); + return [rmessages.reverse(), rUserMap]; } -- 2.45.2 From 6e0a5e72eaf15025421ac71b2a2d9fe487d45492 Mon Sep 17 00:00:00 2001 From: Wroclaw Date: Thu, 28 Sep 2023 11:42:01 +0200 Subject: [PATCH 2/2] functionManager: Forward username mapping here --- src/execution.ts | 5 +++-- src/funcitonManager.ts | 12 +++++++++--- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/execution.ts b/src/execution.ts index dada0a4..1f4b282 100644 --- a/src/execution.ts +++ b/src/execution.ts @@ -212,6 +212,7 @@ async function executeFromQueue(channel: string) { const message = channelQueue.at(0) as RequestMessage; let functionRanCounter = 0; let OpenAImessages: ChatCompletionMessageParam[] = []; + let modelUsernameMap: Map; // ignore if we can't even send anything to reply if (!canReplyToRequest(message)) return; @@ -237,7 +238,7 @@ async function executeFromQueue(channel: string) { return b.createdTimestamp - a.createdTimestamp; }); - [OpenAImessages] = toOpenAIMessages(messages.values()); + [OpenAImessages, modelUsernameMap] = toOpenAIMessages(messages.values()); let generatedMessage: ChatCompletionMessage | undefined = undefined; let answer: Awaited>; @@ -259,7 +260,7 @@ async function executeFromQueue(channel: string) { OpenAImessages.push(generatedMessage); // FIXME: don't use new instance of FunctionManager OpenAImessages.push( - new FunctionManager().handleFunction(generatedMessage.function_call) + new FunctionManager().handleFunction(generatedMessage.function_call, modelUsernameMap) ); } } while (generatedMessage.function_call); diff --git a/src/funcitonManager.ts b/src/funcitonManager.ts index 9bcb0ec..9af00d5 100644 --- a/src/funcitonManager.ts +++ b/src/funcitonManager.ts @@ -43,7 +43,10 @@ export abstract class OpenAIFunction { }; } - abstract execute(data: OpenAIFunctionRequestData): string; + abstract execute( + data: OpenAIFunctionRequestData, + modelUsernameMap: Map, + ): string; } /* @@ -64,7 +67,10 @@ export default class FunctionManager { return rvalue; } - public handleFunction(request: ChatCompletionFunctionCall): ChatCompletionMessageParam { + public handleFunction( + request: ChatCompletionFunctionCall, + modelUsernameMap: Map = new Map(), + ): ChatCompletionMessageParam { // eslint-disable-next-line @typescript-eslint/no-explicit-any let parsedArguments: any; @@ -91,7 +97,7 @@ export default class FunctionManager { role: "function", name: request.name, // eslint-disable-next-line @typescript-eslint/no-unsafe-argument - content: functionToRun.execute(parsedArguments), + content: functionToRun.execute(parsedArguments, modelUsernameMap), }; } } -- 2.45.2