WIP: User descriptions for users #18

Draft
Wroclaw wants to merge 3 commits from feature/user-decriptions into main
3 changed files with 25 additions and 12 deletions

View file

@ -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<string, string>;
// 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<ReturnType<typeof openai.chat.completions.create>>;
@ -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);

View file

@ -43,7 +43,10 @@ export abstract class OpenAIFunction<T extends nameTypeMap = nameTypeMap> {
};
}
abstract execute(data: OpenAIFunctionRequestData<T>): string;
abstract execute(
data: OpenAIFunctionRequestData<T>,
modelUsernameMap: Map<string, string>,
): string;
}
/*
@ -64,7 +67,10 @@ export default class FunctionManager {
return rvalue;
}
public handleFunction(request: ChatCompletionFunctionCall): ChatCompletionMessageParam {
public handleFunction(
request: ChatCompletionFunctionCall,
modelUsernameMap: Map<string, string> = 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),
};
}
}

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
* 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<DiscordMessage>,
): OpenAIMessage[] {
const rvalue: OpenAIMessage[] = [];
): [OpenAIMessage[], Map<string, string>] {
const rmessages: OpenAIMessage[] = [];
const rUserMap = new Map<string, string>();
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];
}