Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

refactor: knowledge compilation templates (#20383)

Jin Haifb2699f9b339
diff --git a/Dockerfile b/Dockerfile
index edf6e94357..55e2b74958 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -254,7 +254,7 @@ COPY rag/prompts rag/prompts
# Wiki page-structure presets read at runtime by the Go backend
# (CompilationTemplateService.LoadWikiPresets).
-COPY api/db/init_data/compilation_templates ./api/db/init_data/compilation_templates
+COPY internal/ingestion/knowledge_compile/templates ./internal/ingestion/knowledge_compile/templates
# Copy compiled web pages
diff --git a/internal/ingestion/component/knowledge_compiler/tree/raptor.go b/internal/ingestion/component/knowledge_compiler/tree/raptor.go
index 833ac75dfd..0003efc78c 100644
--- a/internal/ingestion/component/knowledge_compiler/tree/raptor.go
+++ b/internal/ingestion/component/knowledge_compiler/tree/raptor.go
@@ -667,13 +667,7 @@ func toFloat64Matrix(vecs [][]float32) [][]float64 {
return out
}
-// defaultRaptorPrompt is the summary task template. It mirrors the production
-// prompt from api/db/init_data/compilation_templates/tree.yaml (the tree
-// compilation template), NOT the Python fallback in compiler.py:128. The
-// {cluster_content} placeholder is filled with the joined cluster text. The
-// YAML literal block carries a base indent of 6 spaces before {cluster_content};
-// those 6 spaces are part of the prompt and MUST be preserved. A caller may
-// override it via extra["prompt"] (Python: raptor_cfg["prompt"]).
+// defaultRaptorPrompt is the summary task template.
const defaultRaptorPrompt = "Please summarize the following paragraphs. Be careful with the numbers, do not make things up. Paragraphs as following:\n {cluster_content}\nThe above is the content you need to summarize."
// raptorSystemHelper mirrors the leading "You're a helpful assistant.\n\nHelp me
diff --git a/internal/ingestion/component/knowledge_compiler/tree/raptor_test.go b/internal/ingestion/component/knowledge_compiler/tree/raptor_test.go
index fbe0eea028..c8a71607dc 100644
--- a/internal/ingestion/component/knowledge_compiler/tree/raptor_test.go
+++ b/internal/ingestion/component/knowledge_compiler/tree/raptor_test.go
@@ -177,11 +177,7 @@ func TestBuildTreeNoPanicWhenAllSummariesFail(t *testing.T) {
// TestDefaultRaptorPromptMatchesTreeYAML locks the default summary prompt to the
// production tree.yaml template. It must equal the Python tree compilation
-// template prompt (api/db/init_data/compilation_templates/tree.yaml), NOT the
-// compiler.py:128 fallback. Critically, the YAML literal block carries a base
-// indent of 6 spaces before {cluster_content}; those 6 spaces are part of the
-// prompt and must be preserved (Python does self._prompt.format(...), splicing
-// the cluster text after the 6-space indent).
+// template prompt.
func TestDefaultRaptorPromptMatchesTreeYAML(t *testing.T) {
want := "Please summarize the following paragraphs. Be careful with the numbers, do not make things up. Paragraphs as following:\n {cluster_content}\nThe above is the content you need to summarize."
if defaultRaptorPrompt != want {
diff --git a/api/db/init_data/compilation_templates/knowledge_graph.yaml b/internal/ingestion/knowledge_compile/templates/knowledge_graph.yaml
similarity index 100%
rename from api/db/init_data/compilation_templates/knowledge_graph.yaml
rename to internal/ingestion/knowledge_compile/templates/knowledge_graph.yaml
diff --git a/api/db/init_data/compilation_templates/mind_map.yaml b/internal/ingestion/knowledge_compile/templates/mind_map.yaml
similarity index 100%
rename from api/db/init_data/compilation_templates/mind_map.yaml
rename to internal/ingestion/knowledge_compile/templates/mind_map.yaml
diff --git a/api/db/init_data/compilation_templates/page_index.yaml b/internal/ingestion/knowledge_compile/templates/page_index.yaml
similarity index 100%
rename from api/db/init_data/compilation_templates/page_index.yaml
rename to internal/ingestion/knowledge_compile/templates/page_index.yaml
diff --git a/api/db/init_data/compilation_templates/session_essence.yaml b/internal/ingestion/knowledge_compile/templates/session_essence.yaml
similarity index 100%
rename from api/db/init_data/compilation_templates/session_essence.yaml
rename to internal/ingestion/knowledge_compile/templates/session_essence.yaml
diff --git a/api/db/init_data/compilation_templates/session_graph.yaml b/internal/ingestion/knowledge_compile/templates/session_graph.yaml
similarity index 100%
rename from api/db/init_data/compilation_templates/session_graph.yaml
rename to internal/ingestion/knowledge_compile/templates/session_graph.yaml
diff --git a/api/db/init_data/compilation_templates/timeline.yaml b/internal/ingestion/knowledge_compile/templates/timeline.yaml
similarity index 100%
rename from api/db/init_data/compilation_templates/timeline.yaml
rename to internal/ingestion/knowledge_compile/templates/timeline.yaml
diff --git a/api/db/init_data/compilation_templates/tree.yaml b/internal/ingestion/knowledge_compile/templates/tree.yaml
similarity index 100%
rename from api/db/init_data/compilation_templates/tree.yaml
rename to internal/ingestion/knowledge_compile/templates/tree.yaml
diff --git a/api/db/init_data/compilation_templates/wiki.yaml b/internal/ingestion/knowledge_compile/templates/wiki.yaml
similarity index 100%
rename from api/db/init_data/compilation_templates/wiki.yaml
rename to internal/ingestion/knowledge_compile/templates/wiki.yaml
diff --git a/api/db/init_data/compilation_templates/wiki/brand.yaml b/internal/ingestion/knowledge_compile/templates/wiki/brand.yaml
similarity index 100%
rename from api/db/init_data/compilation_templates/wiki/brand.yaml
rename to internal/ingestion/knowledge_compile/templates/wiki/brand.yaml
diff --git a/api/db/init_data/compilation_templates/wiki/engineering.yaml b/internal/ingestion/knowledge_compile/templates/wiki/engineering.yaml
similarity index 100%
rename from api/db/init_data/compilation_templates/wiki/engineering.yaml
rename to internal/ingestion/knowledge_compile/templates/wiki/engineering.yaml
diff --git a/api/db/init_data/compilation_templates/wiki/general.yaml b/internal/ingestion/knowledge_compile/templates/wiki/general.yaml
similarity index 100%
rename from api/db/init_data/compilation_templates/wiki/general.yaml
rename to internal/ingestion/knowledge_compile/templates/wiki/general.yaml
diff --git a/api/db/init_data/compilation_templates/wiki/market.yaml b/internal/ingestion/knowledge_compile/templates/wiki/market.yaml
similarity index 100%
rename from api/db/init_data/compilation_templates/wiki/market.yaml
rename to internal/ingestion/knowledge_compile/templates/wiki/market.yaml
diff --git a/api/db/init_data/compilation_templates/wiki/product.yaml b/internal/ingestion/knowledge_compile/templates/wiki/product.yaml
similarity index 100%
rename from api/db/init_data/compilation_templates/wiki/product.yaml
rename to internal/ingestion/knowledge_compile/templates/wiki/product.yaml
diff --git a/api/db/init_data/compilation_templates/wiki/user_interview.yaml b/internal/ingestion/knowledge_compile/templates/wiki/user_interview.yaml
similarity index 100%
rename from api/db/init_data/compilation_templates/wiki/user_interview.yaml
rename to internal/ingestion/knowledge_compile/templates/wiki/user_interview.yaml
diff --git a/internal/service/compilation_template_service.go b/internal/service/compilation_template_service.go
index 73decaab90..a65c842e86 100644
--- a/internal/service/compilation_template_service.go
+++ b/internal/service/compilation_template_service.go
@@ -127,10 +127,8 @@ func (s *CompilationTemplateService) ListBuiltins(ctx context.Context, tenantID
}
// LoadWikiPresets loads the wiki page-structure presets from the
-// init_data/compilation_templates/wiki/*.yaml files, filesystem-fresh per call.
func (s *CompilationTemplateService) LoadWikiPresets() ([]*WikiPreset, error) {
- wikiDir := filepath.Join(utility.GetProjectBaseDirectory(),
- "api", "db", "init_data", "compilation_templates", "wiki")
+ wikiDir := filepath.Join(utility.GetProjectBaseDirectory(), "internal", "ingestion", "knowledge_compile", "templates", "wiki")
entries, err := os.ReadDir(wikiDir)
if err != nil {
if os.IsNotExist(err) {
@@ -288,8 +286,7 @@ func (s *CompilationTemplateService) sortBuiltins(templates []*BuiltinTemplate)
}
func loadBuiltinTemplates() ([]*BuiltinTemplate, error) {
- dir := filepath.Join(utility.GetProjectBaseDirectory(),
- "api", "db", "init_data", "compilation_templates")
+ dir := filepath.Join(utility.GetProjectBaseDirectory(), "internal", "ingestion", "knowledge_compile", "templates")
entries, err := os.ReadDir(dir)
if err != nil {
if os.IsNotExist(err) {
diff --git a/internal/service/compilation_template_service_test.go b/internal/service/compilation_template_service_test.go
index feb88d8076..14db93cf6a 100644
--- a/internal/service/compilation_template_service_test.go
+++ b/internal/service/compilation_template_service_test.go
@@ -129,7 +129,7 @@ func TestLoadWikiPresets_FrontendContract(t *testing.T) {
t.Fatalf("LoadWikiPresets: %v", err)
}
if len(presets) == 0 {
- t.Fatal("expected wiki presets to load from api/db/init_data")
+ t.Fatal("expected wiki presets to load from internal/ingestion/knowledge_compile")
}
for _, preset := range presets {
if preset.Instruction == "" {
@@ -165,7 +165,7 @@ func TestLoadBuiltinTemplates_AllYAMLDefinitions(t *testing.T) {
}
t.Cleanup(func() { _ = os.Chdir(origWd) })
- dir := filepath.Join("api", "db", "init_data", "compilation_templates")
+ dir := filepath.Join("internal", "ingestion", "knowledge_compile", "templates")
entries, err := os.ReadDir(dir)
if err != nil {
t.Fatalf("read builtin template directory: %v", err)
@@ -193,7 +193,7 @@ func TestLoadBuiltinTemplates_AllYAMLDefinitions(t *testing.T) {
if template.Config["kind"] == nil {
t.Errorf("builtin template %q has no config.kind", template.ID)
}
- if _, err := common.KindToVariant(template.Kind); err != nil {
+ if _, err = common.KindToVariant(template.Kind); err != nil {
t.Errorf("builtin template %q kind %q is not supported: %v", template.ID, template.Kind, err)
}
// Tree intentionally carries empty entity/relation stubs for the shared
@@ -204,7 +204,7 @@ func TestLoadBuiltinTemplates_AllYAMLDefinitions(t *testing.T) {
"kind": template.Kind,
"config": map[string]interface{}(template.Config),
}
- if err := ValidateTemplatePayload(payload, true); err != nil {
+ if err = ValidateTemplatePayload(payload, true); err != nil {
t.Errorf("builtin template %q failed validation: %v", template.ID, err)
}
}