Skip to content

Commit

Permalink
perf: message filter
Browse files Browse the repository at this point in the history
  • Loading branch information
c121914yu committed May 9, 2023
1 parent 9b68388 commit e3c9b81
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/pages/api/chat/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
} else {
// 没有用知识库搜索,仅用系统提示词
model.chat.systemPrompt &&
prompts.unshift({
prompts.splice(prompts.length - 1, 0, {
obj: ChatRoleEnum.System,
value: model.chat.systemPrompt
});
Expand Down
5 changes: 2 additions & 3 deletions src/pages/api/openapi/chat/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,11 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
prompts.splice(prompts.length - 1, 0, ...searchPrompts);
} else {
// 没有用知识库搜索,仅用系统提示词
if (model.chat.systemPrompt) {
prompts.unshift({
model.chat.systemPrompt &&
prompts.splice(prompts.length - 1, 0, {
obj: ChatRoleEnum.System,
value: model.chat.systemPrompt
});
}
}

// 计算温度
Expand Down
2 changes: 1 addition & 1 deletion src/service/plugins/searchKb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export const searchKb = async ({
{
obj: ChatRoleEnum.System,
value: `我们来玩问答游戏,规则为:
1.你忘记你已有的知识
1.你完全忘记你已有的知识
2.你只能回答关于"${model.name}"的问题
3.你只能从知识库中选择内容进行回答
4.如果问题不在知识库中,你会回答"我不知道。"
Expand Down
19 changes: 3 additions & 16 deletions src/service/utils/chat/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,35 +109,22 @@ export const ChatContextFilter = ({

// 根据 tokens 截断内容
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--) {
chats.unshift(formatPrompts[i]);

messages = systemPrompt ? [systemPrompt, ...chats] : chats;

const tokens = modelToolMap[model].countTokens({
messages
messages: chats
});

/* 整体 tokens 超出范围 */
if (tokens >= maxTokens) {
return systemPrompt ? [systemPrompt, ...chats.slice(1)] : chats.slice(1);
return chats.slice(1);
}
}

return messages;
return chats;
};

/* stream response */
Expand Down

0 comments on commit e3c9b81

Please sign in to comment.