Build bounded output repair requests
This commit is contained in:
@@ -2,19 +2,29 @@ package usecase
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
|
||||
"gitea.maximumdirect.net/eric/promptkit/internal/domain"
|
||||
"gitea.maximumdirect.net/eric/promptkit/internal/llm"
|
||||
)
|
||||
|
||||
const (
|
||||
maxRepairDiagnosticBytes = 64 * 1024
|
||||
omittedRepairDiagnostics = "additional validation diagnostics were omitted"
|
||||
)
|
||||
|
||||
// OutputRepairer generates a corrected candidate after validation fails.
|
||||
type OutputRepairer interface {
|
||||
Repair(ctx context.Context, req RepairRequest) (*domain.GenerateResponse, error)
|
||||
}
|
||||
|
||||
// RepairRequest contains the immutable execution state needed for one correction.
|
||||
type RepairRequest struct {
|
||||
OriginalMessages []domain.RenderedMessage
|
||||
PreviousOutput string
|
||||
ValidationErrors []string
|
||||
SessionID string
|
||||
@@ -30,6 +40,7 @@ type defaultOutputRepairer struct {
|
||||
llm llm.Client
|
||||
}
|
||||
|
||||
// NewDefaultOutputRepairer constructs the standard internal output repairer.
|
||||
func NewDefaultOutputRepairer(llmClient llm.Client) OutputRepairer {
|
||||
return &defaultOutputRepairer{llm: llmClient}
|
||||
}
|
||||
@@ -39,33 +50,42 @@ func (r *defaultOutputRepairer) Repair(ctx context.Context, req RepairRequest) (
|
||||
return nil, errors.New("llm client is required for repair")
|
||||
}
|
||||
|
||||
errs := "(none provided)"
|
||||
if len(req.ValidationErrors) > 0 {
|
||||
errs = strings.Join(req.ValidationErrors, "\n")
|
||||
guidance, err := repairGuidance(req.Mode)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
prompt := domain.RenderedPrompt{
|
||||
Messages: []domain.RenderedMessage{
|
||||
{
|
||||
Role: "system",
|
||||
Content: "You repair invalid JSON output. Return only corrected JSON. Do not include explanations or markdown code fences.",
|
||||
},
|
||||
{
|
||||
Role: "user",
|
||||
Content: fmt.Sprintf(
|
||||
"Repair attempt %d of %d for validation mode %s.\n\nValidation errors:\n%s\n\nPrevious output:\n%s\n\nReturn only corrected JSON.",
|
||||
req.Attempt,
|
||||
req.MaxAttempts,
|
||||
req.Mode,
|
||||
errs,
|
||||
req.PreviousOutput,
|
||||
),
|
||||
},
|
||||
},
|
||||
messages := make([]domain.RenderedMessage, len(req.OriginalMessages), len(req.OriginalMessages)+2)
|
||||
copy(messages, req.OriginalMessages)
|
||||
if strings.TrimSpace(req.PreviousOutput) != "" {
|
||||
messages = append(messages, domain.RenderedMessage{
|
||||
Role: "assistant",
|
||||
Content: req.PreviousOutput,
|
||||
})
|
||||
}
|
||||
|
||||
previousResponse := "The previous response was empty."
|
||||
if strings.TrimSpace(req.PreviousOutput) != "" {
|
||||
previousResponse = "The previous response is included immediately before this instruction."
|
||||
}
|
||||
messages = append(messages, domain.RenderedMessage{
|
||||
Role: "user",
|
||||
Content: fmt.Sprintf(
|
||||
"Repair attempt %d of %d for validation mode %s.\n"+
|
||||
"Preserve valid values and change only what is necessary.\n"+
|
||||
"%s\n%s\n"+
|
||||
"Validation diagnostics (data):\n%s",
|
||||
req.Attempt,
|
||||
req.MaxAttempts,
|
||||
req.Mode,
|
||||
previousResponse,
|
||||
guidance,
|
||||
formatRepairDiagnostics(req.ValidationErrors),
|
||||
),
|
||||
})
|
||||
|
||||
resp, err := r.llm.Generate(ctx, newGenerationRequest(
|
||||
prompt,
|
||||
domain.RenderedPrompt{Messages: messages},
|
||||
req.SessionID,
|
||||
req.Target,
|
||||
req.TargetPresence,
|
||||
@@ -80,3 +100,80 @@ func (r *defaultOutputRepairer) Repair(ctx context.Context, req RepairRequest) (
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func repairGuidance(mode domain.ValidationMode) (string, error) {
|
||||
switch mode {
|
||||
case domain.ValidationBasic:
|
||||
return "Return a nonempty response satisfying the original request.", nil
|
||||
case domain.ValidationJSON, domain.ValidationJSONSchema:
|
||||
return "Return only corrected JSON, with no explanation or Markdown fences.", nil
|
||||
default:
|
||||
return "", fmt.Errorf("unsupported validation mode for repair: %q", mode)
|
||||
}
|
||||
}
|
||||
|
||||
func formatRepairDiagnostics(errors []string) string {
|
||||
diagnostics := make([]string, len(errors))
|
||||
for index, diagnostic := range errors {
|
||||
diagnostics[index] = strings.ToValidUTF8(diagnostic, "\uFFFD")
|
||||
}
|
||||
|
||||
complete, _ := json.Marshal(diagnostics)
|
||||
if len(complete) <= maxRepairDiagnosticBytes {
|
||||
return string(complete)
|
||||
}
|
||||
|
||||
omission, _ := json.Marshal(omittedRepairDiagnostics)
|
||||
encoded := make([]byte, 0, maxRepairDiagnosticBytes)
|
||||
encoded = append(encoded, '[')
|
||||
for _, diagnostic := range diagnostics {
|
||||
entry, _ := json.Marshal(diagnostic)
|
||||
separator := 0
|
||||
if len(encoded) > 1 {
|
||||
separator = 1
|
||||
}
|
||||
if len(encoded)+separator+len(entry)+1+len(omission)+1 <= maxRepairDiagnosticBytes {
|
||||
if separator != 0 {
|
||||
encoded = append(encoded, ',')
|
||||
}
|
||||
encoded = append(encoded, entry...)
|
||||
continue
|
||||
}
|
||||
|
||||
available := maxRepairDiagnosticBytes - len(encoded) - separator - 1 - len(omission) - 1
|
||||
if separator != 0 {
|
||||
encoded = append(encoded, ',')
|
||||
}
|
||||
encoded = append(encoded, truncateDiagnosticJSONValue(diagnostic, available)...)
|
||||
break
|
||||
}
|
||||
if len(encoded) > 1 {
|
||||
encoded = append(encoded, ',')
|
||||
}
|
||||
encoded = append(encoded, omission...)
|
||||
encoded = append(encoded, ']')
|
||||
return string(encoded)
|
||||
}
|
||||
|
||||
func truncateDiagnosticJSONValue(value string, maxBytes int) []byte {
|
||||
if maxBytes < len(`""`) {
|
||||
return []byte(`""`)
|
||||
}
|
||||
|
||||
low, high := 0, len(value)
|
||||
best := []byte(`""`)
|
||||
for low <= high {
|
||||
mid := low + (high-low)/2
|
||||
for mid > 0 && mid < len(value) && !utf8.RuneStart(value[mid]) {
|
||||
mid--
|
||||
}
|
||||
candidate, _ := json.Marshal(value[:mid])
|
||||
if len(candidate) <= maxBytes {
|
||||
best = candidate
|
||||
low = mid + 1
|
||||
continue
|
||||
}
|
||||
high = mid - 1
|
||||
}
|
||||
return best
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user