fixes
This commit is contained in:
1 parent
c347d2418f
commit
1ad180a052
49 files changed
+6339
-89
No files matched your search
@@ -3,7 +3,6 @@ package processing
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"sort"
|
||||
@@ -13,12 +12,6 @@ import (
|
||||
"github.com/descrybe/descrybe-v2/apps/api/internal/aiaudit"
|
||||
)
|
||||
|
||||
// ErrCategoryNotFound fails a product whose category could not be determined:
|
||||
// the model's pick was outside the taxonomy, or it scored below the confidence
|
||||
// floor. Processing stops for that product instead of enhancing it under a
|
||||
// category nobody believes in.
|
||||
var ErrCategoryNotFound = errors.New("product category could not be determined")
|
||||
|
||||
// MaxCategorizeOptions caps the taxonomy list injected into the categorize LLM
|
||||
// prompt (token/cost bound). Prefer stable sort by display name then unique_id.
|
||||
const MaxCategorizeOptions = 200
|
||||
@@ -29,7 +22,8 @@ const MaxCategorizeOptions = 200
|
||||
// like {"categoryId":"1","confidence":0.08} is the model saying "I do not know" —
|
||||
// taking it at face value files a camping chair under Generators and then drives
|
||||
// that category's title/description formula, producing confidently wrong copy.
|
||||
// Below this floor the product fails with ErrCategoryNotFound instead.
|
||||
// Below this floor the pick is discarded and the product continues uncategorized
|
||||
// (generic enhance template, feed attributes kept) instead of under a wrong formula.
|
||||
const DefaultMinCategorizeConfidence = 0.75
|
||||
|
||||
// MaxTokensCategorize budgets a short JSON reply {"categoryId","confidence"}.
|
||||
@@ -134,16 +128,17 @@ func ProductCategorizeUser(name, description string, opts []categoryOption) stri
|
||||
|
||||
// tryAICategorize asks the Completer to pick a company taxonomy unique_id when
|
||||
// Category is still empty after mapped/prior/vector. Never invents outside taxonomy:
|
||||
// responses are coerced then filtered to namesByUID / valid unique_ids.
|
||||
func tryAICategorize(ctx context.Context, e *Engine, out *StepResult, in ProductInput, policy StepPolicy) error {
|
||||
// responses are coerced then filtered to namesByUID / valid unique_ids. A rejected
|
||||
// pick leaves Category empty; supplier feeds without categories must still process.
|
||||
func tryAICategorize(ctx context.Context, e *Engine, out *StepResult, in ProductInput, policy StepPolicy) {
|
||||
if out == nil || strings.TrimSpace(out.Category) != "" {
|
||||
return nil
|
||||
return
|
||||
}
|
||||
if !policy.AllowAI {
|
||||
return nil
|
||||
return
|
||||
}
|
||||
if e == nil || !e.CompleterEnabled() {
|
||||
return nil
|
||||
return
|
||||
}
|
||||
opts := categoryOptionsFromNames(in.CategoryNamesByUID)
|
||||
if len(opts) == 0 {
|
||||
@@ -152,7 +147,7 @@ func tryAICategorize(ctx context.Context, e *Engine, out *StepResult, in Product
|
||||
"status": "skipped",
|
||||
"reason": "no_taxonomy",
|
||||
})
|
||||
return nil
|
||||
return
|
||||
}
|
||||
name := strings.TrimSpace(out.Name)
|
||||
if name == "" {
|
||||
@@ -168,7 +163,7 @@ func tryAICategorize(ctx context.Context, e *Engine, out *StepResult, in Product
|
||||
"status": "skipped",
|
||||
"reason": "empty_product",
|
||||
})
|
||||
return nil
|
||||
return
|
||||
}
|
||||
|
||||
system := ProductCategorizeSystem
|
||||
@@ -185,7 +180,7 @@ func tryAICategorize(ctx context.Context, e *Engine, out *StepResult, in Product
|
||||
"error": TruncateError(err),
|
||||
"tokens": comp.TotalTokens,
|
||||
})
|
||||
return nil
|
||||
return
|
||||
}
|
||||
|
||||
raw := categoryIDFromCategorizeJSON(obj)
|
||||
@@ -202,12 +197,13 @@ func tryAICategorize(ctx context.Context, e *Engine, out *StepResult, in Product
|
||||
"returned": raw,
|
||||
"tokens": comp.TotalTokens,
|
||||
})
|
||||
return fmt.Errorf("%w: model returned %q, which is not in the company taxonomy",
|
||||
ErrCategoryNotFound, truncateRunes(raw, 60))
|
||||
log.Printf("processing: category rejected returned=%q reason=not_in_taxonomy; continuing uncategorized",
|
||||
truncateRunes(raw, 60))
|
||||
return
|
||||
}
|
||||
|
||||
// A confident-looking id with a low score is the model guessing. Refuse it
|
||||
// rather than driving the wrong category's formula with it.
|
||||
// rather than driving the wrong category's formula with it; process uncategorized.
|
||||
if min := policy.CategorizeConfidence(); hasConfidence && confidence < min {
|
||||
out.Notes = append(out.Notes, fmt.Sprintf(
|
||||
"ai_categorize: no category found (confidence %.2f below %.2f)", confidence, min))
|
||||
@@ -219,10 +215,9 @@ func tryAICategorize(ctx context.Context, e *Engine, out *StepResult, in Product
|
||||
"minimum": min,
|
||||
"tokens": comp.TotalTokens,
|
||||
})
|
||||
log.Printf("processing: category rejected uid=%s confidence=%.2f min=%.2f reason=low_confidence",
|
||||
log.Printf("processing: category rejected uid=%s confidence=%.2f min=%.2f reason=low_confidence; continuing uncategorized",
|
||||
resolved, confidence, min)
|
||||
return fmt.Errorf("%w: best match %q scored %.2f, below the %.2f minimum",
|
||||
ErrCategoryNotFound, truncateRunes(resolved, 60), confidence, min)
|
||||
return
|
||||
}
|
||||
|
||||
out.Category = SanitizeText(resolved)
|
||||
@@ -244,7 +239,6 @@ func tryAICategorize(ctx context.Context, e *Engine, out *StepResult, in Product
|
||||
if hasConfidence {
|
||||
out.Notes = append(out.Notes, fmt.Sprintf("ai_categorize: confidence %.2f", confidence))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// categorizeConfidenceFromJSON reads the model's self-reported score. ok=false when
|
||||
@@ -317,12 +311,10 @@ func firstAvailableCategoryIDFromPrompt(user string) string {
|
||||
}
|
||||
|
||||
// runCategorizeStep applies vector then LLM taxonomy selection when Category empty.
|
||||
// runCategorizeStep returns ErrCategoryNotFound when the model was asked for a
|
||||
// category and could not give a usable one — the caller fails the product rather
|
||||
// than enhancing it under a guess.
|
||||
func runCategorizeStep(ctx context.Context, e *Engine, companyID string, out *StepResult, in ProductInput, categoryNames []string, policy StepPolicy) error {
|
||||
// Category is optional: when nothing usable is found the product continues without one.
|
||||
func runCategorizeStep(ctx context.Context, e *Engine, companyID string, out *StepResult, in ProductInput, categoryNames []string, policy StepPolicy) {
|
||||
if out == nil {
|
||||
return nil
|
||||
return
|
||||
}
|
||||
if strings.TrimSpace(out.Category) != "" {
|
||||
appendStepLog(out.GPTResponse, StepCategorize, map[string]any{
|
||||
@@ -330,7 +322,7 @@ func runCategorizeStep(ctx context.Context, e *Engine, companyID string, out *St
|
||||
"reason": "already_set",
|
||||
"category": out.Category,
|
||||
})
|
||||
return nil
|
||||
return
|
||||
}
|
||||
if !policy.AllowAI {
|
||||
out.Notes = append(out.Notes, "categorize: skipped (AI not allowed)")
|
||||
@@ -338,24 +330,21 @@ func runCategorizeStep(ctx context.Context, e *Engine, companyID string, out *St
|
||||
"status": "skipped",
|
||||
"reason": "entitlement_can_use_ai",
|
||||
})
|
||||
return nil
|
||||
return
|
||||
}
|
||||
tryVectorCategorize(ctx, e, companyID, out, categoryNames, policy)
|
||||
if strings.TrimSpace(out.Category) != "" {
|
||||
return nil
|
||||
return
|
||||
}
|
||||
// Taxonomy picks and copy generation share one Completer; tag the role so the
|
||||
// admin inspector can tell them apart.
|
||||
if err := tryAICategorize(aiaudit.WithCall(ctx, aiaudit.CallContext{Role: aiaudit.RoleCategorize}), e, out, in, policy); err != nil {
|
||||
return err
|
||||
}
|
||||
tryAICategorize(aiaudit.WithCall(ctx, aiaudit.CallContext{Role: aiaudit.RoleCategorize}), e, out, in, policy)
|
||||
if strings.TrimSpace(out.Category) == "" {
|
||||
appendStepLog(out.GPTResponse, StepCategorize, map[string]any{
|
||||
"status": "unset",
|
||||
"reason": "no_vector_or_ai_match",
|
||||
})
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// MappedCategorySourceKey marks a mapped_data.category this pipeline wrote itself.
|
||||
|
||||
Reference in new issue
Block a user