toOpenAIMessages: also return the username to user id map

This will be used for fetching user descriptions very soon
This commit is contained in:
Wroclaw 2023-09-28 11:01:35 +02:00
parent b7f6a5fe91
commit 52f16c9d0a
2 changed files with 14 additions and 8 deletions

View file

@ -237,7 +237,7 @@ async function executeFromQueue(channel: string) {
return b.createdTimestamp - a.createdTimestamp; return b.createdTimestamp - a.createdTimestamp;
}); });
OpenAImessages = toOpenAIMessages(messages.values()); [OpenAImessages] = toOpenAIMessages(messages.values());
let generatedMessage: ChatCompletionMessage | undefined = undefined; let generatedMessage: ChatCompletionMessage | undefined = undefined;
let answer: Awaited<ReturnType<typeof openai.chat.completions.create>>; let answer: Awaited<ReturnType<typeof openai.chat.completions.create>>;

View file

@ -95,28 +95,34 @@ function getAuthorUsername(message: DiscordMessage): string | undefined {
/** /**
* Converts the Iterable of Discord Messages to array of OpenAI Messages to send * 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 message in the interable will be the last of the returned array (reverse order)
* (first should be newest)
* @param messages the iterable to convert * @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( export default function toOpenAIMessages(
messages: Iterable<DiscordMessage>, messages: Iterable<DiscordMessage>,
): OpenAIMessage[] { ): [OpenAIMessage[], Map<string, string>] {
const rvalue: OpenAIMessage[] = []; const rmessages: OpenAIMessage[] = [];
const rUserMap = new Map<string, string>();
let tokenCount = 0; let tokenCount = 0;
for (const message of messages) { for (const message of messages) {
// FIXME: multiple users could have the same name here.
const content = formatMessage(message); 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. // FIXME: tokens are not being counted properly (it's lower than it is) but it's enough for me for now.
tokenCount += countTokens(content); tokenCount += countTokens(content);
if (tokenCount > config.readLimits.tokens) break; if (tokenCount > config.readLimits.tokens) break;
rvalue.push({ const name = getAuthorUsername(message);
rmessages.push({
role: message.author.id === message.client.user.id ? "assistant" : "user", role: message.author.id === message.client.user.id ? "assistant" : "user",
content: content, 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];
} }