mirror of
https://github.com/NovaOSS/nova-betterchat.git
synced 2024-11-25 19:24:00 +01:00
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:
parent
f0a0156c22
commit
4cfbdac7d6
|
@ -51,13 +51,39 @@ export const limitMessageTokens = (
|
||||||
const limitedMessages: MessageInterface[] = [];
|
const limitedMessages: MessageInterface[] = [];
|
||||||
let tokenCount = 0;
|
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);
|
const count = countTokens([messages[i]], model);
|
||||||
if (count + tokenCount > limit) break;
|
if (count + tokenCount > limit) break;
|
||||||
tokenCount += count;
|
tokenCount += count;
|
||||||
limitedMessages.unshift({ ...messages[i] });
|
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;
|
return limitedMessages;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue