perf: 文本截取

This commit is contained in:
archer
2023-04-17 09:02:39 +08:00
parent 426eceac22
commit 0db413ab52
6 changed files with 10 additions and 38 deletions

View File

@@ -110,12 +110,13 @@ export const openaiChatFilter = (prompts: ChatItemType[], maxTokens: number) =>
// 从后往前截取
for (let i = formatPrompts.length - 1; i >= 0; i--) {
const tokens = encode(formatPrompts[i].value).length;
if (maxTokens >= tokens) {
res.unshift(formatPrompts[i]);
maxTokens -= tokens;
} else {
res.unshift(formatPrompts[i]);
/* 整体 tokens 超出范围 */
if (tokens >= maxTokens) {
break;
}
maxTokens -= tokens;
}
return systemPrompt ? [systemPrompt, ...res] : res;