mirror of
https://github.com/infiniflow/ragflow.git
synced 2026-07-22 16:36:47 +08:00
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:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user