Bind schema selections throughout execution
This commit is contained in:
@@ -18,6 +18,7 @@ import (
|
||||
"go.maximumdirect.net/promptkit/internal/capacity"
|
||||
"go.maximumdirect.net/promptkit/internal/defaults"
|
||||
"go.maximumdirect.net/promptkit/internal/domain"
|
||||
"go.maximumdirect.net/promptkit/internal/filecatalog"
|
||||
"go.maximumdirect.net/promptkit/internal/generation"
|
||||
"go.maximumdirect.net/promptkit/internal/jsonvalue"
|
||||
"go.maximumdirect.net/promptkit/internal/llm"
|
||||
@@ -73,6 +74,7 @@ type preparationState struct {
|
||||
effectiveModel domain.ExecutionTarget
|
||||
targetPresence domain.ExecutionTargetPresence
|
||||
effectiveContract domain.OutputContract
|
||||
validationInput validate.Input
|
||||
start time.Time
|
||||
transientRetries uint64
|
||||
}
|
||||
@@ -353,8 +355,15 @@ func (r *Runner) resolvePreparation(
|
||||
promptDefinitionHash := promptSelection.hash
|
||||
effectiveContract, err := resolveOutputContract(ctx, def, req.Validation)
|
||||
if err != nil {
|
||||
if contextErr := ctx.Err(); contextErr != nil {
|
||||
return nil, contextErr
|
||||
}
|
||||
return nil, fmt.Errorf("%w: output contract: %v", ErrInvalidRequest, err)
|
||||
}
|
||||
validationInput, err := bindValidationInput(def, effectiveContract, req.Validation != nil)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%w: prompt %q: %v", ErrInvalidRequest, def.ID, err)
|
||||
}
|
||||
|
||||
selectedProfileID := req.ProfileID
|
||||
if strings.TrimSpace(selectedProfileID) == "" {
|
||||
@@ -394,6 +403,7 @@ func (r *Runner) resolvePreparation(
|
||||
effectiveModel: effectiveModel,
|
||||
targetPresence: targetPresence,
|
||||
effectiveContract: effectiveContract,
|
||||
validationInput: validationInput,
|
||||
start: start,
|
||||
transientRetries: transientRetries,
|
||||
}, nil
|
||||
@@ -404,7 +414,7 @@ func (r *Runner) completePreparation(
|
||||
req domain.RunRequest,
|
||||
state *preparationState,
|
||||
) (*preparedOperation, error) {
|
||||
validationPlan, err := r.prepareValidation(ctx, state.effectiveContract)
|
||||
validationPlan, err := r.prepareValidation(ctx, state.definition.ID, state.validationInput)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -425,14 +435,15 @@ func (r *Runner) completePreparation(
|
||||
|
||||
func (r *Runner) prepareValidation(
|
||||
ctx context.Context,
|
||||
contract domain.OutputContract,
|
||||
promptID string,
|
||||
input validate.Input,
|
||||
) (validate.PreparedValidation, error) {
|
||||
if r.validator == nil {
|
||||
return noOpPreparedValidation{contract: contract}, nil
|
||||
return noOpPreparedValidation{contract: input.Contract}, nil
|
||||
}
|
||||
plan, err := r.validator.PrepareValidation(ctx, contract)
|
||||
plan, err := r.validator.PrepareValidation(ctx, input)
|
||||
if err != nil {
|
||||
return nil, &OperationError{Category: ErrValidation, Cause: err}
|
||||
return nil, &OperationError{Category: ErrValidation, Cause: fmt.Errorf("prompt %q: %w", promptID, err)}
|
||||
}
|
||||
if plan == nil {
|
||||
return nil, fmt.Errorf("%w: validator returned nil prepared validation", ErrValidation)
|
||||
@@ -440,6 +451,31 @@ func (r *Runner) prepareValidation(
|
||||
return plan, nil
|
||||
}
|
||||
|
||||
func bindValidationInput(def *domain.PromptDefinition, contract domain.OutputContract, replaced bool) (validate.Input, error) {
|
||||
input := validate.Input{Contract: contract}
|
||||
if contract.ValidationMode != domain.ValidationJSONSchema {
|
||||
return input, nil
|
||||
}
|
||||
if contract.SchemaFile != nil && !replaced {
|
||||
if def.SchemaFile == nil {
|
||||
return validate.Input{}, fmt.Errorf("schema_file %q has no resource binding", *contract.SchemaFile)
|
||||
}
|
||||
input.File = def.SchemaFile
|
||||
return input, nil
|
||||
}
|
||||
if contract.SchemaPath != "" {
|
||||
if def.SchemaRoot == nil {
|
||||
return validate.Input{}, errors.New("schema_path requires an applicable schema_root")
|
||||
}
|
||||
resolved, _, err := filecatalog.ResolveFSPath(def.SchemaRoot.Path, def.SchemaRoot.Path, contract.SchemaPath)
|
||||
if err != nil {
|
||||
return validate.Input{}, fmt.Errorf("invalid schema_path %q: %w", contract.SchemaPath, err)
|
||||
}
|
||||
input.File = &domain.ResourceBinding{FS: def.SchemaRoot.FS, Path: resolved}
|
||||
}
|
||||
return input, nil
|
||||
}
|
||||
|
||||
func (r *Runner) structuredOutputFromValidationPlan(
|
||||
def *domain.PromptDefinition,
|
||||
contract domain.OutputContract,
|
||||
|
||||
Reference in New Issue
Block a user