feat(summary): send complete articles within user budget (#39)
This commit is contained in:
@ -10,14 +10,17 @@ import (
|
||||
|
||||
"github.com/tradarr/backend/internal/models"
|
||||
"github.com/tradarr/backend/internal/scraper"
|
||||
"github.com/tradarr/backend/internal/summarybudget"
|
||||
)
|
||||
|
||||
type UserInput struct {
|
||||
SummaryModelID *string
|
||||
Language, RankingMode string
|
||||
GlobalLimit int
|
||||
Assets []models.UserAsset
|
||||
Sources []models.Source
|
||||
SummaryContext string
|
||||
SummaryPromptTokenBudget int
|
||||
SummaryModelID *string
|
||||
Language, RankingMode string
|
||||
GlobalLimit int
|
||||
Assets []models.UserAsset
|
||||
Sources []models.Source
|
||||
}
|
||||
type Candidate struct {
|
||||
ID, SourceID string
|
||||
@ -26,29 +29,38 @@ type Candidate struct {
|
||||
orderFrozen bool
|
||||
}
|
||||
type SourceStat struct {
|
||||
SourceID string `json:"source_id"`
|
||||
SourceType string `json:"source_type"`
|
||||
Status string `json:"status"`
|
||||
Candidates int `json:"candidates"`
|
||||
LayaSelected int `json:"laya_selected"`
|
||||
Selected int `json:"selected"`
|
||||
ArticleLimit int `json:"article_limit"`
|
||||
ContentFallbacks int `json:"content_fallbacks"`
|
||||
FailureCode string `json:"failure_code,omitempty"`
|
||||
ContentComplete int `json:"content_complete"`
|
||||
ContentIncomplete int `json:"content_incomplete"`
|
||||
ContentUnavailable int `json:"content_unavailable"`
|
||||
DatesAvailable int `json:"dates_available"`
|
||||
SourceID string `json:"source_id"`
|
||||
SourceType string `json:"source_type"`
|
||||
Status string `json:"status"`
|
||||
Candidates int `json:"candidates"`
|
||||
LayaSelected int `json:"laya_selected"`
|
||||
Selected int `json:"selected"`
|
||||
ArticleLimit int `json:"article_limit"`
|
||||
ContentFallbacks int `json:"content_fallbacks"`
|
||||
FailureCode string `json:"failure_code,omitempty"`
|
||||
}
|
||||
type GenerationOutcome struct {
|
||||
ID string `json:"id"`
|
||||
Status string `json:"status"`
|
||||
Language string `json:"language"`
|
||||
RankingMode string `json:"ranking_mode"`
|
||||
TotalCandidates int `json:"total_candidates"`
|
||||
ContentFallbacks int `json:"content_fallbacks"`
|
||||
LayaSelected int `json:"laya_selected"`
|
||||
Selected int `json:"selected"`
|
||||
GlobalLimit int `json:"global_limit"`
|
||||
SourceStats []SourceStat `json:"source_stats"`
|
||||
FailureCode string `json:"failure_code,omitempty"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
ContentComplete int `json:"content_complete"`
|
||||
ContentIncomplete int `json:"content_incomplete"`
|
||||
ContentUnavailable int `json:"content_unavailable"`
|
||||
DatesAvailable int `json:"dates_available"`
|
||||
PromptBudget summarybudget.Usage `json:"prompt_budget"`
|
||||
ID string `json:"id"`
|
||||
Status string `json:"status"`
|
||||
Language string `json:"language"`
|
||||
RankingMode string `json:"ranking_mode"`
|
||||
TotalCandidates int `json:"total_candidates"`
|
||||
ContentFallbacks int `json:"content_fallbacks"`
|
||||
LayaSelected int `json:"laya_selected"`
|
||||
Selected int `json:"selected"`
|
||||
GlobalLimit int `json:"global_limit"`
|
||||
SourceStats []SourceStat `json:"source_stats"`
|
||||
FailureCode string `json:"failure_code,omitempty"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
}
|
||||
type Result struct {
|
||||
Summary *models.Summary `json:"summary"`
|
||||
@ -69,6 +81,17 @@ type Ranker = LayaRanker
|
||||
type Summarizer interface {
|
||||
Summarize(context.Context, string, []models.UserAsset, []Candidate) (string, error)
|
||||
}
|
||||
type SummaryRequest struct {
|
||||
Language string
|
||||
Assets []models.UserAsset
|
||||
Candidates []Candidate
|
||||
Budget int
|
||||
Context string
|
||||
GeneratedAt time.Time
|
||||
}
|
||||
type BudgetedSummarizer interface {
|
||||
SummarizeWithBudget(context.Context, SummaryRequest) (string, summarybudget.Usage, error)
|
||||
}
|
||||
type ResolvedSummary struct {
|
||||
Summarizer Summarizer
|
||||
CatalogID, ProviderID, Provider, Model string
|
||||
@ -123,7 +146,11 @@ func (u *useCase) Generate(ctx context.Context, userID string) (Result, error) {
|
||||
if id == "" {
|
||||
id = NewID()
|
||||
}
|
||||
out := GenerationOutcome{ID: id, Language: input.Language, RankingMode: input.RankingMode, GlobalLimit: input.GlobalLimit, CreatedAt: u.Clock(), SourceStats: []SourceStat{}}
|
||||
requestedBudget := input.SummaryPromptTokenBudget
|
||||
if requestedBudget == 0 {
|
||||
requestedBudget = models.DefaultSummaryPromptTokenBudget
|
||||
}
|
||||
out := GenerationOutcome{PromptBudget: summarybudget.Usage{RequestedTokens: requestedBudget}, ID: id, Language: input.Language, RankingMode: input.RankingMode, GlobalLimit: input.GlobalLimit, CreatedAt: u.Clock(), SourceStats: []SourceStat{}}
|
||||
resolved := ResolvedSummary{Summarizer: u.Summarizer}
|
||||
finish := func(status, code, content string) (Result, error) {
|
||||
out.Status = status
|
||||
@ -151,7 +178,7 @@ func (u *useCase) Generate(ctx context.Context, userID string) (Result, error) {
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
if (input.Language != "fr" && input.Language != "en") || (input.RankingMode != "title" && input.RankingMode != "title_content") || input.GlobalLimit <= 0 {
|
||||
if (input.Language != "fr" && input.Language != "en") || (input.RankingMode != "title" && input.RankingMode != "title_content") || input.GlobalLimit <= 0 || requestedBudget < 1 || requestedBudget > models.MaxSummaryPromptTokenBudget {
|
||||
return finish("failed", "invalid_preferences", "")
|
||||
}
|
||||
// Explicit Summarizer is a test/injected port. Production always resolves
|
||||
@ -210,7 +237,7 @@ func (u *useCase) Generate(ctx context.Context, userID string) (Result, error) {
|
||||
orderDates[i] = &date
|
||||
}
|
||||
}
|
||||
if input.RankingMode == "title_content" && *source.SourceKey == models.SourceBloomberg {
|
||||
if input.RankingMode == "title_content" {
|
||||
if enricher, ok := feed.(scraper.ArticleEnricher); ok {
|
||||
enriched, enrichErr := enricher.Enrich(ctx, append([]scraper.Article(nil), articles...))
|
||||
valid := enrichErr == nil && len(enriched) == len(articles)
|
||||
@ -224,6 +251,7 @@ func (u *useCase) Generate(ctx context.Context, userID string) (Result, error) {
|
||||
}
|
||||
if valid {
|
||||
for i, a := range enriched {
|
||||
articles[i].ContentState = a.ContentState
|
||||
articles[i].Content = a.Content
|
||||
articles[i].ContentTruncated = a.ContentTruncated
|
||||
if a.PublishedAt != nil {
|
||||
@ -242,6 +270,28 @@ func (u *useCase) Generate(ctx context.Context, userID string) (Result, error) {
|
||||
article.Content = ""
|
||||
article.ContentTruncated = false
|
||||
}
|
||||
article.Source = *source.SourceKey
|
||||
if article.PublishedAt != nil {
|
||||
stat.DatesAvailable++
|
||||
out.DatesAvailable++
|
||||
}
|
||||
if input.RankingMode == "title_content" {
|
||||
switch scraper.ContentStatus(article) {
|
||||
case scraper.ContentComplete:
|
||||
stat.ContentComplete++
|
||||
out.ContentComplete++
|
||||
case scraper.ContentIncomplete:
|
||||
stat.ContentIncomplete++
|
||||
out.ContentIncomplete++
|
||||
partial = true
|
||||
continue
|
||||
default:
|
||||
stat.ContentUnavailable++
|
||||
out.ContentUnavailable++
|
||||
partial = true
|
||||
continue
|
||||
}
|
||||
}
|
||||
if input.RankingMode == "title" {
|
||||
article.Content = ""
|
||||
article.ContentTruncated = false
|
||||
@ -253,6 +303,9 @@ func (u *useCase) Generate(ctx context.Context, userID string) (Result, error) {
|
||||
out.SourceStats = append(out.SourceStats, stat)
|
||||
}
|
||||
if len(candidates) == 0 {
|
||||
if input.RankingMode == "title_content" && out.TotalCandidates > 0 {
|
||||
return finish("no_results", "no_complete_content", "")
|
||||
}
|
||||
if partial {
|
||||
return finish("failed", "sources_unavailable", "")
|
||||
}
|
||||
@ -274,7 +327,13 @@ func (u *useCase) Generate(ctx context.Context, userID string) (Result, error) {
|
||||
if len(selected) == 0 {
|
||||
return finish("no_results", "", "")
|
||||
}
|
||||
content, err := resolved.Summarizer.Summarize(ctx, input.Language, input.Assets, selected)
|
||||
var content string
|
||||
if budgeted, ok := resolved.Summarizer.(BudgetedSummarizer); ok {
|
||||
budget := requestedBudget
|
||||
content, out.PromptBudget, err = budgeted.SummarizeWithBudget(ctx, SummaryRequest{Language: input.Language, Assets: input.Assets, Candidates: selected, Budget: budget, Context: input.SummaryContext, GeneratedAt: out.CreatedAt})
|
||||
} else {
|
||||
content, err = resolved.Summarizer.Summarize(ctx, input.Language, input.Assets, selected)
|
||||
}
|
||||
if err != nil || ctx.Err() != nil {
|
||||
return finish("failed", summaryFailureCode(ctx, err), "")
|
||||
}
|
||||
@ -282,12 +341,25 @@ func (u *useCase) Generate(ctx context.Context, userID string) (Result, error) {
|
||||
return finish("failed", "summary_failed", "")
|
||||
}
|
||||
if partial {
|
||||
return finish("partial", "source_unavailable", content)
|
||||
code := "content_incomplete"
|
||||
for _, s := range out.SourceStats {
|
||||
if s.Status == "failed" {
|
||||
code = "source_unavailable"
|
||||
break
|
||||
}
|
||||
}
|
||||
return finish("partial", code, content)
|
||||
}
|
||||
return finish("succeeded", "", content)
|
||||
}
|
||||
|
||||
func summaryFailureCode(ctx context.Context, err error) string {
|
||||
if errors.Is(err, summarybudget.ErrUnknown) {
|
||||
return "summary_capacity_unknown"
|
||||
}
|
||||
if errors.Is(err, summarybudget.ErrExceeded) {
|
||||
return "summary_prompt_budget_exceeded"
|
||||
}
|
||||
if errors.Is(ctx.Err(), context.DeadlineExceeded) || errors.Is(err, context.DeadlineExceeded) {
|
||||
return "summary_timeout"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user