A group's catalog entry took the smallest member's output limit, so /v1/models and the agents' catalogs told agents that, and every reply through the group was capped at it, even from the members that write far longer ones. The group's Output is now its largest member's (as its context window already is, #700), and the gateway, for a group request, lowers the agent's max tokens to each member's own known limit (its catalog Output: the user's per-model setting, else the vendor's list or models.dev) only when the request asks for more: max_tokens / max_completion_tokens (Chat), max_output_tokens (Responses), generationConfig.maxOutputTokens (Gemini), and for Anthropic max_tokens with the thinking budget kept under it (budget = limit-1, or thinking off when no 1024+ budget fits). A request asking for less, or a member with no known limit, goes as before. TestGroupOutputPerMember (members at 4096 and 32000: the group says 32000; asked for 20000, the 4096 one gets 4096 and the 32000 one 20000; asked for 1000, 1000) fails on the old code at either half; TestWithMaxOutputAnthropicThinking. go vet, builds (darwin, GOOS=linux nogui, GOOS=windows) and go test -tags nogui -count=1 ./... under a temp HOME pass (TestProvidersAnswerWhileListsComeIn, TestUserPath and TestClaudeSignedOut failed under load and pass alone).
76 lines
2.3 KiB
Go
76 lines
2.3 KiB
Go
package gateway
|
|
|
|
import (
|
|
"encoding/json"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// outputLimit is the most a reply of a provider's model may hold, where
|
|
// known: its catalog entry's (the user's setting, else the vendor's list
|
|
// or models.dev).
|
|
func outputLimit(p provider.Provider, model string) int {
|
|
for _, e := range provider.Served() {
|
|
if e.Group == "" && e.Provider.ID == p.ID && (e.Model == model || e.ID == model) && e.Output > 0 {
|
|
return e.Output
|
|
}
|
|
}
|
|
return 0
|
|
}
|
|
|
|
// withMaxOutput is body, in proto's words, asking for a reply of at most
|
|
// limit tokens: a request that asks for more is lowered to limit, and
|
|
// Anthropic's thinking budget, which must be under it, with it. A request
|
|
// that asks for limit or less, or names no limit, goes as it is.
|
|
func withMaxOutput(proto provider.Protocol, body []byte, limit int) []byte {
|
|
var v struct {
|
|
MaxTokens int `json:"max_tokens"`
|
|
MaxCompletionTokens int `json:"max_completion_tokens"`
|
|
MaxOutputTokens int `json:"max_output_tokens"`
|
|
Thinking *struct {
|
|
Type string `json:"type"`
|
|
Budget int `json:"budget_tokens"`
|
|
} `json:"thinking"`
|
|
GenerationConfig map[string]any `json:"generationConfig"`
|
|
}
|
|
if limit <= 0 || json.Unmarshal(body, &v) != nil {
|
|
return body
|
|
}
|
|
fields := map[string]any{}
|
|
switch proto {
|
|
case provider.Chat:
|
|
if v.MaxCompletionTokens > limit {
|
|
fields["max_completion_tokens"] = limit
|
|
}
|
|
if v.MaxTokens > limit {
|
|
fields["max_tokens"] = limit
|
|
}
|
|
case provider.Responses:
|
|
if v.MaxOutputTokens > limit {
|
|
fields["max_output_tokens"] = limit
|
|
}
|
|
case provider.Anthropic:
|
|
if v.MaxTokens <= limit {
|
|
return body
|
|
}
|
|
fields["max_tokens"] = limit
|
|
if t := v.Thinking; t != nil && t.Type == "enabled" && t.Budget >= limit {
|
|
if limit-1 >= 1024 {
|
|
fields["thinking"] = map[string]any{"type": "enabled", "budget_tokens": limit - 1}
|
|
} else {
|
|
// no budget fits under it (Anthropic's least is 1024)
|
|
fields["thinking"] = map[string]any{"type": "disabled"}
|
|
}
|
|
}
|
|
case provider.Gemini:
|
|
if n, ok := v.GenerationConfig["maxOutputTokens"].(float64); ok && int(n) > limit {
|
|
v.GenerationConfig["maxOutputTokens"] = limit
|
|
fields["generationConfig"] = v.GenerationConfig
|
|
}
|
|
}
|
|
if len(fields) == 0 {
|
|
return body
|
|
}
|
|
return withFields(body, fields)
|
|
}
|