feat(summary): send complete articles within user budget (#39)
This commit is contained in:
76
backend/internal/summarybudget/budget.go
Normal file
76
backend/internal/summarybudget/budget.go
Normal file
@ -0,0 +1,76 @@
|
||||
// Package summarybudget owns offline, provider-scoped synthesis admission.
|
||||
package summarybudget
|
||||
|
||||
import "errors"
|
||||
|
||||
const UTF8UpperBound = "utf8_bytes_upper_bound_v1"
|
||||
|
||||
var ErrUnknown = errors.New("summary_capacity_unknown")
|
||||
var ErrExceeded = errors.New("summary_prompt_budget_exceeded")
|
||||
|
||||
type Profile struct {
|
||||
ContextWindowTokens int `json:"context_window_tokens"`
|
||||
InputLimitTokens int `json:"input_limit_tokens"`
|
||||
OutputReserveTokens int `json:"output_reserve_tokens"`
|
||||
Counter string `json:"token_count_method"`
|
||||
FramingTokens int `json:"framing_tokens"`
|
||||
}
|
||||
type Usage struct {
|
||||
RequestedTokens int `json:"requested_tokens"`
|
||||
EffectiveTokens int `json:"effective_tokens"`
|
||||
PromptTokens int `json:"prompt_tokens"`
|
||||
OutputReserveTokens int `json:"output_reserve_tokens"`
|
||||
CountMethod string `json:"token_count_method"`
|
||||
}
|
||||
|
||||
func (p Profile) Effective(requested int) (int, error) {
|
||||
if requested < 1 || p.Counter != UTF8UpperBound || p.ContextWindowTokens <= p.OutputReserveTokens || p.InputLimitTokens < 1 || p.OutputReserveTokens < 1 || p.FramingTokens < 0 {
|
||||
return 0, ErrUnknown
|
||||
}
|
||||
return min(requested, p.InputLimitTokens, p.ContextWindowTokens-p.OutputReserveTokens), nil
|
||||
}
|
||||
func (p Profile) Measure(prompt string, requested int) (Usage, error) {
|
||||
effective, err := p.Effective(requested)
|
||||
u := Usage{RequestedTokens: requested, EffectiveTokens: effective, OutputReserveTokens: p.OutputReserveTokens, CountMethod: p.Counter}
|
||||
if err != nil {
|
||||
return u, err
|
||||
}
|
||||
// Text-only byte-fallback tokenizers cannot produce more tokens than input
|
||||
// UTF-8 bytes. This conservative bound deliberately avoids bytes/4 guesses.
|
||||
// Framing includes the adapter's message envelope and model special tokens.
|
||||
u.PromptTokens = len(prompt) + p.FramingTokens
|
||||
if u.PromptTokens > effective {
|
||||
return u, ErrExceeded
|
||||
}
|
||||
return u, nil
|
||||
}
|
||||
|
||||
// Lookup is exact and scoped to the actual adapter, never a model-name prefix
|
||||
// or cross-provider fallback. Unknown/local/custom models fail closed.
|
||||
func Lookup(provider, model string) (Profile, bool) {
|
||||
window, input, reserve := 0, 0, 4096
|
||||
switch provider {
|
||||
case "openai":
|
||||
switch model {
|
||||
case "gpt-4o-mini", "gpt-4o-mini-2024-07-18", "gpt-4o", "gpt-4o-2024-08-06":
|
||||
window = 128000
|
||||
input = 128000
|
||||
}
|
||||
case "anthropic":
|
||||
switch model {
|
||||
case "claude-sonnet-4-6", "claude-opus-4-6", "claude-haiku-4-5-20251001", "claude-sonnet-4-5-20250929":
|
||||
window = 200000
|
||||
input = 200000
|
||||
}
|
||||
case "gemini":
|
||||
switch model {
|
||||
case "gemini-2.0-flash", "gemini-2.0-flash-001", "gemini-2.5-flash", "gemini-2.5-pro":
|
||||
window = 1048576
|
||||
input = 1048576
|
||||
}
|
||||
}
|
||||
if window == 0 {
|
||||
return Profile{}, false
|
||||
}
|
||||
return Profile{ContextWindowTokens: window, InputLimitTokens: input, OutputReserveTokens: reserve, Counter: UTF8UpperBound, FramingTokens: 1024}, true
|
||||
}
|
||||
33
backend/internal/summarybudget/budget_test.go
Normal file
33
backend/internal/summarybudget/budget_test.go
Normal file
@ -0,0 +1,33 @@
|
||||
package summarybudget
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestTicket39CapacityAndCounting(t *testing.T) {
|
||||
p := Profile{ContextWindowTokens: 100, InputLimitTokens: 90, OutputReserveTokens: 20, Counter: UTF8UpperBound, FramingTokens: 5}
|
||||
for _, tc := range []struct{ budget, want int }{{50, 50}, {250000, 80}} {
|
||||
if n, err := p.Effective(tc.budget); err != nil || n != tc.want {
|
||||
t.Fatalf("capacity %d %v", n, err)
|
||||
}
|
||||
}
|
||||
u, err := p.Measure("é🙂", 11)
|
||||
if err != nil || u.PromptTokens != 11 {
|
||||
t.Fatalf("tokens %+v %v", u, err)
|
||||
}
|
||||
_, err = p.Measure("é🙂", 10)
|
||||
if !errors.Is(err, ErrExceeded) {
|
||||
t.Fatal("overflow accepted")
|
||||
}
|
||||
p.Counter = "unknown"
|
||||
if _, err = p.Measure("x", 50); !errors.Is(err, ErrUnknown) {
|
||||
t.Fatal("unknown counter accepted")
|
||||
}
|
||||
if _, ok := Lookup("openai", "invented"); ok {
|
||||
t.Fatal("invented model accepted")
|
||||
}
|
||||
if _, ok := Lookup("ollama", "gpt-4o-mini"); ok {
|
||||
t.Fatal("cross-provider capacity inferred")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user