perf: message filter

This commit is contained in:
archer 2023-05-09 23:40:02 +08:00
parent 9b683884cc
commit e3c9b8179e
No known key found for this signature in database
GPG Key ID: 569A5660D2379E28
4 changed files with 7 additions and 21 deletions

View File

@ -72,7 +72,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
} else { } else {
// 没有用知识库搜索,仅用系统提示词 // 没有用知识库搜索,仅用系统提示词
model.chat.systemPrompt && model.chat.systemPrompt &&
prompts.unshift({ prompts.splice(prompts.length - 1, 0, {
obj: ChatRoleEnum.System, obj: ChatRoleEnum.System,
value: model.chat.systemPrompt value: model.chat.systemPrompt
}); });

View File

@ -87,13 +87,12 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
prompts.splice(prompts.length - 1, 0, ...searchPrompts); prompts.splice(prompts.length - 1, 0, ...searchPrompts);
} else { } else {
// 没有用知识库搜索,仅用系统提示词 // 没有用知识库搜索,仅用系统提示词
if (model.chat.systemPrompt) { model.chat.systemPrompt &&
prompts.unshift({ prompts.splice(prompts.length - 1, 0, {
obj: ChatRoleEnum.System, obj: ChatRoleEnum.System,
value: model.chat.systemPrompt value: model.chat.systemPrompt
}); });
} }
}
// 计算温度 // 计算温度
const temperature = (modelConstantsData.maxTemperature * (model.chat.temperature / 10)).toFixed( const temperature = (modelConstantsData.maxTemperature * (model.chat.temperature / 10)).toFixed(

View File

@ -106,7 +106,7 @@ export const searchKb = async ({
{ {
obj: ChatRoleEnum.System, obj: ChatRoleEnum.System,
value: `我们来玩问答游戏,规则为: value: `我们来玩问答游戏,规则为:
1. 1.
2."${model.name}" 2."${model.name}"
3. 3.
4.,"我不知道。" 4.,"我不知道。"

View File

@ -109,35 +109,22 @@ export const ChatContextFilter = ({
// 根据 tokens 截断内容 // 根据 tokens 截断内容
const chats: ChatItemSimpleType[] = []; const chats: ChatItemSimpleType[] = [];
let systemPrompt: ChatItemSimpleType | null = null;
// System 词保留
if (formatPrompts[0].obj === ChatRoleEnum.System) {
const prompt = formatPrompts.shift();
if (prompt) {
systemPrompt = prompt;
}
}
let messages: ChatItemSimpleType[] = [];
// 从后往前截取对话内容 // 从后往前截取对话内容
for (let i = formatPrompts.length - 1; i >= 0; i--) { for (let i = formatPrompts.length - 1; i >= 0; i--) {
chats.unshift(formatPrompts[i]); chats.unshift(formatPrompts[i]);
messages = systemPrompt ? [systemPrompt, ...chats] : chats;
const tokens = modelToolMap[model].countTokens({ const tokens = modelToolMap[model].countTokens({
messages messages: chats
}); });
/* 整体 tokens 超出范围 */ /* 整体 tokens 超出范围 */
if (tokens >= maxTokens) { if (tokens >= maxTokens) {
return systemPrompt ? [systemPrompt, ...chats.slice(1)] : chats.slice(1); return chats.slice(1);
} }
} }
return messages; return chats;
}; };
/* stream response */ /* stream response */