Skip to content

Commit 360a2ec

Browse files
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 <[email protected]>
1 parent b0297d2 commit 360a2ec

File tree

9 files changed

+48
-10
lines changed

9 files changed

+48
-10
lines changed

packages/global/core/ai/prompt/AIChat.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -321,13 +321,24 @@ export const Prompt_systemQuotePromptList: PromptTemplateItem[] = [
321321
}
322322
];
323323

324-
export const getQuotePrompt = (version?: string, role: 'user' | 'system' = 'user') => {
324+
export const getQuotePrompt = (
325+
version?: string,
326+
role: 'user' | 'system' = 'user',
327+
parseQuote = true
328+
) => {
325329
const quotePromptTemplates =
326330
role === 'user' ? Prompt_userQuotePromptList : Prompt_systemQuotePromptList;
327331

328332
const defaultTemplate = quotePromptTemplates[0].value;
329333

330-
return getPromptByVersion(version, defaultTemplate);
334+
return parseQuote
335+
? getPromptByVersion(version, defaultTemplate)
336+
: getPromptByVersion(version, defaultTemplate).replace(
337+
`- 使用 [id](QUOTE) 格式来引用<Reference></Reference>中的知识,其中 QUOTE 是固定常量, id 为引文中的 id。
338+
- 在每段结尾自然地整合引用。例如: "FastGPT 是一个基于大语言模型(LLM)的知识库问答系统[67e517e74767063e882d6861](QUOTE)。"
339+
- 每段至少包含一个引用,也可根据内容需要加入多个引用,按顺序排列。`,
340+
''
341+
);
331342
};
332343

333344
// Document quote prompt
Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
export const getDatasetSearchToolResponsePrompt = () => {
2-
return `## Role
1+
export const getDatasetSearchToolResponsePrompt = (parseQuote: boolean) => {
2+
return parseQuote
3+
? `## Role
34
你是一个知识库回答助手,可以 "quotes" 中的内容作为本次对话的参考。为了使回答结果更加可信并且可追溯,你需要在每段话结尾添加引用标记。
45
56
## Rules
@@ -10,5 +11,14 @@ export const getDatasetSearchToolResponsePrompt = () => {
1011
- 使用与问题相同的语言回答。
1112
- 使用 [id](QUOTE) 格式来引用 "quotes" 中的知识,其中 QUOTE 是固定常量, id 为引文中的 id。
1213
- 在每段话结尾自然地整合引用。例如: "FastGPT 是一个基于大语言模型(LLM)的知识库问答系统[67e517e74767063e882d6861](QUOTE)。"
13-
- 每段话至少包含一个引用,也可根据内容需要加入多个引用,按顺序排列。`;
14+
- 每段话至少包含一个引用,也可根据内容需要加入多个引用,按顺序排列。`
15+
: `## Role
16+
你是一个知识库回答助手,可以 "quotes" 中的内容作为本次对话的参考。
17+
18+
## Rules
19+
- 如果你不清楚答案,你需要澄清。
20+
- 避免提及你是从 "quotes" 获取的知识。
21+
- 保持答案与 "quotes" 中描述的一致。
22+
- 使用 Markdown 语法优化回答格式。尤其是图片、表格、序列号等内容,需严格完整输出。
23+
- 使用与问题相同的语言回答。`;
1424
};

packages/global/core/workflow/runtime/type.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ export type ChatDispatchProps = {
5858
chatConfig: AppSchema['chatConfig'];
5959
lastInteractive?: WorkflowInteractiveResponseType; // last interactive response
6060
stream: boolean;
61+
parseQuote?: boolean;
6162
maxRunTimes: number;
6263
isToolCall?: boolean;
6364
workflowStreamResponse?: WorkflowResponseType;

packages/service/core/workflow/dispatch/chat/oneapi.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ export const dispatchChatCompletion = async (props: ChatProps): Promise<ChatResp
7575
res,
7676
requestOrigin,
7777
stream = false,
78+
parseQuote = true,
7879
externalProvider,
7980
histories,
8081
node: { name, version },
@@ -158,7 +159,8 @@ export const dispatchChatCompletion = async (props: ChatProps): Promise<ChatResp
158159
userChatInput,
159160
systemPrompt,
160161
userFiles,
161-
documentQuoteText
162+
documentQuoteText,
163+
parseQuote
162164
}),
163165
// Censor = true and system key, will check content
164166
(() => {
@@ -450,7 +452,8 @@ async function getChatMessages({
450452
systemPrompt,
451453
userChatInput,
452454
userFiles,
453-
documentQuoteText
455+
documentQuoteText,
456+
parseQuote = true
454457
}: {
455458
model: LLMModelItemType;
456459
maxTokens?: number;
@@ -467,13 +470,16 @@ async function getChatMessages({
467470

468471
userFiles: UserChatItemValueItemType['file'][];
469472
documentQuoteText?: string; // document quote
473+
parseQuote?: boolean;
470474
}) {
471475
// Dataset prompt ====>
472476
// User role or prompt include question
473477
const quoteRole =
474478
aiChatQuoteRole === 'user' || datasetQuotePrompt.includes('{{question}}') ? 'user' : 'system';
475479

476-
const datasetQuotePromptTemplate = datasetQuotePrompt || getQuotePrompt(version, quoteRole);
480+
const defaultQuotePrompt = getQuotePrompt(version, quoteRole, parseQuote);
481+
482+
const datasetQuotePromptTemplate = datasetQuotePrompt || defaultQuotePrompt;
477483

478484
// Reset user input, add dataset quote to user input
479485
const replaceInputValue =

packages/service/core/workflow/dispatch/dataset/search.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export async function dispatchDatasetSearch(
5555
runningUserInfo: { tmbId },
5656
histories,
5757
node,
58+
parseQuote = true,
5859
params: {
5960
datasets = [],
6061
similarity,
@@ -266,7 +267,7 @@ export async function dispatchDatasetSearch(
266267
[DispatchNodeResponseKeyEnum.nodeResponse]: responseData,
267268
nodeDispatchUsages,
268269
[DispatchNodeResponseKeyEnum.toolResponses]: {
269-
prompt: getDatasetSearchToolResponsePrompt(),
270+
prompt: getDatasetSearchToolResponsePrompt(parseQuote),
270271
quotes: searchRes.map((item) => ({
271272
id: item.id,
272273
sourceName: item.sourceName,

packages/service/core/workflow/dispatch/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ export async function dispatchWorkFlow(data: Props): Promise<DispatchFlowRespons
135135
timezone,
136136
externalProvider,
137137
stream = false,
138+
parseQuote = true,
138139
version = 'v1',
139140
responseDetail = true,
140141
responseAllData = true,
@@ -606,6 +607,7 @@ export async function dispatchWorkFlow(data: Props): Promise<DispatchFlowRespons
606607
timezone,
607608
externalProvider,
608609
stream,
610+
parseQuote,
609611
node,
610612
runtimeNodes,
611613
runtimeEdges,

projects/app/src/pages/api/v1/chat/completions.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ export type Props = ChatCompletionCreateParams &
7474
responseChatItemId?: string;
7575
stream?: boolean;
7676
detail?: boolean;
77+
parseQuote?: boolean;
7778
variables: Record<string, any>; // Global variables or plugin inputs
7879
};
7980

@@ -106,6 +107,7 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
106107

107108
stream = false,
108109
detail = false,
110+
parseQuote = false,
109111
messages = [],
110112
variables = {},
111113
responseChatItemId = getNanoid(),
@@ -289,6 +291,7 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
289291
chatConfig,
290292
histories: newHistories,
291293
stream,
294+
parseQuote,
292295
maxRunTimes: WORKFLOW_MAX_RUN_TIMES,
293296
workflowStreamResponse: workflowResponseWrite
294297
});

projects/app/src/pages/api/v2/chat/completions.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ export type Props = ChatCompletionCreateParams &
7474
responseChatItemId?: string;
7575
stream?: boolean;
7676
detail?: boolean;
77+
parseQuote?: boolean;
7778
variables: Record<string, any>; // Global variables or plugin inputs
7879
};
7980

@@ -106,6 +107,7 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
106107

107108
stream = false,
108109
detail = false,
110+
parseQuote = false,
109111
messages = [],
110112
variables = {},
111113
responseChatItemId = getNanoid(),
@@ -288,6 +290,7 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
288290
chatConfig,
289291
histories: newHistories,
290292
stream,
293+
parseQuote,
291294
maxRunTimes: WORKFLOW_MAX_RUN_TIMES,
292295
workflowStreamResponse: workflowResponseWrite,
293296
version: 'v2',

projects/app/src/web/common/api/fetch.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,8 @@ export const streamFetch = ({
131131
...data,
132132
variables,
133133
detail: true,
134-
stream: true
134+
stream: true,
135+
parseQuote: true
135136
})
136137
};
137138

0 commit comments

Comments
 (0)