mirror of
https://github.com/infiniflow/ragflow.git
synced 2026-08-11 18:01:26 +08:00
Revert "feat: Go knowledge compiler with scheduler-driven dataset compilation" (#17897)
Reverts infiniflow/ragflow#17881
This commit is contained in:
@@ -43,6 +43,7 @@ const (
|
||||
CodeLicenseTimeRollback ErrorCode = 324
|
||||
CodeLicenseNotFound ErrorCode = 325
|
||||
CodeLicenseUnexpectedError ErrorCode = 326
|
||||
CodeLicenseNotValidYet ErrorCode = 327
|
||||
CodeBadRequest ErrorCode = 400
|
||||
CodeUnauthorized ErrorCode = 401
|
||||
CodeForbidden ErrorCode = 403
|
||||
|
||||
@@ -1,5 +1,129 @@
|
||||
package common
|
||||
|
||||
import "strings"
|
||||
|
||||
// InjectExtractorLLMID finds all Extractor component entries (keys prefixed
|
||||
// with "extractor:" or "extractor_") in parserConfig and sets their llm_id
|
||||
// to the given value. Returns whether any entry was updated.
|
||||
func InjectExtractorLLMID(parserConfig map[string]interface{}, llmID string) bool {
|
||||
if parserConfig == nil || llmID == "" {
|
||||
return false
|
||||
}
|
||||
updated := false
|
||||
for cid, raw := range parserConfig {
|
||||
compMap, ok := raw.(map[string]interface{})
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
cidLower := strings.ToLower(cid)
|
||||
if strings.HasPrefix(cidLower, "extractor:") || strings.HasPrefix(cidLower, "extractor_") {
|
||||
if current, ok := compMap["llm_id"].(string); !ok || current == "" {
|
||||
compMap["llm_id"] = llmID
|
||||
updated = true
|
||||
}
|
||||
}
|
||||
}
|
||||
return updated
|
||||
}
|
||||
|
||||
// InjectExtractorEnableMetadata enables auto-metadata (enable_metadata) extraction
|
||||
// on every Extractor node when the dataset has enable_metadata on and a
|
||||
// non-empty field set (metadata and/or built_in_metadata). The dataset-level
|
||||
// enable_metadata flag is authoritative (mirrors Python task_executor.py:519,
|
||||
// which reads parser_config directly and never consults a per-node flag): a
|
||||
// shipped DSL that defaults enable_metadata to 0 is still turned on. Only a
|
||||
// node the user already turned ON (enable_metadata truthy) is left untouched,
|
||||
// so an explicit per-node enablement keeps its own config. The field schema is
|
||||
// taken from parserConfig["metadata"] and parserConfig["built_in_metadata"]
|
||||
// (combined). Returns whether any entry was updated.
|
||||
func InjectExtractorEnableMetadata(parserConfig map[string]interface{}) bool {
|
||||
if parserConfig == nil {
|
||||
return false
|
||||
}
|
||||
if !isTruthy(parserConfig["enable_metadata"]) {
|
||||
return false
|
||||
}
|
||||
fields := metadataFieldDefs(parserConfig)
|
||||
if len(fields) == 0 {
|
||||
return false
|
||||
}
|
||||
updated := false
|
||||
for cid, raw := range parserConfig {
|
||||
compMap, ok := raw.(map[string]interface{})
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
cidLower := strings.ToLower(cid)
|
||||
if !strings.HasPrefix(cidLower, "extractor:") && !strings.HasPrefix(cidLower, "extractor_") {
|
||||
continue
|
||||
}
|
||||
// The dataset-level enable_metadata flag is authoritative (mirrors
|
||||
// Python task_executor.py:519, which reads parser_config directly and
|
||||
// never consults a per-node flag). Only a node the user already turned
|
||||
// ON (truthy) is left alone; a shipped DSL that defaults the field to
|
||||
// 0 must still be enabled by the dataset flag, otherwise auto-metadata
|
||||
// could never turn on for any of the built-in pipelines.
|
||||
if isTruthy(compMap["enable_metadata"]) {
|
||||
continue
|
||||
}
|
||||
compMap["enable_metadata"] = 1
|
||||
compMap["metadata"] = fields
|
||||
updated = true
|
||||
}
|
||||
return updated
|
||||
}
|
||||
|
||||
// metadataFieldDefs combines parserConfig["metadata"] and
|
||||
// parserConfig["built_in_metadata"] into the field list injected as
|
||||
// metadata (each entry keeps key/type/description/enum, mirroring the
|
||||
// stored shape from dataset/helpers.go normalizeMetadataConfigFields).
|
||||
//
|
||||
// It returns []any (i.e. []interface{}) rather than []map[string]interface{}
|
||||
// because the injected value is handed to NewExtractorComponent, which reads
|
||||
// params["metadata"].([]any). A []map[string]interface{} value would fail that
|
||||
// type assertion (Go slice types are not covariant) and the field schema would
|
||||
// be silently dropped, so auto-metadata never reached ExtractorParam.Metadata.
|
||||
func metadataFieldDefs(parserConfig map[string]interface{}) []any {
|
||||
var out []any
|
||||
for _, key := range []string{"metadata", "built_in_metadata"} {
|
||||
raw, ok := parserConfig[key].([]interface{})
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
for _, item := range raw {
|
||||
m, ok := item.(map[string]interface{})
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
k, _ := m["key"].(string)
|
||||
if strings.TrimSpace(k) == "" {
|
||||
continue
|
||||
}
|
||||
out = append(out, m)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// isTruthy reports whether a parserConfig flag (e.g. enable_metadata) is on.
|
||||
// It tolerates bool, numeric >0 and the strings "true"/"1" so storage
|
||||
// representation differences don't silently disable the feature.
|
||||
func isTruthy(v interface{}) bool {
|
||||
switch t := v.(type) {
|
||||
case bool:
|
||||
return t
|
||||
case string:
|
||||
return t == "true" || t == "1" || t == "True" || t == "TRUE"
|
||||
case float64:
|
||||
return t > 0
|
||||
case int:
|
||||
return t > 0
|
||||
case int64:
|
||||
return t > 0
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// deepCopyMap duplicates a JSON-like map so later merges do not mutate shared defaults.
|
||||
func deepCopyMap(source map[string]interface{}) map[string]interface{} {
|
||||
if source == nil {
|
||||
|
||||
135
internal/common/parser_config_test.go
Normal file
135
internal/common/parser_config_test.go
Normal file
@@ -0,0 +1,135 @@
|
||||
package common
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestInjectExtractorLLMID_SkipWhenUUID(t *testing.T) {
|
||||
uuid := "9e819c2442b14f9dab46062916e29195"
|
||||
pc := map[string]interface{}{
|
||||
"Extractor:A": map[string]interface{}{
|
||||
"llm_id": uuid,
|
||||
},
|
||||
}
|
||||
InjectExtractorLLMID(pc, "Qwen/Qwen3-8B@siliconflow")
|
||||
id := pc["Extractor:A"].(map[string]interface{})["llm_id"].(string)
|
||||
if id != uuid {
|
||||
t.Fatalf("expected UUID preserved, got %q", id)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInjectExtractorLLMID_SkipWhenComposite(t *testing.T) {
|
||||
composite := "Qwen/Qwen3-8B@siliconflow"
|
||||
pc := map[string]interface{}{
|
||||
"Extractor:B": map[string]interface{}{
|
||||
"llm_id": composite,
|
||||
},
|
||||
}
|
||||
InjectExtractorLLMID(pc, "DeepSeek@siliconflow")
|
||||
id := pc["Extractor:B"].(map[string]interface{})["llm_id"].(string)
|
||||
if id != composite {
|
||||
t.Fatalf("expected composite preserved, got %q", id)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInjectExtractorLLMID_InjectWhenEmpty(t *testing.T) {
|
||||
defaultLLM := "Qwen/Qwen3-8B@siliconflow"
|
||||
pc := map[string]interface{}{
|
||||
"Extractor:C": map[string]interface{}{},
|
||||
}
|
||||
InjectExtractorLLMID(pc, defaultLLM)
|
||||
id := pc["Extractor:C"].(map[string]interface{})["llm_id"].(string)
|
||||
if id != defaultLLM {
|
||||
t.Fatalf("expected %q injected, got %q", defaultLLM, id)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInjectExtractorLLMID_NoExtractor(t *testing.T) {
|
||||
pc := map[string]interface{}{
|
||||
"Parser:X": map[string]interface{}{"llm_id": ""},
|
||||
}
|
||||
InjectExtractorLLMID(pc, "default@provider")
|
||||
if _, ok := pc["Parser:X"]; !ok {
|
||||
t.Fatal("expected Parser:X still present")
|
||||
}
|
||||
}
|
||||
|
||||
func TestInjectExtractorEnableMetadata_Disabled(t *testing.T) {
|
||||
pc := map[string]interface{}{
|
||||
"Extractor:A": map[string]interface{}{},
|
||||
}
|
||||
if InjectExtractorEnableMetadata(pc) {
|
||||
t.Fatal("expected no update when enable_metadata is off")
|
||||
}
|
||||
if _, ok := pc["Extractor:A"].(map[string]interface{})["enable_metadata"]; ok {
|
||||
t.Fatal("enable_metadata should not be set")
|
||||
}
|
||||
}
|
||||
|
||||
func TestInjectExtractorEnableMetadata_InjectsAndMerges(t *testing.T) {
|
||||
pc := map[string]interface{}{
|
||||
"enable_metadata": true,
|
||||
"metadata": []interface{}{
|
||||
map[string]interface{}{"key": "author", "type": "string", "description": "doc author", "enum": []interface{}{"Alice", "Bob"}},
|
||||
},
|
||||
"built_in_metadata": []interface{}{
|
||||
map[string]interface{}{"key": "year", "type": "number"},
|
||||
},
|
||||
"Extractor:A": map[string]interface{}{},
|
||||
"Parser:X": map[string]interface{}{},
|
||||
}
|
||||
if !InjectExtractorEnableMetadata(pc) {
|
||||
t.Fatal("expected update")
|
||||
}
|
||||
ext := pc["Extractor:A"].(map[string]interface{})
|
||||
if got, _ := ext["enable_metadata"].(int); got != 1 {
|
||||
t.Fatalf("expected enable_metadata=1, got %v", ext["enable_metadata"])
|
||||
}
|
||||
fields, ok := ext["metadata"].([]any)
|
||||
if !ok || len(fields) != 2 {
|
||||
t.Fatalf("expected 2 merged metadata, got %#v", ext["metadata"])
|
||||
}
|
||||
if _, ok := pc["Parser:X"].(map[string]interface{})["enable_metadata"]; ok {
|
||||
t.Fatal("Parser node must not be touched")
|
||||
}
|
||||
}
|
||||
|
||||
func TestInjectExtractorEnableMetadata_RespectsExplicitEnabled(t *testing.T) {
|
||||
// A node the user already turned ON keeps its own config (no clobber).
|
||||
pc := map[string]interface{}{
|
||||
"enable_metadata": true,
|
||||
"metadata": []interface{}{map[string]interface{}{"key": "author"}},
|
||||
"Extractor:A": map[string]interface{}{"enable_metadata": 1},
|
||||
}
|
||||
if InjectExtractorEnableMetadata(pc) {
|
||||
t.Fatal("expected no update when user explicitly enabled enable_metadata")
|
||||
}
|
||||
}
|
||||
|
||||
func TestInjectExtractorEnableMetadata_OverridesDefaultZero(t *testing.T) {
|
||||
// Shipped DSLs default enable_metadata to 0; the dataset flag must still
|
||||
// turn them on (otherwise auto-metadata could never activate).
|
||||
pc := map[string]interface{}{
|
||||
"enable_metadata": true,
|
||||
"metadata": []interface{}{map[string]interface{}{"key": "author"}},
|
||||
"Extractor:A": map[string]interface{}{"enable_metadata": 0},
|
||||
}
|
||||
if !InjectExtractorEnableMetadata(pc) {
|
||||
t.Fatal("expected update: dataset flag must override default enable_metadata=0")
|
||||
}
|
||||
ext := pc["Extractor:A"].(map[string]interface{})
|
||||
if got, _ := ext["enable_metadata"].(int); got != 1 {
|
||||
t.Fatalf("expected enable_metadata=1 after override, got %v", ext["enable_metadata"])
|
||||
}
|
||||
if _, ok := ext["metadata"].([]any); !ok {
|
||||
t.Fatalf("expected metadata field schema injected, got %#v", ext["metadata"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestInjectExtractorEnableMetadata_NoFields(t *testing.T) {
|
||||
pc := map[string]interface{}{
|
||||
"enable_metadata": true,
|
||||
"Extractor:A": map[string]interface{}{},
|
||||
}
|
||||
if InjectExtractorEnableMetadata(pc) {
|
||||
t.Fatal("expected no update when no fields configured")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user