Skip to content
Source
Sign inSign up
TanStack/schemas
Open History to see the last commit

voyage-pricing.ts

112 lines3.6 KB

1/**
2 * Voyage prices from the published pricing page (issue #122). Token rows
3 * are dollars per million tokens. Multimodal rows also name dollars per
4 * billion pixels. A model named twice at different rates is dropped. A
5 * page that prices nothing throws.
6 */
7import { compileTokenCard } from '@modelschemas/rate-card'
8import type { RateCard } from '@modelschemas/rate-card'
9​
10import { tagDocsFacts } from './fact-sources.ts'
11import { assertParsed, cachedDocs, markdownTableRows } from './model-facts.ts'
12import { fetchText, sha256Text } from './types.ts'
13import type { ModelInfo } from './types.ts'
14​
15export const VOYAGE_PRICING_URL = 'https://docs.voyageai.com/docs/pricing.md'
16​
17const MODEL_ID = /voyage-[a-z0-9.-]+/g
18const DOLLARS = /^\$(\d+(?:\.\d+)?)$/
19​
20export interface VoyageRates {
21 /** USD per token, from the "per million tokens" column. */
22 inputTokens: number
23 /** USD per pixel, when the row also prices pixels. */
24 pixels?: number
26​
27function headerIndex(cells: Array<string>, needle: string): number {
28 return cells.findIndex((cell) => cell.toLowerCase().includes(needle))
30​
31/** Model id → rates the pricing page states. Conflicting ids are absent. */
32export function parseVoyagePricing(markdown: string): Map<string, VoyageRates> {
33 const out = new Map<string, VoyageRates>()
34 const dropped = new Set<string>()
35 let million = -1
36 let pixels = -1
37 for (const cells of markdownTableRows(markdown)) {
38 const modelCol = headerIndex(cells, 'model')
39 const nextMillion = headerIndex(cells, 'per million')
40 if (modelCol >= 0 && nextMillion >= 0) {
41 million = nextMillion
42 pixels = headerIndex(cells, 'per billion pixels')
43 continue
44 }
45 if (million < 0) continue
46 const ids = (cells[0] ?? '').match(MODEL_ID) ?? []
47 const perMillion = (cells[million] ?? '').match(DOLLARS)?.[1]
48 if (ids.length === 0 || perMillion === undefined) continue
49 const rates: VoyageRates = { inputTokens: Number(perMillion) / 1e6 }
50 if (pixels >= 0) {
51 const perBillion = (cells[pixels] ?? '').match(DOLLARS)?.[1]
52 if (perBillion === undefined) continue
53 rates.pixels = Number(perBillion) / 1e9
54 }
55 for (const id of ids) {
56 const prior = out.get(id)
57 if (dropped.has(id)) continue
58 if (
59 prior &&
60 (prior.inputTokens !== rates.inputTokens ||
61 prior.pixels !== rates.pixels)
62 ) {
63 out.delete(id)
64 dropped.add(id)
65 continue
66 }
67 out.set(id, rates)
68 }
69 }
70 return out
72​
73function cardFor(
74 rates: VoyageRates,
75 source: RateCard['source'],
76): RateCard | null {
77 const levers: Record<string, number> = { input_tokens: rates.inputTokens }
78 if (rates.pixels !== undefined) levers.pixels = rates.pixels
79 return compileTokenCard(levers, [], source)
81​
82type PricedFacts = Pick<ModelInfo, 'pricing' | 'factSources'>
83​
84/** Card lookup by listed model id. */
85export async function voyageModelPricing(
86 kv?: KVNamespace,
87): Promise<(rawId: string) => PricedFacts> {
88 const doc = await cachedDocs(kv, VOYAGE_PRICING_URL, async () => {
89 const markdown = await fetchText(VOYAGE_PRICING_URL)
90 const parsed = parseVoyagePricing(markdown)
91 assertParsed(parsed, 'voyage pricing page')
92 return {
93 rates: Object.fromEntries(parsed),
94 hash: await sha256Text(markdown),
95 extractedAt: new Date().toISOString(),
96 }
97 })
98 return (rawId) => {
99 const row = doc.rates[rawId]
100 if (!row) return {}
101 const pricing = cardFor(row, {
102 url: VOYAGE_PRICING_URL,
103 hash: doc.hash,
104 extractedAt: doc.extractedAt,
105 })
106 if (!pricing) return {}
107 return {
108 pricing,
109 factSources: tagDocsFacts({ pricing }, VOYAGE_PRICING_URL, doc.hash),
110 }
111 }