Repository navigation
Fix provider catalog metadata safety #573
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+104
−6
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1859cf7
fix: normalize provider catalog metadata
e3becae
fix: bound normalized catalog output limits
84d7b9e
fix: reserve usable catalog input budgets
43a5bc4
fix: normalize context-only catalog limits
371e43b
fix: normalize output-only catalog limits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| import assert from "node:assert/strict"; | ||
| import test from "node:test"; | ||
| import { normalizeModelTokenLimits } from "../provider-model-catalog-normalization.mjs"; | ||
|
|
||
| test("catalog token limits preserve output capacity that leaves room for model input", () => { | ||
| assert.deepEqual(normalizeModelTokenLimits({ context: 8_192, output: 4_096 }), { | ||
| contextWindow: 8_192, | ||
| maxTokens: 4_096, | ||
| }); | ||
| }); | ||
|
|
||
| test("catalog token limits bound invalid output capacity to leave a usable input budget", () => { | ||
| for (const [context, output, expectedOutput] of [ | ||
| [8_192, 8_192, 6_656], | ||
| [8_192, 16_384, 6_656], | ||
| [8_192, 7_680, 6_656], | ||
| [1_048_576, 1_048_576, 16_384], | ||
| ]) { | ||
| assert.deepEqual(normalizeModelTokenLimits({ context, output }), { | ||
| contextWindow: context, | ||
| maxTokens: expectedOutput, | ||
| }); | ||
| } | ||
| }); | ||
|
|
||
| test("catalog token limits omit an unusably small paired budget", () => { | ||
| assert.deepEqual(normalizeModelTokenLimits({ context: 512, output: 512 }), { | ||
| contextWindow: undefined, | ||
| maxTokens: undefined, | ||
| }); | ||
| }); | ||
|
|
||
| test("catalog token limits retain valid standalone capacities", () => { | ||
| assert.deepEqual(normalizeModelTokenLimits({ output: 4_096 }), { | ||
| contextWindow: undefined, | ||
| maxTokens: 4_096, | ||
| }); | ||
| assert.deepEqual(normalizeModelTokenLimits({ output: 100_000 }), { | ||
| contextWindow: undefined, | ||
| maxTokens: 100_000, | ||
| }); | ||
| assert.deepEqual(normalizeModelTokenLimits({ output: 128_000 }), { | ||
| contextWindow: undefined, | ||
| maxTokens: 16_384, | ||
| }); | ||
| assert.deepEqual(normalizeModelTokenLimits({ context: 8_192 }), { | ||
| contextWindow: 8_192, | ||
| maxTokens: 6_656, | ||
| }); | ||
| assert.deepEqual(normalizeModelTokenLimits({ context: 128_000 }), { | ||
| contextWindow: 128_000, | ||
| maxTokens: undefined, | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| const RESERVED_RUNTIME_OVERHEAD_TOKENS = 512; | ||
| const MINIMUM_INPUT_BUDGET_TOKENS = 1_024; | ||
| const DEFAULT_CONTEXT_WINDOW_TOKENS = 128_000; | ||
| const DEFAULT_OUTPUT_LIMIT_TOKENS = 16_384; | ||
|
|
||
| export function normalizeModelTokenLimits(limit) { | ||
| let contextWindow = positiveInteger(limit?.context); | ||
| const outputLimit = positiveInteger(limit?.output); | ||
| let maxTokens = outputLimit; | ||
|
|
||
| if (contextWindow === undefined && outputLimit !== undefined) { | ||
| const fallbackSafeOutput = DEFAULT_CONTEXT_WINDOW_TOKENS | ||
| - RESERVED_RUNTIME_OVERHEAD_TOKENS | ||
| - MINIMUM_INPUT_BUDGET_TOKENS; | ||
| if (outputLimit > fallbackSafeOutput) { | ||
| maxTokens = Math.min(outputLimit, DEFAULT_OUTPUT_LIMIT_TOKENS, fallbackSafeOutput); | ||
| } | ||
| } else if (contextWindow !== undefined) { | ||
| const largestSafeOutput = contextWindow - RESERVED_RUNTIME_OVERHEAD_TOKENS - MINIMUM_INPUT_BUDGET_TOKENS; | ||
| if (largestSafeOutput <= 0) { | ||
| contextWindow = undefined; | ||
| maxTokens = undefined; | ||
| } else if (outputLimit === undefined) { | ||
| maxTokens = DEFAULT_OUTPUT_LIMIT_TOKENS > largestSafeOutput ? largestSafeOutput : undefined; | ||
| } else if (outputLimit > largestSafeOutput) { | ||
| maxTokens = Math.min(outputLimit, DEFAULT_OUTPUT_LIMIT_TOKENS, largestSafeOutput); | ||
| } | ||
| } | ||
|
|
||
| return { contextWindow, maxTokens }; | ||
| } | ||
|
|
||
| function positiveInteger(value) { | ||
| return typeof value === "number" && Number.isSafeInteger(value) && value > 0 ? value : undefined; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.