perf: filter prompt

This commit is contained in:
archer
2023-05-09 19:11:21 +08:00
parent a745993829
commit cb3b9efc6e

View File

@@ -85,11 +85,39 @@ export const searchKb = async ({
};
const filterRate = filterRateMap[systemPrompts.length] || filterRateMap[0];
const filterPrompts = [
...(model.chat.systemPrompt
? [
{
obj: ChatRoleEnum.System,
value: model.chat.systemPrompt
}
]
: []),
...(model.chat.searchMode !== ModelVectorSearchModeEnum.noContext
? [
{
obj: ChatRoleEnum.System,
value: `我们来玩问答游戏,规则为:
1.你完全忘记你已有的知识
2.你只能回答关于"${model.name}"的问题
3.你只能从知识库中选择内容进行回答
4.如果问题不在知识库中,你会回答"我不知道。"
务必遵守规则`
}
]
: [])
];
const fixedSystemTokens = modelToolMap[model.chat.chatModel].countTokens({
messages: filterPrompts
});
const maxTokens = modelConstantsData.systemMaxToken - fixedSystemTokens;
const filterSystemPrompt = filterRate
.map((rate, i) =>
modelToolMap[model.chat.chatModel].sliceText({
text: systemPrompts[i],
length: Math.floor(modelConstantsData.systemMaxToken * rate)
length: Math.floor(maxTokens * rate)
})
)
.join('\n');
@@ -129,27 +157,7 @@ export const searchKb = async ({
obj: ChatRoleEnum.System,
value: `知识库:'${filterSystemPrompt}'`
},
...(model.chat.systemPrompt
? [
{
obj: ChatRoleEnum.System,
value: model.chat.systemPrompt
}
]
: []),
...(model.chat.searchMode !== ModelVectorSearchModeEnum.noContext
? [
{
obj: ChatRoleEnum.System,
value: `我们来玩问答游戏,规则为:
1.你完全忘记你已有的知识
2.你只能回答关于"${model.name}"的问题
3.你只能从知识库中选择内容进行回答
4.如果问题不在知识库中,你会回答"我不知道。"
务必遵守规则`
}
]
: [])
...filterPrompts
]
};
};