Set a system role as a persistent role. (#134)

* set a system role as a persistent role

Fixes https://github.com/ztjhz/BetterChatGPT/issues/130

* handle system message exceed token limit + improve logic

---------

Co-authored-by: Jing Hua <tohjinghua123@gmail.com>
This commit is contained in:
Limour 2023-05-06 11:15:12 +08:00 committed by GitHub
parent f0a0156c22
commit 4cfbdac7d6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -51,13 +51,39 @@ export const limitMessageTokens = (
const limitedMessages: MessageInterface[] = [];
let tokenCount = 0;
for (let i = messages.length - 1; i >= 0; i--) {
const isSystemFirstMessage = messages[0]?.role === 'system';
let retainSystemMessage = false;
// Check if the first message is a system message and if it fits within the token limit
if (isSystemFirstMessage) {
const systemTokenCount = countTokens([messages[0]], model);
if (systemTokenCount < limit) {
tokenCount += systemTokenCount;
retainSystemMessage = true;
}
}
// Iterate through messages in reverse order, adding them to the limitedMessages array
// until the token limit is reached (excludes first message)
for (let i = messages.length - 1; i >= 1; i--) {
const count = countTokens([messages[i]], model);
if (count + tokenCount > limit) break;
tokenCount += count;
limitedMessages.unshift({ ...messages[i] });
}
// Process first message
if (retainSystemMessage) {
// Insert the system message in the third position from the end
limitedMessages.splice(-3, 0, { ...messages[0] });
} else if (!isSystemFirstMessage) {
// Check if the first message (non-system) can fit within the limit
const firstMessageTokenCount = countTokens([messages[0]], model);
if (firstMessageTokenCount + tokenCount < limit) {
limitedMessages.unshift({ ...messages[0] });
}
}
return limitedMessages;
};