-
Notifications
You must be signed in to change notification settings - Fork 19
Show context window usage #262
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
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
4a2a995
Show context window usage
jtpio 79631ce
get context window info
jtpio cbadb92
simplify
jtpio a9d14ef
comments
jtpio 41cbd5a
round number is fine
jtpio 0232453
Merge branch 'main' into context-window
jtpio 03e565c
simplify
jtpio 00921c6
more cleanup
jtpio 9ae64f1
Merge branch 'main' into context-window
brichet 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
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,228 @@ | ||
| #!/usr/bin/env node | ||
|
|
||
| import { readFile, writeFile } from 'node:fs/promises'; | ||
| import path from 'node:path'; | ||
| import { fileURLToPath } from 'node:url'; | ||
|
|
||
| const __dirname = path.dirname(fileURLToPath(import.meta.url)); | ||
| const ROOT_DIR = path.resolve(__dirname, '..'); | ||
| const BUILT_IN_PROVIDERS_FILE = path.join( | ||
| ROOT_DIR, | ||
| 'src/providers/built-in-providers.ts' | ||
| ); | ||
| const OUTPUT_FILE = path.join( | ||
| ROOT_DIR, | ||
| 'src/providers/generated-context-windows.ts' | ||
| ); | ||
| const BUILT_IN_PROVIDER_IDS = ['anthropic', 'google', 'mistral', 'openai']; | ||
| const DATE_SUFFIX = /^(.*)-\d{4}-\d{2}-\d{2}$/; | ||
| const SHORT_VERSION_SUFFIX = /^(.*)-\d{4}$/; | ||
|
|
||
| function extractDefaultModels(source, providerId) { | ||
| const pattern = new RegExp( | ||
| `export const ${providerId}Provider: IProviderInfo = \\{[\\s\\S]*?defaultModels: \\[([\\s\\S]*?)\\]\\s*,`, | ||
| 'm' | ||
| ); | ||
| const match = source.match(pattern); | ||
|
|
||
| if (!match) { | ||
| throw new Error( | ||
| `Could not find defaultModels for provider "${providerId}" in ${BUILT_IN_PROVIDERS_FILE}` | ||
| ); | ||
| } | ||
|
|
||
| return [...match[1].matchAll(/'([^']+)'/g)].map(entry => entry[1]); | ||
| } | ||
|
|
||
| function normalizeProviders(payload) { | ||
| if (Array.isArray(payload)) { | ||
| return Object.fromEntries( | ||
| payload | ||
| .filter(provider => provider && typeof provider.id === 'string') | ||
| .map(provider => [provider.id, provider]) | ||
| ); | ||
| } | ||
|
|
||
| if (!payload || typeof payload !== 'object') { | ||
| throw new Error('Unexpected models.dev payload shape'); | ||
| } | ||
|
|
||
| if (payload.providers && typeof payload.providers === 'object') { | ||
| return payload.providers; | ||
| } | ||
|
|
||
| if ( | ||
| payload['.opencode.models'] && | ||
| typeof payload['.opencode.models'] === 'object' | ||
| ) { | ||
| return payload['.opencode.models']; | ||
| } | ||
|
|
||
| return payload; | ||
| } | ||
|
|
||
| function getCandidateModelIds(modelId, providerModels = {}) { | ||
| const candidates = [modelId]; | ||
|
|
||
| if (modelId.endsWith('-latest')) { | ||
| const familyId = modelId.slice(0, -7); | ||
| candidates.push(familyId); | ||
| candidates.push( | ||
| ...Object.keys(providerModels) | ||
| .filter(candidateId => { | ||
| if (providerModels[candidateId]?.limit?.context === undefined) { | ||
| return false; | ||
| } | ||
| return ( | ||
| candidateId === familyId || candidateId.startsWith(`${familyId}-`) | ||
| ); | ||
| }) | ||
| .sort((a, b) => b.localeCompare(a)) | ||
| ); | ||
| } | ||
|
|
||
| const dateSuffixMatch = modelId.match(DATE_SUFFIX); | ||
| if (dateSuffixMatch) { | ||
| candidates.push(dateSuffixMatch[1]); | ||
| } | ||
|
|
||
| const shortVersionSuffixMatch = modelId.match(SHORT_VERSION_SUFFIX); | ||
| if (shortVersionSuffixMatch) { | ||
| candidates.push(shortVersionSuffixMatch[1]); | ||
| } | ||
|
|
||
| return [...new Set(candidates)]; | ||
| } | ||
|
|
||
| async function loadModelsDevPayload() { | ||
| const response = await fetch('https://models.dev/api.json'); | ||
|
|
||
| if (!response.ok) { | ||
| throw new Error( | ||
| `Failed to fetch https://models.dev/api.json: ${response.status} ${response.statusText}` | ||
| ); | ||
| } | ||
|
|
||
| return response.json(); | ||
| } | ||
|
|
||
| function renderGeneratedFile(contextWindows) { | ||
| const generatedAt = new Date().toISOString(); | ||
| const providersSource = BUILT_IN_PROVIDER_IDS.map(providerId => { | ||
| const models = contextWindows[providerId]; | ||
| const renderedModels = Object.entries(models) | ||
| .map( | ||
| ([modelId, modelInfo]) => | ||
| ` '${modelId}': { contextWindow: ${modelInfo.contextWindow} }` | ||
| ) | ||
| .join(',\n'); | ||
|
|
||
| return ` ${providerId}: {\n${renderedModels}\n }`; | ||
| }).join(',\n'); | ||
|
|
||
| return `/** | ||
| * This file is generated by \`jlpm sync:model-context-windows\`. | ||
| * Source: https://models.dev/api.json | ||
| * Backed by: https://github.com/anomalyco/models.dev | ||
| * Generated: ${generatedAt} | ||
| */ | ||
|
|
||
| import type { IProviderModelInfo } from '../tokens'; | ||
|
|
||
| export const BUILT_IN_PROVIDER_MODEL_INFO: Record< | ||
| string, | ||
| Record<string, IProviderModelInfo> | ||
| > = { | ||
| ${providersSource} | ||
| }; | ||
| `; | ||
| } | ||
|
|
||
| async function main() { | ||
| const builtInProvidersSource = await readFile( | ||
| BUILT_IN_PROVIDERS_FILE, | ||
| 'utf8' | ||
| ); | ||
| const modelsDevPayload = await loadModelsDevPayload(); | ||
| const providers = normalizeProviders(modelsDevPayload); | ||
| const contextWindows = {}; | ||
| const aliasResolutions = []; | ||
| const missingModels = []; | ||
|
|
||
| for (const providerId of BUILT_IN_PROVIDER_IDS) { | ||
| const providerData = providers[providerId]; | ||
|
|
||
| if (!providerData || typeof providerData !== 'object') { | ||
| throw new Error(`Provider "${providerId}" was not found in models.dev`); | ||
| } | ||
|
|
||
| const providerModels = | ||
| providerData.models && typeof providerData.models === 'object' | ||
| ? providerData.models | ||
| : {}; | ||
| const defaultModels = extractDefaultModels( | ||
| builtInProvidersSource, | ||
| providerId | ||
| ); | ||
| const resolvedModels = {}; | ||
| const unresolvedModels = []; | ||
|
|
||
| for (const modelId of defaultModels) { | ||
| let resolvedId; | ||
|
|
||
| for (const candidateId of getCandidateModelIds(modelId, providerModels)) { | ||
| if (providerModels[candidateId]?.limit?.context !== undefined) { | ||
| resolvedId = candidateId; | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| if (!resolvedId) { | ||
| unresolvedModels.push(modelId); | ||
| continue; | ||
| } | ||
|
|
||
| const contextWindow = providerModels[resolvedId].limit.context; | ||
|
|
||
| resolvedModels[modelId] = { contextWindow }; | ||
| resolvedModels[resolvedId] = { contextWindow }; | ||
|
|
||
| if (resolvedId !== modelId) { | ||
| aliasResolutions.push({ providerId, modelId, resolvedId }); | ||
| } | ||
| } | ||
|
|
||
| contextWindows[providerId] = resolvedModels; | ||
|
|
||
| if (unresolvedModels.length > 0) { | ||
| missingModels.push({ providerId, modelIds: unresolvedModels }); | ||
| } | ||
| } | ||
|
|
||
| await writeFile(OUTPUT_FILE, renderGeneratedFile(contextWindows)); | ||
|
|
||
| console.log(`Wrote ${path.relative(ROOT_DIR, OUTPUT_FILE)}.`); | ||
| console.log( | ||
| `Resolved ${aliasResolutions.length} aliased model IDs from built-in defaults.` | ||
| ); | ||
|
|
||
| if (aliasResolutions.length > 0) { | ||
| for (const { providerId, modelId, resolvedId } of aliasResolutions) { | ||
| console.log(` ${providerId}: ${modelId} -> ${resolvedId}`); | ||
| } | ||
| } | ||
|
|
||
| if (missingModels.length > 0) { | ||
| console.warn( | ||
| '\nmodels.dev does not currently expose context windows for some built-in model IDs:' | ||
| ); | ||
| for (const { providerId, modelIds } of missingModels) { | ||
| console.warn(` ${providerId}: ${modelIds.join(', ')}`); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| main().catch(error => { | ||
| console.error(error instanceof Error ? error.message : String(error)); | ||
| process.exitCode = 1; | ||
| }); |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In a follow up PR we could add a workflow to update it every X days and open a PR.