Collect optional validation context only when it can fit
This commit is contained in:
@@ -3,6 +3,7 @@ package validators
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"slices"
|
||||
|
||||
"gitea.maximumdirect.net/eric/audita/v3/internal/core/chunking"
|
||||
)
|
||||
@@ -47,21 +48,31 @@ type fittedValidationItem struct {
|
||||
|
||||
// fitValidationContext tries the required payload followed by glossary entries
|
||||
// and then neighboring segments. The first excess ends the usable prefix.
|
||||
func fitValidationContext(item LLMValidationItem, budget int, estimator chunking.TokenEstimator) (fittedValidationItem, error) {
|
||||
func fitValidationContext(item LLMValidationItem, budget int, estimator chunking.TokenEstimator, context optionalValidationContext) (result fittedValidationItem, err error) {
|
||||
fitted := fittedValidationItem{item: item}
|
||||
fitted.item.GlossaryEntries = item.GlossaryEntries[:0]
|
||||
fitted.item.NeighboringSegments = item.NeighboringSegments[:0]
|
||||
fitted.item.GlossaryEntries = nil
|
||||
fitted.item.NeighboringSegments = nil
|
||||
simple, _ := estimator.(*chunking.SimpleTokenEstimator)
|
||||
var err error
|
||||
if simple != nil {
|
||||
fitted.cost, err = measureValidationItem(simple, fitted.item)
|
||||
fitted.tokens = fitted.cost.arrayTokens()
|
||||
} else {
|
||||
fitted.tokens, err = estimateBatchTokens(estimator, []LLMValidationItem{fitted.item})
|
||||
}
|
||||
if err != nil || fitted.tokens > budget {
|
||||
if err != nil || fitted.tokens > budget || (simple != nil && fitted.tokens == budget) {
|
||||
return fitted, err
|
||||
}
|
||||
if context != nil {
|
||||
item.NeighboringSegments, item.GlossaryEntries = context(item)
|
||||
}
|
||||
defer func() {
|
||||
if len(result.item.GlossaryEntries) < len(item.GlossaryEntries) {
|
||||
result.item.GlossaryEntries = slices.Clone(result.item.GlossaryEntries)
|
||||
}
|
||||
if len(result.item.NeighboringSegments) < len(item.NeighboringSegments) {
|
||||
result.item.NeighboringSegments = slices.Clone(result.item.NeighboringSegments)
|
||||
}
|
||||
}()
|
||||
try := func(candidate LLMValidationItem, field string, first bool, entry any) (bool, error) {
|
||||
cost := fitted.cost
|
||||
var tokens int
|
||||
|
||||
Reference in New Issue
Block a user