Reuse accepted results in core operations
This commit is contained in:
@@ -82,7 +82,47 @@ func executeNative[T any](ctx context.Context, execution ExecutionContext[T], op
|
||||
|
||||
request := producerRequest(operation.Initial, execution.Execution)
|
||||
request.CaptureExchange = request.CaptureExchange || (operation.Correction != nil && execution.Controls.MaxAdditionalTurns > 0)
|
||||
result, err := execution.Engine.Run(ctx, request)
|
||||
var result *promptkit.RunResult
|
||||
var err error
|
||||
var cacheKey string
|
||||
cache := execution.Cache
|
||||
if cache != nil && cache.Store != nil && execution.ReuseEligible && execution.Controls.Cache != contracts.CacheBypass {
|
||||
if cache.Encode == nil || cache.Decode == nil {
|
||||
return empty, errors.New("producer cache codec must be supplied")
|
||||
}
|
||||
if mode := execution.Controls.Cache; mode != "" && mode != contracts.CacheAuto && mode != contracts.CacheRefresh {
|
||||
return empty, fmt.Errorf("%w: unknown cache mode %q", promptkit.ErrInvalidRequest, mode)
|
||||
}
|
||||
var prepared *promptkit.PreparedExecution
|
||||
prepared, err = execution.Engine.PrepareExecution(ctx, request)
|
||||
if err == nil {
|
||||
defer prepared.Discard()
|
||||
details := prepared.Details()
|
||||
cacheKey = details.MessageContentHash
|
||||
if err := observeProducerCache(ctx, cache, contracts.CacheObservation{Decision: "prepared", MessageContentHash: cacheKey, Prepared: &details}); err != nil {
|
||||
return empty, err
|
||||
}
|
||||
reused, lookupErr := lookupProducerCache(ctx, cache, execution.Controls.Cache, cacheKey)
|
||||
if lookupErr != nil {
|
||||
return empty, lookupErr
|
||||
}
|
||||
if reused != nil {
|
||||
return OperationResult[T]{Value: *reused, Outcome: contracts.ProducerOutcome{Disposition: contracts.ProducerAccepted, ReuseEligible: true}}, nil
|
||||
}
|
||||
result, err = execution.Engine.RunPrepared(ctx, prepared)
|
||||
}
|
||||
} else {
|
||||
if cache != nil {
|
||||
decision := "bypass"
|
||||
if !execution.ReuseEligible {
|
||||
decision = "ineligible"
|
||||
}
|
||||
if err := observeProducerCache(ctx, cache, contracts.CacheObservation{Decision: decision}); err != nil {
|
||||
return empty, err
|
||||
}
|
||||
}
|
||||
result, err = execution.Engine.Run(ctx, request)
|
||||
}
|
||||
if canceled := ctx.Err(); canceled != nil {
|
||||
return empty, canceled
|
||||
}
|
||||
@@ -150,6 +190,11 @@ func executeNative[T any](ctx context.Context, execution ExecutionContext[T], op
|
||||
}
|
||||
}
|
||||
if current.accepted {
|
||||
if cacheKey != "" {
|
||||
if err := storeProducerCache(ctx, cache, execution.Controls.Cache, cacheKey, *current.evaluation.View().Candidate); err != nil {
|
||||
return empty, err
|
||||
}
|
||||
}
|
||||
outcome := contracts.ProducerOutcome{Disposition: contracts.ProducerAccepted, ReuseEligible: execution.ReuseEligible}
|
||||
return completeOperation(*current.evaluation.View().Candidate, current.evaluation, current.assessment, outcome, operation.Diagnostics)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user