fix(provider): tweak reasoning option driven max budget variants (#36626)
This commit is contained in:
@@ -1611,17 +1611,13 @@ function effortVariants(model: Provider.Model, values: readonly unknown[]) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function budgetVariants(model: Provider.Model, min?: number, max?: number) {
|
function budgetVariants(model: Provider.Model, min?: number, max?: number) {
|
||||||
const limit = model.limit.output - 1
|
const maximum = Math.min(max ?? OUTPUT_TOKEN_MAX - 1, model.limit.output - 1, OUTPUT_TOKEN_MAX - 1)
|
||||||
if (limit <= 0) return {}
|
if (maximum <= 0) return {}
|
||||||
const high = Math.min(
|
const high = Math.min(Math.max(min ?? 0, Math.floor((maximum + 1) / 2)), maximum)
|
||||||
max === undefined ? Math.max(min ?? 0, 16_000) : Math.min(Math.max(min ?? 0, 16_000), max),
|
|
||||||
limit,
|
|
||||||
)
|
|
||||||
const maximum = max === undefined ? undefined : Math.min(max, limit)
|
|
||||||
return Object.fromEntries(
|
return Object.fromEntries(
|
||||||
[
|
[
|
||||||
{ id: "high", budget: high },
|
{ id: "high", budget: high },
|
||||||
...(maximum === undefined || maximum === high ? [] : [{ id: "max", budget: maximum }]),
|
{ id: "max", budget: maximum },
|
||||||
].flatMap((item) => {
|
].flatMap((item) => {
|
||||||
const settings = reasoningBudget(model, item.budget)
|
const settings = reasoningBudget(model, item.budget)
|
||||||
return settings ? [[item.id, settings]] : []
|
return settings ? [[item.id, settings]] : []
|
||||||
|
|||||||
@@ -3111,9 +3111,9 @@ describe("ProviderTransform.reasoningVariants", () => {
|
|||||||
["@ai-sdk/cohere", { thinking: { type: "enabled", tokenBudget: 16_000 } }],
|
["@ai-sdk/cohere", { thinking: { type: "enabled", tokenBudget: 16_000 } }],
|
||||||
["@ai-sdk/alibaba", { enableThinking: true, thinkingBudget: 16_000 }],
|
["@ai-sdk/alibaba", { enableThinking: true, thinkingBudget: 16_000 }],
|
||||||
])("converts token budgets for %s", (npm, high) => {
|
])("converts token budgets for %s", (npm, high) => {
|
||||||
expect(
|
const variants = ProviderTransform.reasoningVariants(model([{ type: "budget_tokens", min: 1_024 }]), target(npm))
|
||||||
ProviderTransform.reasoningVariants(model([{ type: "budget_tokens", min: 1_024, max: 16_000 }]), target(npm)),
|
expect(variants?.high).toEqual(high)
|
||||||
).toEqual({ high })
|
expect(Object.keys(variants ?? {})).toEqual(["high", "max"])
|
||||||
})
|
})
|
||||||
|
|
||||||
test("maps null effort to none", () => {
|
test("maps null effort to none", () => {
|
||||||
@@ -3150,6 +3150,7 @@ describe("ProviderTransform.reasoningVariants", () => {
|
|||||||
).toEqual({
|
).toEqual({
|
||||||
none: { thinking: { type: "disabled" } },
|
none: { thinking: { type: "disabled" } },
|
||||||
high: { thinking: { type: "enabled", tokenBudget: 16_000 } },
|
high: { thinking: { type: "enabled", tokenBudget: 16_000 } },
|
||||||
|
max: { thinking: { type: "enabled", tokenBudget: 31_999 } },
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -3161,7 +3162,7 @@ describe("ProviderTransform.reasoningVariants", () => {
|
|||||||
),
|
),
|
||||||
).toEqual({
|
).toEqual({
|
||||||
high: { thinking: { type: "enabled", budgetTokens: 16_000 } },
|
high: { thinking: { type: "enabled", budgetTokens: 16_000 } },
|
||||||
max: { thinking: { type: "enabled", budgetTokens: 63_999 } },
|
max: { thinking: { type: "enabled", budgetTokens: 31_999 } },
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -3171,7 +3172,32 @@ describe("ProviderTransform.reasoningVariants", () => {
|
|||||||
expect(
|
expect(
|
||||||
ProviderTransform.reasoningVariants(model([{ type: "budget_tokens", min: 1_024, max: 64_000 }]), anthropic),
|
ProviderTransform.reasoningVariants(model([{ type: "budget_tokens", min: 1_024, max: 64_000 }]), anthropic),
|
||||||
).toEqual({
|
).toEqual({
|
||||||
high: { thinking: { type: "enabled", budgetTokens: 4_999 } },
|
high: { thinking: { type: "enabled", budgetTokens: 2_500 } },
|
||||||
|
max: { thinking: { type: "enabled", budgetTokens: 4_999 } },
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
test("derives high and max budgets when models.dev omits max", () => {
|
||||||
|
expect(
|
||||||
|
ProviderTransform.reasoningVariants(
|
||||||
|
model([{ type: "budget_tokens", min: 1_024 }]),
|
||||||
|
target("@ai-sdk/anthropic", "claude-haiku-4-5"),
|
||||||
|
),
|
||||||
|
).toEqual({
|
||||||
|
high: { thinking: { type: "enabled", budgetTokens: 16_000 } },
|
||||||
|
max: { thinking: { type: "enabled", budgetTokens: 31_999 } },
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
test("preserves explicit inclusive budget maxima", () => {
|
||||||
|
expect(
|
||||||
|
ProviderTransform.reasoningVariants(
|
||||||
|
model([{ type: "budget_tokens", min: 1_024, max: 24_576 }]),
|
||||||
|
target("@ai-sdk/google", "gemini-2.5-pro"),
|
||||||
|
),
|
||||||
|
).toEqual({
|
||||||
|
high: { thinkingConfig: { includeThoughts: true, thinkingBudget: 12_288 } },
|
||||||
|
max: { thinkingConfig: { includeThoughts: true, thinkingBudget: 24_576 } },
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user