RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.
Go agent-harnessagent-native-databaseagentic-aiagentic-retrievalagentic-searchaiai-agentscontext-enginecontext-engineeringcontext-managementharness-engineeringknowledge-compilationragretrieval-augmented-generationretrieval-harnesssearch-harness
fix(ingestion): honor auto-keyword/auto-question top_n sliders in extractor prompts (#18632)
euvre743491b8fb11
diff --git a/internal/ingestion/component/extractor.go b/internal/ingestion/component/extractor.goindex d2cdcf04cd..b5646e1fe9 100644--- a/internal/ingestion/component/extractor.go+++ b/internal/ingestion/component/extractor.go@@ -126,6 +126,18 @@ func SetExtractorConcurrency(n int) {}}+// extractorTopNPattern matches the {{ topn }} placeholder accepted in+// keyword/question system prompts (same convention as rag/prompts/*.md).+// It is replaced with the configured top_n so the count slider stays+// authoritative even when a prompt was pre-filled by the frontend.+var extractorTopNPattern = regexp.MustCompile(`\{\{\s*topn\s*\}\}`)++// renderExtractorPrompt substitutes every {{ topn }} placeholder in the+// system prompt with the configured extraction count.+func renderExtractorPrompt(prompt string, topN int) string {+ return extractorTopNPattern.ReplaceAllString(prompt, strconv.Itoa(topN))+}+const (autoKeywordPrompt = `## RoleYou are a text analyzer.@@ -134,7 +146,7 @@ You are a text analyzer.Extract the most important keywords/phrases of a given piece of text content.## Requirements-- Summarize the text content, and give the top %d important keywords/phrases.+- Summarize the text content, and give the top {{ topn }} important keywords/phrases.- The keywords MUST be in the same language as the given piece of text content.- The keywords are delimited by ENGLISH COMMA.- Output keywords ONLY.`@@ -146,7 +158,7 @@ You are a text analyzer.Propose questions about a given piece of text content.## Requirements-- Understand and summarize the text content, and propose the top %d important questions.+- Understand and summarize the text content, and propose the top {{ topn }} important questions.- The questions SHOULD NOT have overlapping meanings.- The questions SHOULD cover the main content of the text as much as possible.- The questions MUST be in the same language as the given piece of text content.@@ -629,10 +641,11 @@ func (c *ExtractorComponent) runAutoKeywords(ctx context.Context, db *gorm.DB, iif topN <= 0 {return nil}- systemPrompt := c.Param.Keywords.SystemPrompt- if strings.TrimSpace(systemPrompt) == "" {- systemPrompt = fmt.Sprintf(autoKeywordPrompt, topN)+ systemPrompt := strings.TrimSpace(c.Param.Keywords.SystemPrompt)+ if systemPrompt == "" {+ systemPrompt = autoKeywordPrompt}+ systemPrompt = renderExtractorPrompt(systemPrompt, topN)kwTemp := extractorTemperaturekwIn := extractorInputs{llmID: in.llmID,@@ -669,10 +682,11 @@ func (c *ExtractorComponent) runAutoQuestions(ctx context.Context, db *gorm.DB,if topN <= 0 {return nil}- systemPrompt := c.Param.Questions.SystemPrompt- if strings.TrimSpace(systemPrompt) == "" {- systemPrompt = fmt.Sprintf(autoQuestionPrompt, topN)+ systemPrompt := strings.TrimSpace(c.Param.Questions.SystemPrompt)+ if systemPrompt == "" {+ systemPrompt = autoQuestionPrompt}+ systemPrompt = renderExtractorPrompt(systemPrompt, topN)qTemp := extractorTemperatureqIn := extractorInputs{llmID: in.llmID,diff --git a/internal/ingestion/component/extractor_test.go b/internal/ingestion/component/extractor_test.goindex 7aca970979..5c7e579d2e 100644--- a/internal/ingestion/component/extractor_test.go+++ b/internal/ingestion/component/extractor_test.go@@ -1712,6 +1712,103 @@ func TestExtractorCustomKeywordsAndQuestionsSystemPrompt(t *testing.T) {}}+// TestExtractorTopNPlaceholderSubstitution verifies the {{ topn }} placeholder+// in custom keyword/question system prompts is replaced with the configured+// top_n, so the count slider stays authoritative when the frontend pre-fills+// a prompt.+func TestExtractorTopNPlaceholderSubstitution(t *testing.T) {+ stub := withStubChatInvoker(t,+ stubResponse{Content: "alpha, beta"},+ stubResponse{Content: "q1?\nq2?"},+ )++ params := map[string]any{+ "llm_id": "llm-1",+ "keywords": map[string]any{+ "top_n": 9,+ "system_prompt": "Give the top {{ topn }} keywords.",+ },+ "questions": map[string]any{+ "top_n": 7,+ "system_prompt": "Propose {{topn}} questions.",+ },+ }++ comp, err := NewExtractorComponent(params)+ if err != nil {+ t.Fatalf("NewExtractorComponent: %v", err)+ }++ in := map[string]any{+ "chunks": []map[string]any{+ {"content_with_weight": "Content text."},+ },+ }++ if _, err := comp.Invoke(t.Context(), nil, in); err != nil {+ t.Fatalf("Invoke: %v", err)+ }++ reqs := stub.requests+ if len(reqs) != 2 {+ t.Fatalf("expected 2 LLM calls, got %d", len(reqs))+ }++ if got := reqs[0].Messages[0].Content; got != "Give the top 9 keywords." {+ t.Errorf("expected keywords prompt with top_n=9 substituted, got: %q", got)+ }+ if got := reqs[1].Messages[0].Content; got != "Propose 7 questions." {+ t.Errorf("expected questions prompt with top_n=7 substituted, got: %q", got)+ }+}++// TestExtractorDefaultPromptsRenderTopN verifies the built-in keyword/question+// prompts interpolate the configured top_n when no custom prompt is set.+func TestExtractorDefaultPromptsRenderTopN(t *testing.T) {+ stub := withStubChatInvoker(t,+ stubResponse{Content: "k1, k2"},+ stubResponse{Content: "q1?"},+ )++ params := map[string]any{+ "llm_id": "llm-1",+ "keywords": map[string]any{"top_n": 4},+ "questions": map[string]any{"top_n": 6},+ }++ comp, err := NewExtractorComponent(params)+ if err != nil {+ t.Fatalf("NewExtractorComponent: %v", err)+ }++ in := map[string]any{+ "chunks": []map[string]any{+ {"content_with_weight": "Content text."},+ },+ }++ if _, err := comp.Invoke(t.Context(), nil, in); err != nil {+ t.Fatalf("Invoke: %v", err)+ }++ reqs := stub.requests+ if len(reqs) != 2 {+ t.Fatalf("expected 2 LLM calls, got %d", len(reqs))+ }++ kwPrompt := reqs[0].Messages[0].Content+ if !strings.Contains(kwPrompt, "top 4 important keywords/phrases") {+ t.Errorf("expected built-in keywords prompt with top_n=4, got: %q", kwPrompt)+ }+ qPrompt := reqs[1].Messages[0].Content+ if !strings.Contains(qPrompt, "top 6 important questions") {+ t.Errorf("expected built-in questions prompt with top_n=6, got: %q", qPrompt)+ }+ if strings.Contains(kwPrompt, "{{") || strings.Contains(qPrompt, "{{") {+ t.Errorf("expected no leftover placeholders, got keywords=%q questions=%q", kwPrompt, qPrompt)+ }+}+func TestExtractorDisabledSummarySkipsCall(t *testing.T) {stub := withStubChatInvoker(t, stubResponse{Content: "Not expected"})diff --git a/web/src/locales/bg.ts b/web/src/locales/bg.tsindex 5a49118918..978936f5ab 100644--- a/web/src/locales/bg.ts+++ b/web/src/locales/bg.ts@@ -2186,7 +2186,7 @@ TaskExtract the most important keywords/phrases of a given piece of text content.Requirements-- Summarize the text content, and give the top 5 important keywords/phrases.+- Summarize the text content, and give the top {{ topn }} important keywords/phrases.- The keywords MUST be in the same language as the given piece of text content.- The keywords are delimited by ENGLISH COMMA.- Output keywords ONLY.`,@@ -2194,10 +2194,10 @@ RequirementsYou are a text analyzer.Task-Propose 3 questions about a given piece of text content.+Propose {{ topn }} questions about a given piece of text content.Requirements-- Understand and summarize the text content, and propose the top 3 important questions.+- Understand and summarize the text content, and propose the top {{ topn }} important questions.- The questions SHOULD NOT have overlapping meanings.- The questions SHOULD cover the main content of the text as much as possible.- The questions MUST be in the same language as the given piece of text content.diff --git a/web/src/locales/en.ts b/web/src/locales/en.tsindex a2bc4d9825..39d1373897 100644--- a/web/src/locales/en.ts+++ b/web/src/locales/en.ts@@ -3361,7 +3361,7 @@ TaskExtract the most important keywords/phrases of a given piece of text content.Requirements-- Summarize the text content, and give the top 5 important keywords/phrases.+- Summarize the text content, and give the top {{ topn }} important keywords/phrases.- The keywords MUST be in the same language as the given piece of text content.- The keywords are delimited by ENGLISH COMMA.- Output keywords ONLY.`,@@ -3369,10 +3369,10 @@ RequirementsYou are a text analyzer.Task-Propose 3 questions about a given piece of text content.+Propose {{ topn }} questions about a given piece of text content.Requirements-- Understand and summarize the text content, and propose the top 3 important questions.+- Understand and summarize the text content, and propose the top {{ topn }} important questions.- The questions SHOULD NOT have overlapping meanings.- The questions SHOULD cover the main content of the text as much as possible.- The questions MUST be in the same language as the given piece of text content.diff --git a/web/src/locales/ko.ts b/web/src/locales/ko.tsindex 5af43427e7..612a598385 100644--- a/web/src/locales/ko.ts+++ b/web/src/locales/ko.ts@@ -2679,7 +2679,7 @@ TaskExtract the most important keywords/phrases of a given piece of text content.Requirements-- Summarize the text content, and give the top 5 important keywords/phrases.+- Summarize the text content, and give the top {{ topn }} important keywords/phrases.- The keywords MUST be in the same language as the given piece of text content.- The keywords are delimited by ENGLISH COMMA.- Output keywords ONLY.`,@@ -2687,10 +2687,10 @@ RequirementsYou are a text analyzer.Task-Propose 3 questions about a given piece of text content.+Propose {{ topn }} questions about a given piece of text content.Requirements-- Understand and summarize the text content, and propose the top 3 important questions.+- Understand and summarize the text content, and propose the top {{ topn }} important questions.- The questions SHOULD NOT have overlapping meanings.- The questions SHOULD cover the main content of the text as much as possible.- The questions MUST be in the same language as the given piece of text content.diff --git a/web/src/locales/zh.ts b/web/src/locales/zh.tsindex b18fbe58c5..b7d5f0e4bb 100644--- a/web/src/locales/zh.ts+++ b/web/src/locales/zh.ts@@ -2941,7 +2941,7 @@ Tokenizer 会根据所选方式将内容存储为对应的数据结构。`,从给定的文本内容中提取最重要的关键词/短语。要求-- 总结文本内容,并给出最重要的5个关键词/短语。+- 总结文本内容,并给出最重要的{{ topn }}个关键词/短语。- 关键词必须与给定的文本内容使用相同的语言。- 关键词之间用英文逗号分隔。- 仅输出关键词。`,@@ -2949,10 +2949,10 @@ Tokenizer 会根据所选方式将内容存储为对应的数据结构。`,你是一名文本分析员。任务-针对给定的文本内容提出3个问题。+针对给定的文本内容提出{{ topn }}个问题。要求-- 理解并总结文本内容,并提出最重要的3个问题。+- 理解并总结文本内容,并提出最重要的{{ topn }}个问题。- 问题的含义不应重叠。- 问题应尽可能涵盖文本的主要内容。- 问题必须与给定的文本内容使用相同的语言。diff --git a/web/src/pages/agent/form/extractor-form/use-switch-prompt.ts b/web/src/pages/agent/form/extractor-form/use-switch-prompt.tsindex 1efe7b409c..00a90706a4 100644--- a/web/src/pages/agent/form/extractor-form/use-switch-prompt.ts+++ b/web/src/pages/agent/form/extractor-form/use-switch-prompt.ts@@ -4,6 +4,11 @@ import { useTranslation } from 'react-i18next';type SwitchPromptField = 'field_name' | 'sys_prompt' | 'prompts';+// The Python canvas Extractor has no top_n input and sends sys_prompt to the+// LLM verbatim, so its keyword and question prompts pin the {{ topn }}+// placeholder to fixed counts.+const pythonCanvasTopN: Record<string, number> = { keywords: 5, questions: 3 };+type SwitchPromptForm = {getValues(name: 'field_name'): string;setValue(@@ -20,10 +25,17 @@ export function useSwitchPrompt(form: SwitchPromptForm) {const setPromptValue = useCallback((field: SwitchPromptField, key: string, value: string) => {- form.setValue(field, t(`flow.prompts.${key}.${value}`), {- shouldDirty: true,- shouldValidate: true,- });+ const prompt = t(`flow.prompts.${key}.${value}`);+ form.setValue(+ field,+ Object.hasOwn(pythonCanvasTopN, value)+ ? prompt.replace(/\{\{\s*topn\s*\}\}/g, String(pythonCanvasTopN[value]))+ : prompt,+ {+ shouldDirty: true,+ shouldValidate: true,+ },+ );},[form, t],);