Go: parse ingestion DSL (#15938)

PR #15938

### Type of change

- [x] New Feature (non-breaking change which adds functionality)

---------

Signed-off-by: Jin Hai <haijin.chn@gmail.com>
This commit is contained in:
Jin Hai
2026-06-12 17:58:36 +08:00
committed by GitHub
parent 89aac82663
commit 115b730d07
11 changed files with 2103 additions and 7 deletions

View File

@@ -28,6 +28,7 @@ import (
"os"
"os/exec"
"path/filepath"
"ragflow/internal/ingestion"
"strings"
"time"
)
@@ -3445,27 +3446,60 @@ func (c *CLI) ChunkCommand(cmd *Command) (ResponseIf, error) {
return nil, fmt.Errorf("this command is only allowed in USER mode")
}
var result ExplainResponse
start := time.Now()
filename, ok := cmd.Params["filename"].(string)
if !ok {
return nil, fmt.Errorf("filename not provided")
}
dsl, ok := cmd.Params["dsl"].(string)
dslFilename, ok := cmd.Params["dsl"].(string)
if !ok {
return nil, fmt.Errorf("dsl not provided")
}
dsl, err := os.ReadFile(dslFilename)
if err != nil {
return nil, fmt.Errorf("failed to read dsl file: %w", err)
}
explain, ok := cmd.Params["explain"].(bool)
if !ok {
explain = false
}
if explain {
fmt.Printf("Explain chunk file: %s, DSL: %s\n", filename, dsl)
} else {
// TODO: not implemented
fmt.Printf("Chunk file: %s, DSL: %s\n", filename, dsl)
engine := ingestion.NewChunkEngine()
plan, err := engine.Compile(string(dsl))
if err != nil {
return nil, fmt.Errorf("compile failed: %w", err)
}
var result SimpleResponse
if explain {
explanation, err := engine.Explain(plan)
if err != nil {
return nil, fmt.Errorf("explain error: %w", err)
}
result.Message = explanation
} else {
fileToChunking, err := os.ReadFile(filename)
if err != nil {
return nil, fmt.Errorf("failed to read file: %w", err)
}
chunkContext, err := engine.Execute(plan, string(fileToChunking))
if err != nil {
return nil, fmt.Errorf("chunking error: %w", err)
}
for _, resultChunk := range chunkContext.ResultChunks {
fmt.Printf("Chunk index: %d\n", resultChunk.Index)
fmt.Printf("Chunk size: %d\n", resultChunk.Size)
fmt.Printf("Chunk content: \n%s\n", resultChunk.Content)
}
}
result.Duration = time.Since(start).Seconds()
result.Code = 0
result.Message = fmt.Sprintf("Success to chunk %s", filename)
return &result, nil