fix(ai): diagnose summary provider failures (#37)
This commit is contained in:
@ -5,6 +5,7 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/tradarr/backend/internal/companycatalog"
|
||||
"time"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/tradarr/backend/internal/generation"
|
||||
@ -42,11 +43,25 @@ func (a generationRanker) WithSelectionBudget(ctx context.Context) (context.Cont
|
||||
}
|
||||
|
||||
type generationSummarizer struct {
|
||||
p *Pipeline
|
||||
provider Provider
|
||||
p *Pipeline
|
||||
provider Provider
|
||||
diagnostics SummaryDiagnosticCollector
|
||||
}
|
||||
|
||||
func (a generationSummarizer) Summarize(ctx context.Context, language string, assets []models.UserAsset, candidates []generation.Candidate) (string, error) {
|
||||
func (a generationSummarizer) Summarize(ctx context.Context, language string, assets []models.UserAsset, candidates []generation.Candidate) (result string, summaryErr error) {
|
||||
started := time.Now()
|
||||
d := SummaryDiagnostic{ArticleCount: len(candidates), PromptBudgetBytes: 24 * 1024}
|
||||
collector := a.diagnostics
|
||||
if collector == nil && a.p != nil {
|
||||
collector = a.p.summaryDiagnostics
|
||||
}
|
||||
defer func() {
|
||||
if d.Category == "" {
|
||||
classifySummaryDiagnostic(ctx, summaryErr, result, &d)
|
||||
}
|
||||
d.DurationMS = time.Since(started).Milliseconds()
|
||||
emitSummaryDiagnostic(collector, d)
|
||||
}()
|
||||
provider := a.provider
|
||||
if provider == nil {
|
||||
return "", fmt.Errorf("summary_model_unavailable")
|
||||
@ -87,7 +102,9 @@ func (a generationSummarizer) Summarize(ctx context.Context, language string, as
|
||||
// slots for generation/framing in the conservative byte-token model.
|
||||
// Titles, candidate IDs, asset identities and article order are immutable.
|
||||
const promptByteBudget = 24 * 1024
|
||||
if len(build(0)) > promptByteBudget {
|
||||
d.PromptBytes = len(build(0))
|
||||
if d.PromptBytes > promptByteBudget {
|
||||
d.Category = "metadata_budget_exceeded"
|
||||
return "", fmt.Errorf("summary_prompt_metadata_exceeds_budget")
|
||||
}
|
||||
low, high := 0, promptByteBudget
|
||||
@ -100,6 +117,12 @@ func (a generationSummarizer) Summarize(ctx context.Context, language string, as
|
||||
}
|
||||
}
|
||||
prompt := build(low)
|
||||
d.PromptBytes = len(prompt)
|
||||
for _, article := range articles {
|
||||
if article.ContentTruncated || len(utf8Prefix(article.Content, low)) < len(article.Content) {
|
||||
d.TruncatedArticles++
|
||||
}
|
||||
}
|
||||
if len(prompt) > promptByteBudget {
|
||||
return "", fmt.Errorf("summary_prompt_exceeds_budget")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user