Move chunk planning into core operation execution
This commit is contained in:
@@ -37,15 +37,10 @@ var _ contracts.Chunker = (*Chunker)(nil)
|
||||
|
||||
type Options struct{}
|
||||
|
||||
type Chunker struct {
|
||||
engine *promptkit.Engine
|
||||
}
|
||||
type Chunker struct{}
|
||||
|
||||
func New(engine *promptkit.Engine, _ Options) (*Chunker, error) {
|
||||
if engine == nil {
|
||||
return nil, chunkerErrorf("native engine must not be nil")
|
||||
}
|
||||
return &Chunker{engine: engine}, nil
|
||||
func New(_ Options) (*Chunker, error) {
|
||||
return &Chunker{}, nil
|
||||
}
|
||||
|
||||
func (c *Chunker) Key() string {
|
||||
@@ -56,27 +51,24 @@ func (c *Chunker) ReferenceSlots() []contracts.ReferenceSlot {
|
||||
return shared.ReferenceSlots(referenceSlotDescriptions)
|
||||
}
|
||||
|
||||
func (c *Chunker) Plan(ctx context.Context, req contracts.ChunkRequest) (contracts.ChunkPlanResult, error) {
|
||||
func (c *Chunker) PrepareChunk(ctx context.Context, req contracts.ChunkInput) (contracts.PreparedOperation[source.ChunkPlan], error) {
|
||||
if c == nil {
|
||||
return contracts.ChunkPlanResult{}, chunkerErrorf("chunker must not be nil")
|
||||
}
|
||||
if c.engine == nil {
|
||||
return contracts.ChunkPlanResult{}, chunkerErrorf("native engine must not be nil")
|
||||
return nil, chunkerErrorf("chunker must not be nil")
|
||||
}
|
||||
if ctx == nil {
|
||||
return contracts.ChunkPlanResult{}, chunkerErrorf("context must not be nil")
|
||||
return nil, chunkerErrorf("context must not be nil")
|
||||
}
|
||||
if err := ctx.Err(); err != nil {
|
||||
return contracts.ChunkPlanResult{}, chunkerErrorf("context error before chunking: %w", err)
|
||||
return nil, chunkerErrorf("context error before chunking: %w", err)
|
||||
}
|
||||
if req.Source == nil {
|
||||
return contracts.ChunkPlanResult{}, chunkerErrorf("source must not be nil")
|
||||
return nil, chunkerErrorf("source must not be nil")
|
||||
}
|
||||
if len(req.Source.Units) == 0 {
|
||||
return contracts.ChunkPlanResult{}, chunkerErrorf("source units must not be empty")
|
||||
return nil, chunkerErrorf("source units must not be empty")
|
||||
}
|
||||
if err := source.ValidateDocument(req.Source); err != nil {
|
||||
return contracts.ChunkPlanResult{}, chunkerErrorf("validate source document: %w", err)
|
||||
return nil, chunkerErrorf("validate source document: %w", err)
|
||||
}
|
||||
materials := shared.PromptInputs(req.SourceInput, req.References)
|
||||
inputs := make(map[string]promptkit.ArtifactRef, len(materials))
|
||||
@@ -84,23 +76,20 @@ func (c *Chunker) Plan(ctx context.Context, req contracts.ChunkRequest) (contrac
|
||||
inputs[name] = promptkit.InlineWithURI(material.OriginURI, string(material.Content))
|
||||
}
|
||||
initial := promptkit.RunRequest{PromptID: PromptID, ProfileID: req.LLMProfile, SessionID: req.SessionID, Inputs: inputs}
|
||||
plan, outcome, err := pipeline.RunProducer(ctx, c.engine, req.Operation, initial,
|
||||
func(_ context.Context, result *promptkit.RunResult) (*source.ChunkPlan, []string, error) {
|
||||
return contracts.NativeOperation[source.ChunkPlan]{Initial: initial,
|
||||
Interpret: func(_ context.Context, result *promptkit.RunResult) (contracts.Evaluation[source.ChunkPlan], error) {
|
||||
var response chunkResponse
|
||||
if err := json.Unmarshal([]byte(result.RawOutput), &response); err != nil {
|
||||
return nil, nil, chunkerErrorf("decode native output: %w", err)
|
||||
return nil, chunkerErrorf("decode native output: %w", err)
|
||||
}
|
||||
plan, err := planFromResponse(req.Source, response)
|
||||
if err != nil {
|
||||
return nil, []string{fmt.Sprintf("Revise the scene boundaries: %v. Use source-unit IDs from the supplied transcript and cover every unit in document order, without gaps or overlaps.", err)}, nil
|
||||
return contracts.CandidateEvaluation[source.ChunkPlan]{Result: contracts.EvaluationView[source.ChunkPlan]{Guidance: []string{fmt.Sprintf("Revise the scene boundaries: %v. Use source-unit IDs from the supplied transcript and cover every unit in document order, without gaps or overlaps.", err)}}}, nil
|
||||
}
|
||||
return &plan, nil, nil
|
||||
return contracts.CandidateEvaluation[source.ChunkPlan]{Result: contracts.EvaluationView[source.ChunkPlan]{Candidate: &plan}}, nil
|
||||
},
|
||||
func(_ context.Context, guidance []string) (promptkit.RunRequest, error) {
|
||||
return promptkit.RunRequest{PromptID: CorrectionPromptID, ProfileID: req.LLMProfile, SessionID: req.SessionID,
|
||||
Inputs: map[string]promptkit.ArtifactRef{"guidance": promptkit.Inline(strings.Join(guidance, "\n\n"))}}, nil
|
||||
},
|
||||
func(context.Context, *source.ChunkPlan) (source.ChunkPlan, error) {
|
||||
Correction: contracts.PromptCorrection(CorrectionPromptID, "guidance", func(guidance []string) promptkit.ArtifactRef { return promptkit.Inline(strings.Join(guidance, "\n\n")) }),
|
||||
Fallback: func(context.Context, contracts.Evaluation[source.ChunkPlan]) (source.ChunkPlan, error) {
|
||||
plan := source.ChunkPlan{SourceDigest: req.Source.Digest, Ranges: []source.ChunkRange{{
|
||||
StartUnitID: req.Source.Units[0].ID, EndUnitID: req.Source.Units[len(req.Source.Units)-1].ID,
|
||||
}}}
|
||||
@@ -108,11 +97,7 @@ func (c *Chunker) Plan(ctx context.Context, req contracts.ChunkRequest) (contrac
|
||||
return source.ChunkPlan{}, chunkerErrorf("materialize whole-source fallback: %w", err)
|
||||
}
|
||||
return plan, nil
|
||||
})
|
||||
if err != nil {
|
||||
return contracts.ChunkPlanResult{}, chunkerErrorf("produce scene plan: %w", err)
|
||||
}
|
||||
return contracts.ChunkPlanResult{Plan: plan, Outcome: outcome}, nil
|
||||
}}, nil
|
||||
}
|
||||
|
||||
func ModuleSpec() pipeline.ModuleSpec {
|
||||
@@ -133,7 +118,7 @@ func Register(registry *pipeline.ChunkerRegistry) error {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return New(request.Dependencies.Engine, options)
|
||||
return New(options)
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user