Remove quote prompt in api request (#4724)
* chat completion add parsequote param (#4720) * chat completion add parsequote param * fix * perf: quote prompt --------- Co-authored-by: heheer <heheer@sealos.io>
This commit is contained in:
parent
b0297d2915
commit
360a2ec392
@ -321,13 +321,24 @@ export const Prompt_systemQuotePromptList: PromptTemplateItem[] = [
|
|||||||
}
|
}
|
||||||
];
|
];
|
||||||
|
|
||||||
export const getQuotePrompt = (version?: string, role: 'user' | 'system' = 'user') => {
|
export const getQuotePrompt = (
|
||||||
|
version?: string,
|
||||||
|
role: 'user' | 'system' = 'user',
|
||||||
|
parseQuote = true
|
||||||
|
) => {
|
||||||
const quotePromptTemplates =
|
const quotePromptTemplates =
|
||||||
role === 'user' ? Prompt_userQuotePromptList : Prompt_systemQuotePromptList;
|
role === 'user' ? Prompt_userQuotePromptList : Prompt_systemQuotePromptList;
|
||||||
|
|
||||||
const defaultTemplate = quotePromptTemplates[0].value;
|
const defaultTemplate = quotePromptTemplates[0].value;
|
||||||
|
|
||||||
return getPromptByVersion(version, defaultTemplate);
|
return parseQuote
|
||||||
|
? getPromptByVersion(version, defaultTemplate)
|
||||||
|
: getPromptByVersion(version, defaultTemplate).replace(
|
||||||
|
`- 使用 [id](QUOTE) 格式来引用<Reference></Reference>中的知识,其中 QUOTE 是固定常量, id 为引文中的 id。
|
||||||
|
- 在每段结尾自然地整合引用。例如: "FastGPT 是一个基于大语言模型(LLM)的知识库问答系统[67e517e74767063e882d6861](QUOTE)。"
|
||||||
|
- 每段至少包含一个引用,也可根据内容需要加入多个引用,按顺序排列。`,
|
||||||
|
''
|
||||||
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
// Document quote prompt
|
// Document quote prompt
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
export const getDatasetSearchToolResponsePrompt = () => {
|
export const getDatasetSearchToolResponsePrompt = (parseQuote: boolean) => {
|
||||||
return `## Role
|
return parseQuote
|
||||||
|
? `## Role
|
||||||
你是一个知识库回答助手,可以 "quotes" 中的内容作为本次对话的参考。为了使回答结果更加可信并且可追溯,你需要在每段话结尾添加引用标记。
|
你是一个知识库回答助手,可以 "quotes" 中的内容作为本次对话的参考。为了使回答结果更加可信并且可追溯,你需要在每段话结尾添加引用标记。
|
||||||
|
|
||||||
## Rules
|
## Rules
|
||||||
@ -10,5 +11,14 @@ export const getDatasetSearchToolResponsePrompt = () => {
|
|||||||
- 使用与问题相同的语言回答。
|
- 使用与问题相同的语言回答。
|
||||||
- 使用 [id](QUOTE) 格式来引用 "quotes" 中的知识,其中 QUOTE 是固定常量, id 为引文中的 id。
|
- 使用 [id](QUOTE) 格式来引用 "quotes" 中的知识,其中 QUOTE 是固定常量, id 为引文中的 id。
|
||||||
- 在每段话结尾自然地整合引用。例如: "FastGPT 是一个基于大语言模型(LLM)的知识库问答系统[67e517e74767063e882d6861](QUOTE)。"
|
- 在每段话结尾自然地整合引用。例如: "FastGPT 是一个基于大语言模型(LLM)的知识库问答系统[67e517e74767063e882d6861](QUOTE)。"
|
||||||
- 每段话至少包含一个引用,也可根据内容需要加入多个引用,按顺序排列。`;
|
- 每段话至少包含一个引用,也可根据内容需要加入多个引用,按顺序排列。`
|
||||||
|
: `## Role
|
||||||
|
你是一个知识库回答助手,可以 "quotes" 中的内容作为本次对话的参考。
|
||||||
|
|
||||||
|
## Rules
|
||||||
|
- 如果你不清楚答案,你需要澄清。
|
||||||
|
- 避免提及你是从 "quotes" 获取的知识。
|
||||||
|
- 保持答案与 "quotes" 中描述的一致。
|
||||||
|
- 使用 Markdown 语法优化回答格式。尤其是图片、表格、序列号等内容,需严格完整输出。
|
||||||
|
- 使用与问题相同的语言回答。`;
|
||||||
};
|
};
|
||||||
|
|||||||
@ -58,6 +58,7 @@ export type ChatDispatchProps = {
|
|||||||
chatConfig: AppSchema['chatConfig'];
|
chatConfig: AppSchema['chatConfig'];
|
||||||
lastInteractive?: WorkflowInteractiveResponseType; // last interactive response
|
lastInteractive?: WorkflowInteractiveResponseType; // last interactive response
|
||||||
stream: boolean;
|
stream: boolean;
|
||||||
|
parseQuote?: boolean;
|
||||||
maxRunTimes: number;
|
maxRunTimes: number;
|
||||||
isToolCall?: boolean;
|
isToolCall?: boolean;
|
||||||
workflowStreamResponse?: WorkflowResponseType;
|
workflowStreamResponse?: WorkflowResponseType;
|
||||||
|
|||||||
@ -75,6 +75,7 @@ export const dispatchChatCompletion = async (props: ChatProps): Promise<ChatResp
|
|||||||
res,
|
res,
|
||||||
requestOrigin,
|
requestOrigin,
|
||||||
stream = false,
|
stream = false,
|
||||||
|
parseQuote = true,
|
||||||
externalProvider,
|
externalProvider,
|
||||||
histories,
|
histories,
|
||||||
node: { name, version },
|
node: { name, version },
|
||||||
@ -158,7 +159,8 @@ export const dispatchChatCompletion = async (props: ChatProps): Promise<ChatResp
|
|||||||
userChatInput,
|
userChatInput,
|
||||||
systemPrompt,
|
systemPrompt,
|
||||||
userFiles,
|
userFiles,
|
||||||
documentQuoteText
|
documentQuoteText,
|
||||||
|
parseQuote
|
||||||
}),
|
}),
|
||||||
// Censor = true and system key, will check content
|
// Censor = true and system key, will check content
|
||||||
(() => {
|
(() => {
|
||||||
@ -450,7 +452,8 @@ async function getChatMessages({
|
|||||||
systemPrompt,
|
systemPrompt,
|
||||||
userChatInput,
|
userChatInput,
|
||||||
userFiles,
|
userFiles,
|
||||||
documentQuoteText
|
documentQuoteText,
|
||||||
|
parseQuote = true
|
||||||
}: {
|
}: {
|
||||||
model: LLMModelItemType;
|
model: LLMModelItemType;
|
||||||
maxTokens?: number;
|
maxTokens?: number;
|
||||||
@ -467,13 +470,16 @@ async function getChatMessages({
|
|||||||
|
|
||||||
userFiles: UserChatItemValueItemType['file'][];
|
userFiles: UserChatItemValueItemType['file'][];
|
||||||
documentQuoteText?: string; // document quote
|
documentQuoteText?: string; // document quote
|
||||||
|
parseQuote?: boolean;
|
||||||
}) {
|
}) {
|
||||||
// Dataset prompt ====>
|
// Dataset prompt ====>
|
||||||
// User role or prompt include question
|
// User role or prompt include question
|
||||||
const quoteRole =
|
const quoteRole =
|
||||||
aiChatQuoteRole === 'user' || datasetQuotePrompt.includes('{{question}}') ? 'user' : 'system';
|
aiChatQuoteRole === 'user' || datasetQuotePrompt.includes('{{question}}') ? 'user' : 'system';
|
||||||
|
|
||||||
const datasetQuotePromptTemplate = datasetQuotePrompt || getQuotePrompt(version, quoteRole);
|
const defaultQuotePrompt = getQuotePrompt(version, quoteRole, parseQuote);
|
||||||
|
|
||||||
|
const datasetQuotePromptTemplate = datasetQuotePrompt || defaultQuotePrompt;
|
||||||
|
|
||||||
// Reset user input, add dataset quote to user input
|
// Reset user input, add dataset quote to user input
|
||||||
const replaceInputValue =
|
const replaceInputValue =
|
||||||
|
|||||||
@ -55,6 +55,7 @@ export async function dispatchDatasetSearch(
|
|||||||
runningUserInfo: { tmbId },
|
runningUserInfo: { tmbId },
|
||||||
histories,
|
histories,
|
||||||
node,
|
node,
|
||||||
|
parseQuote = true,
|
||||||
params: {
|
params: {
|
||||||
datasets = [],
|
datasets = [],
|
||||||
similarity,
|
similarity,
|
||||||
@ -266,7 +267,7 @@ export async function dispatchDatasetSearch(
|
|||||||
[DispatchNodeResponseKeyEnum.nodeResponse]: responseData,
|
[DispatchNodeResponseKeyEnum.nodeResponse]: responseData,
|
||||||
nodeDispatchUsages,
|
nodeDispatchUsages,
|
||||||
[DispatchNodeResponseKeyEnum.toolResponses]: {
|
[DispatchNodeResponseKeyEnum.toolResponses]: {
|
||||||
prompt: getDatasetSearchToolResponsePrompt(),
|
prompt: getDatasetSearchToolResponsePrompt(parseQuote),
|
||||||
quotes: searchRes.map((item) => ({
|
quotes: searchRes.map((item) => ({
|
||||||
id: item.id,
|
id: item.id,
|
||||||
sourceName: item.sourceName,
|
sourceName: item.sourceName,
|
||||||
|
|||||||
@ -135,6 +135,7 @@ export async function dispatchWorkFlow(data: Props): Promise<DispatchFlowRespons
|
|||||||
timezone,
|
timezone,
|
||||||
externalProvider,
|
externalProvider,
|
||||||
stream = false,
|
stream = false,
|
||||||
|
parseQuote = true,
|
||||||
version = 'v1',
|
version = 'v1',
|
||||||
responseDetail = true,
|
responseDetail = true,
|
||||||
responseAllData = true,
|
responseAllData = true,
|
||||||
@ -606,6 +607,7 @@ export async function dispatchWorkFlow(data: Props): Promise<DispatchFlowRespons
|
|||||||
timezone,
|
timezone,
|
||||||
externalProvider,
|
externalProvider,
|
||||||
stream,
|
stream,
|
||||||
|
parseQuote,
|
||||||
node,
|
node,
|
||||||
runtimeNodes,
|
runtimeNodes,
|
||||||
runtimeEdges,
|
runtimeEdges,
|
||||||
|
|||||||
@ -74,6 +74,7 @@ export type Props = ChatCompletionCreateParams &
|
|||||||
responseChatItemId?: string;
|
responseChatItemId?: string;
|
||||||
stream?: boolean;
|
stream?: boolean;
|
||||||
detail?: boolean;
|
detail?: boolean;
|
||||||
|
parseQuote?: boolean;
|
||||||
variables: Record<string, any>; // Global variables or plugin inputs
|
variables: Record<string, any>; // Global variables or plugin inputs
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -106,6 +107,7 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
|
|
||||||
stream = false,
|
stream = false,
|
||||||
detail = false,
|
detail = false,
|
||||||
|
parseQuote = false,
|
||||||
messages = [],
|
messages = [],
|
||||||
variables = {},
|
variables = {},
|
||||||
responseChatItemId = getNanoid(),
|
responseChatItemId = getNanoid(),
|
||||||
@ -289,6 +291,7 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
chatConfig,
|
chatConfig,
|
||||||
histories: newHistories,
|
histories: newHistories,
|
||||||
stream,
|
stream,
|
||||||
|
parseQuote,
|
||||||
maxRunTimes: WORKFLOW_MAX_RUN_TIMES,
|
maxRunTimes: WORKFLOW_MAX_RUN_TIMES,
|
||||||
workflowStreamResponse: workflowResponseWrite
|
workflowStreamResponse: workflowResponseWrite
|
||||||
});
|
});
|
||||||
|
|||||||
@ -74,6 +74,7 @@ export type Props = ChatCompletionCreateParams &
|
|||||||
responseChatItemId?: string;
|
responseChatItemId?: string;
|
||||||
stream?: boolean;
|
stream?: boolean;
|
||||||
detail?: boolean;
|
detail?: boolean;
|
||||||
|
parseQuote?: boolean;
|
||||||
variables: Record<string, any>; // Global variables or plugin inputs
|
variables: Record<string, any>; // Global variables or plugin inputs
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -106,6 +107,7 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
|
|
||||||
stream = false,
|
stream = false,
|
||||||
detail = false,
|
detail = false,
|
||||||
|
parseQuote = false,
|
||||||
messages = [],
|
messages = [],
|
||||||
variables = {},
|
variables = {},
|
||||||
responseChatItemId = getNanoid(),
|
responseChatItemId = getNanoid(),
|
||||||
@ -288,6 +290,7 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
chatConfig,
|
chatConfig,
|
||||||
histories: newHistories,
|
histories: newHistories,
|
||||||
stream,
|
stream,
|
||||||
|
parseQuote,
|
||||||
maxRunTimes: WORKFLOW_MAX_RUN_TIMES,
|
maxRunTimes: WORKFLOW_MAX_RUN_TIMES,
|
||||||
workflowStreamResponse: workflowResponseWrite,
|
workflowStreamResponse: workflowResponseWrite,
|
||||||
version: 'v2',
|
version: 'v2',
|
||||||
|
|||||||
@ -131,7 +131,8 @@ export const streamFetch = ({
|
|||||||
...data,
|
...data,
|
||||||
variables,
|
variables,
|
||||||
detail: true,
|
detail: true,
|
||||||
stream: true
|
stream: true,
|
||||||
|
parseQuote: true
|
||||||
})
|
})
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user