-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcompare-models.ts
More file actions
381 lines (344 loc) · 11.8 KB
/
Copy pathcompare-models.ts
File metadata and controls
381 lines (344 loc) · 11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#!/usr/bin/env node
import { readdir, readFile } from 'node:fs/promises'
import { dirname, join } from 'node:path'
import { fileURLToPath } from 'node:url'
const __dirname = dirname(fileURLToPath(import.meta.url))
const manifestsDir = join(__dirname, '../../manifests/models')
const apiDataFile = join(__dirname, '../../tmp/models-dev-api.json')
const mappingFile = join(__dirname, '../../manifests/mapping.json')
interface ComparisonResult {
match: boolean
skip: boolean
manifest?: unknown
api?: unknown
}
interface FieldComparison {
field: string
manifestKey: string
apiKey: string
match: boolean
skip: boolean
manifest?: unknown
api?: unknown
}
interface NormalizedApiModel {
name: string
releaseDate: string | null
contextWindow: number | null
maxOutput: number | null
inputModalities: string[]
tokenPricing: {
input: number | null
output: number | null
cache: number | null
}
capabilities: string[]
}
interface ModelResult {
modelId: string
apiModelId: string
vendor: string
vendorKey: string
vendorExists: boolean
modelExists: boolean
comparisons: FieldComparison[]
}
interface ApiData {
[vendorKey: string]: {
models?: {
[modelId: string]: {
name: string
release_date?: string | null
limit?: {
context?: number | null
output?: number | null
}
modalities?: {
input?: string[]
}
cost?: {
input?: number | null
output?: number | null
cache_read?: number | null
}
tool_call?: boolean
reasoning?: boolean
}
}
}
}
interface MappingData {
vendors: Record<string, string>
models: Record<string, string>
}
function getPrimaryManifestRates(manifest: {
tokenPricing?: {
status?: string
primaryOffer?: string | null
offers?: Array<{
id: string
tiers: Array<{
rates: {
input: number | null
output: number | null
cacheRead: number | null
}
}>
}>
}
}): { input: number | null; output: number | null; cacheRead: number | null } {
const pricing = manifest.tokenPricing
if (pricing?.status !== 'available') return { input: null, output: null, cacheRead: null }
const offer =
pricing.offers?.find(candidate => candidate.id === pricing.primaryOffer) ?? pricing.offers?.[0]
const rates = offer?.tiers[0]?.rates
return {
input: rates?.input ?? null,
output: rates?.output ?? null,
cacheRead: rates?.cacheRead ?? null,
}
}
// Helper to compare values and return match status
function compare(
manifestValue: unknown,
apiValue: unknown,
_manifestKey: string,
_apiKey: string
): ComparisonResult {
if (manifestValue === null && apiValue === undefined) return { match: true, skip: true }
if (manifestValue === null && apiValue === null) return { match: true, skip: false }
if (manifestValue === null && apiValue !== undefined)
return { match: false, skip: false, manifest: null, api: apiValue }
if (manifestValue !== null && apiValue === undefined)
return { match: false, skip: false, manifest: manifestValue, api: null }
if (manifestValue === apiValue) return { match: true, skip: false }
return { match: false, skip: false, manifest: manifestValue, api: apiValue }
}
// Convert API model data to manifest-compatible format for comparison
function normalizeApiModel(apiModel: Record<string, unknown> | undefined): NormalizedApiModel {
if (!apiModel) {
return {
name: '',
releaseDate: null,
contextWindow: null,
maxOutput: null,
inputModalities: [],
tokenPricing: {
input: null,
output: null,
cache: null,
},
capabilities: [],
}
}
const model = apiModel as {
name: string
release_date?: string | null
limit?: { context?: number | null; output?: number | null } | null
modalities?: { input?: string[] } | null
cost?: { input?: number | null; output?: number | null; cache_read?: number | null } | null
tool_call?: boolean
reasoning?: boolean
}
return {
name: model.name,
releaseDate: model.release_date || null,
contextWindow: model.limit?.context || null,
maxOutput: model.limit?.output || null,
inputModalities: model.modalities?.input || [],
tokenPricing: {
input: model.cost?.input || null,
output: model.cost?.output || null,
cache: model.cost?.cache_read || null,
},
capabilities: [
...(model.tool_call ? ['function-calling', 'tool-choice', 'structured-outputs'] : []),
...(model.reasoning ? ['reasoning'] : []),
].sort(),
}
}
async function main(): Promise<void> {
// Read API reference data and mapping
const apiData = JSON.parse(await readFile(apiDataFile, 'utf-8')) as ApiData
const { vendors: vendorMapping, models: modelMapping } = JSON.parse(
await readFile(mappingFile, 'utf-8')
) as MappingData
// Read all model manifests
const files = await readdir(manifestsDir)
const manifestFiles = files.filter(f => f.endsWith('.json'))
const results: ModelResult[] = []
for (const file of manifestFiles) {
const manifest = JSON.parse(await readFile(join(manifestsDir, file), 'utf-8'))
const modelId = manifest.id
const vendor = manifest.vendor
// Use mapping.json if available, otherwise lowercase the vendor name
const vendorKey = vendorMapping[vendor] ?? vendor.toLowerCase()
// Use mapping.json for model IDs if available
const apiModelId = modelMapping[modelId] ?? modelId
// Check if vendor exists in API data
const vendorData = apiData[vendorKey]
const vendorExists = !!vendorData
// Check if model exists under that vendor
const apiModel = vendorData?.models?.[apiModelId]
const modelExists = !!apiModel
const comparisons: FieldComparison[] = []
if (modelExists && apiModel) {
const normalizedApi = normalizeApiModel(apiModel)
// Compare releaseDate
comparisons.push({
field: 'releaseDate',
manifestKey: 'releaseDate',
apiKey: 'release_date',
...compare(manifest.releaseDate, normalizedApi.releaseDate, 'releaseDate', 'release_date'),
})
// Compare contextWindow
comparisons.push({
field: 'contextWindow',
manifestKey: 'contextWindow',
apiKey: 'limit.context',
...compare(
manifest.contextWindow,
normalizedApi.contextWindow,
'contextWindow',
'limit.context'
),
})
// Compare maxOutput
comparisons.push({
field: 'maxOutput',
manifestKey: 'maxOutput',
apiKey: 'limit.output',
...compare(manifest.maxOutput, normalizedApi.maxOutput, 'maxOutput', 'limit.output'),
})
// Compare inputModalities
const modalitiesMatch =
JSON.stringify(manifest.inputModalities.sort()) ===
JSON.stringify(normalizedApi.inputModalities.sort())
comparisons.push({
field: 'inputModalities',
manifestKey: 'inputModalities',
apiKey: 'modalities.input',
match: !!modalitiesMatch,
skip: false,
...(!modalitiesMatch
? { manifest: manifest.inputModalities, api: normalizedApi.inputModalities }
: {}),
})
// Compare tokenPricing
const manifestRates = getPrimaryManifestRates(manifest)
const inputPriceMatch = compare(
manifestRates.input,
normalizedApi.tokenPricing.input,
'input',
'input'
)
comparisons.push({
field: 'tokenPricing.input',
manifestKey: 'tokenPricing.offers[].tiers[].rates.input',
apiKey: 'cost.input',
...inputPriceMatch,
})
const outputPriceMatch = compare(
manifestRates.output,
normalizedApi.tokenPricing.output,
'output',
'output'
)
comparisons.push({
field: 'tokenPricing.output',
manifestKey: 'tokenPricing.offers[].tiers[].rates.output',
apiKey: 'cost.output',
...outputPriceMatch,
})
const cachePriceMatch = compare(
manifestRates.cacheRead,
normalizedApi.tokenPricing.cache,
'cache',
'cache_read'
)
comparisons.push({
field: 'tokenPricing.cacheRead',
manifestKey: 'tokenPricing.offers[].tiers[].rates.cacheRead',
apiKey: 'cost.cache_read',
...cachePriceMatch,
})
// Compare capabilities
const capabilitiesMatch =
JSON.stringify(manifest.capabilities.sort()) ===
JSON.stringify(normalizedApi.capabilities.sort())
comparisons.push({
field: 'capabilities',
manifestKey: 'capabilities',
apiKey: 'tool_call/reasoning',
match: !!capabilitiesMatch,
skip: false,
...(!capabilitiesMatch
? { manifest: manifest.capabilities, api: normalizedApi.capabilities }
: {}),
})
}
results.push({
modelId,
apiModelId,
vendor,
vendorKey,
vendorExists,
modelExists,
comparisons,
})
}
// Group by match status and field mismatches
const matched = results.filter(r => r.modelExists)
const unmatched = results.filter(r => !r.modelExists)
const withMismatches = matched.filter(r => r.comparisons.some(c => !c.match && !c.skip))
// Output matched models with all fields matching
console.log('Matched Models (no field mismatches):')
console.log('=======================================\n')
const perfectlyMatched = matched.filter(r => !r.comparisons.some(c => !c.match && !c.skip))
for (const r of perfectlyMatched) {
const modelDisplay = r.apiModelId !== r.modelId ? `${r.modelId} → ${r.apiModelId}` : r.modelId
console.log(` ${modelDisplay} | ${r.vendor} (${r.vendorKey})`)
}
// Output matched models with field mismatches
console.log(`\nMatched Models (with field mismatches):`)
console.log('========================================\n')
for (const r of withMismatches) {
const modelDisplay = r.apiModelId !== r.modelId ? `${r.modelId} → ${r.apiModelId}` : r.modelId
console.log(` ${modelDisplay} | ${r.vendor} (${r.vendorKey})`)
for (const comp of r.comparisons.filter(c => !c.match && !c.skip)) {
const mValue = JSON.stringify(comp.manifest)
const aValue = JSON.stringify(comp.api)
console.log(` ✗ ${comp.field} (manifest: ${mValue}, api: ${aValue})`)
}
}
// Output matched models with skipped fields (null in manifest)
console.log(`\nMatched Models (with skipped fields - null in manifest):`)
console.log('========================================================\n')
const withSkipped = matched.filter(r => r.comparisons.some(c => c.skip))
for (const r of withSkipped) {
const modelDisplay = r.apiModelId !== r.modelId ? `${r.modelId} → ${r.apiModelId}` : r.modelId
console.log(` ${modelDisplay} | ${r.vendor} (${r.vendorKey})`)
for (const comp of r.comparisons.filter(c => c.skip)) {
console.log(` → ${comp.field}: null (api: ${JSON.stringify(comp.api ?? 'undefined')})`)
}
}
// Output unmatched models
console.log(`\nUnmatched Models:`)
console.log('=================\n')
for (const r of unmatched) {
const vendorStatus = r.vendorExists ? '✓' : '✗'
const modelDisplay = r.apiModelId !== r.modelId ? `${r.modelId} → ${r.apiModelId}` : r.modelId
console.log(` ${modelDisplay} | ${r.vendor} (${r.vendorKey}) | Vendor: ${vendorStatus}`)
}
// Summary
const total = results.length
console.log(`\nSummary:`)
console.log(` Total models: ${total}`)
console.log(` Perfectly matched: ${perfectlyMatched.length}`)
console.log(` Matched with mismatches: ${withMismatches.length}`)
console.log(
` Matched with skipped fields: ${withSkipped.filter(r => !r.comparisons.some(c => !c.match && !c.skip)).length}`
)
console.log(` Not found in API: ${unmatched.length}`)
}
main().catch(console.error)