Open History to see the last commit
1/**
2 * Cohere's models overview. `GET /v1/models` gives the context length and
3 * features but no output cap, and the chat spec's `max_tokens` points here
4 * for "the maximum output token limits for each model". The Command, North
5 * and Aya tables share the columns read below. Cells are matched whole: a
6 * row with a reworded status, modality or limit is dropped, never guessed.
7 */
8import { tagDocsFacts } from './fact-sources.ts'
9import { assertParsed, cachedDocs, markdownTableRows } from './model-facts.ts'
10import { fetchText, sha256Text } from './types.ts'
11import type { ModelInfo } from './types.ts'
12
13export const COHERE_MODELS_DOC_URL = 'https://docs.cohere.com/docs/models.md'
14
15const COLUMNS = [
16 'Model Name',
17 'Status',
18 'Modality',
19 'Maximum Output Tokens',
20 'Endpoints',
21] as const
22
23const INPUT: Record<string, string> = { Text: 'text', Images: 'image' }
24
25export interface CohereDocRow {
26 /** Status is exactly `Live`, not `Deprecated …` or `Retired …`. */
27 live: boolean
28 input: Array<string>
29 maxOutput: number
30}
31
32/** API id → the chat facts its table row states. */
33export function parseCohereModelTable(
34 markdown: string,
35): Map<string, CohereDocRow> {
36 const out = new Map<string, CohereDocRow>()
37 let header: Array<string> | null = null
38 for (const cells of markdownTableRows(markdown)) {
39 if (cells[0] === 'Model Name') {
40 header = COLUMNS.every((name) => cells.includes(name)) ? cells : null
41 continue
42 }
43 if (!header || cells.length !== header.length) continue
44 const cell = (name: (typeof COLUMNS)[number]) =>
45 cells[header?.indexOf(name) ?? -1] ?? ''
46 const id = /^`([a-z0-9][a-z0-9.-]*)`$/.exec(cell('Model Name'))?.[1]
47 const thousands = /^(\d+)k$/i.exec(cell('Maximum Output Tokens'))?.[1]
48 const input = cell('Modality')
49 .split(',')
50 .map((word) => INPUT[word.trim()])
51 // The chat endpoint is what makes the output text.
52 if (!id || !thousands || !/\[Chat\]\(/.test(cell('Endpoints'))) continue
53 if (!input.every((modality) => modality !== undefined)) continue
54 out.set(id, {
55 live: cell('Status') === 'Live',
56 input,
57 maxOutput: Number(thousands) * 1000,
58 })
59 }
60 return out
61}
62
63/** The parsed overview page with its hash, cached like every docs page. */
64export function cohereModelDocs(kv?: KVNamespace) {
65 return cachedDocs(kv, COHERE_MODELS_DOC_URL, async () => {
66 const markdown = await fetchText(COHERE_MODELS_DOC_URL, {
67 signal: AbortSignal.timeout(30_000),
68 })
69 const parsed = parseCohereModelTable(markdown)
70 assertParsed(parsed, 'cohere models page')
71 return {
72 rows: Object.fromEntries(parsed),
73 hash: await sha256Text(markdown),
74 }
75 })
76}
77
78type DocFacts = Pick<ModelInfo, 'maxOutput' | 'modalities' | 'factSources'>
79
80/** Facts by listed model id. An id the page has no row for gets nothing. */
81export async function cohereModelDocFacts(
82 kv?: KVNamespace,
83): Promise<(rawId: string) => DocFacts> {
84 const doc = await cohereModelDocs(kv)
85 return (rawId) => {
86 const row = doc.rows[rawId]
87 if (!row) return {}
88 const facts = {
89 maxOutput: row.maxOutput,
90 modalities: { input: row.input, output: ['text'] },
91 }
92 return {
93 ...facts,
94 factSources: tagDocsFacts(facts, COHERE_MODELS_DOC_URL, doc.hash),
95 }
96 }
97}