feat(generation): enforce prompt budget with whole article eviction (#47)
This commit is contained in:
@ -46,23 +46,24 @@ type SourceStat struct {
|
||||
FailureCode string `json:"failure_code,omitempty"`
|
||||
}
|
||||
type GenerationOutcome struct {
|
||||
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"`
|
||||
ArticlesRemovedForPromptBudget int `json:"articles_removed_for_prompt_budget"`
|
||||
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"`
|
||||
@ -92,7 +93,7 @@ type SummaryRequest struct {
|
||||
GeneratedAt time.Time
|
||||
}
|
||||
type BudgetedSummarizer interface {
|
||||
SummarizeWithBudget(context.Context, SummaryRequest) (string, summarybudget.Usage, error)
|
||||
SummarizeWithBudget(context.Context, SummaryRequest) (string, summarybudget.Usage, int, error)
|
||||
}
|
||||
type ResolvedSummary struct {
|
||||
Summarizer Summarizer
|
||||
@ -342,7 +343,7 @@ func (u *useCase) Generate(ctx context.Context, userID string) (Result, error) {
|
||||
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})
|
||||
content, out.PromptBudget, out.ArticlesRemovedForPromptBudget, 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)
|
||||
}
|
||||
@ -366,8 +367,15 @@ func (u *useCase) Generate(ctx context.Context, userID string) (Result, error) {
|
||||
}
|
||||
|
||||
func summaryFailureCode(ctx context.Context, err error) string {
|
||||
if errors.Is(err, summarybudget.ErrUnknown) {
|
||||
return "summary_capacity_unknown"
|
||||
var providerFailure ProviderFailure
|
||||
if errors.As(err, &providerFailure) {
|
||||
return providerFailure.FailureCode()
|
||||
}
|
||||
if errors.Is(err, summarybudget.ErrFixedContextExceeded) {
|
||||
return "summary_fixed_context_budget_exceeded"
|
||||
}
|
||||
if errors.Is(err, summarybudget.ErrNoArticlesFit) {
|
||||
return "no_articles_fit_prompt_budget"
|
||||
}
|
||||
if errors.Is(err, summarybudget.ErrExceeded) {
|
||||
return "summary_prompt_budget_exceeded"
|
||||
|
||||
15
backend/internal/generation/provider_failure.go
Normal file
15
backend/internal/generation/provider_failure.go
Normal file
@ -0,0 +1,15 @@
|
||||
package generation
|
||||
|
||||
// ProviderFailure crosses the synthesis port without retaining provider payloads.
|
||||
// Only these stable codes may enter the persisted outcome.
|
||||
type ProviderFailure string
|
||||
|
||||
func (e ProviderFailure) Error() string { return e.FailureCode() }
|
||||
func (e ProviderFailure) FailureCode() string {
|
||||
switch string(e) {
|
||||
case "summary_model_rejected", "summary_provider_limit", "summary_auth_failed", "summary_rate_limited", "summary_unavailable", "summary_timeout", "summary_canceled":
|
||||
return string(e)
|
||||
default:
|
||||
return "summary_failed"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user