Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set a system role as a persistent role. #134

Merged
merged 15 commits into from
May 6, 2023
Merged
Next Next commit
set a system role as a persistent role
  • Loading branch information
Limour-dev authored Mar 26, 2023
commit bd8e72e7bb7f1a80bc79ab82c784b2197cdc3530
16 changes: 16 additions & 0 deletions src/utils/messageUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,29 @@ export const limitMessageTokens = (
const limitedMessages: MessageInterface[] = [];
let tokenCount = 0;

if (messages[0]?.role === 'system') {
const count = countTokens([messages[0]], model);
tokenCount += count;
}

for (let i = messages.length - 1; i >= 0; i--) {
const count = countTokens([messages[i]], model);
if (count + tokenCount > limit) break;
tokenCount += count;
limitedMessages.unshift({ ...messages[i] });
}

if (messages[0]?.role === 'system' && limitedMessages[0]?.role !== 'system') {
limitedMessages.unshift({ ...messages[0] });
}

if (limitedMessages.length > 4 && limitedMessages[0].role === 'system'){
const firstElement = limitedMessages.shift(); // 取出第一个元素并移除
if (firstElement !== undefined) { // 确保 firstElement 不为 undefined
limitedMessages.splice(-3, 0, firstElement); // 将第一个元素插入到倒数第四个位置
}
}

return limitedMessages;
};

Expand Down