Open History to see the last commit
1/**
2 * SambaNova catalog facts the models list does not state.
3 * Modalities: the SambaCloud models table.
4 * Capability flags: ChatCompletionRequest in SambaNova's inference OpenAPI,
5 * minus fields the description says are unimplemented or only on some
6 * models. `tools` / `tool_choice` only for ids on the function-calling page.
7 * `seed` is dropped for a multimodal row. The compatibility page says seed
8 * is not supported on multi-modality models:
9 * https://docs.sambanova.ai/docs/en/features/openai-compatibility.md
10 */
11import { markdownSection, markdownTableRows } from './model-facts.ts'
12import type { FactSource, ModelInfo } from './types.ts'
13
14export const SAMBANOVA_MODELS_DOCS_URL =
15 'https://docs.sambanova.ai/docs/en/models/sambacloud-models.md'
16export const SAMBANOVA_SPEC_URL =
17 'https://raw.githubusercontent.com/sambanova/sambanova-inference-api-spec/refs/heads/main/openapi.documented.json'
18export const SAMBANOVA_TOOLS_DOCS_URL =
19 'https://docs.sambanova.ai/docs/en/features/function-calling.md'
20
21const MODALITY: Record<string, string> = {
22 text: 'text',
23 image: 'image',
24 video: 'video',
25 audio: 'audio',
26}
27
28/** Property name on ChatCompletionRequest → stored capability flag. */
29const PROPERTY_FLAGS: Record<string, string> = {
30 tools: 'tools',
31 tool_choice: 'tool_choice',
32 max_tokens: 'max_tokens',
33 max_completion_tokens: 'max_tokens',
34 temperature: 'temperature',
35 top_p: 'top_p',
36 top_k: 'top_k',
37 stop: 'stop',
38 seed: 'seed',
39 response_format: 'response_format',
40 reasoning_effort: 'reasoning_effort',
41}
42
43const FLAG_ORDER = [
44 'tools',
45 'tool_choice',
46 'max_tokens',
47 'temperature',
48 'top_p',
49 'top_k',
50 'stop',
51 'seed',
52 'response_format',
53 'structured_outputs',
54 'reasoning_effort',
55]
56
57export interface SambanovaModalities {
58 input: Array<string>
59 output: Array<string>
60}
61
62function isRecord(value: unknown): value is Record<string, unknown> {
63 return typeof value === 'object' && value !== null && !Array.isArray(value)
64}
65
66function cellText(cell: string): string {
67 return cell.replace(/[`*]/g, '').trim()
68}
69
70/**
71 * Both production and preview tables. Context length is not copied: the
72 * listing's `context_length` is the stored window (gemma's cell says 128k;
73 * the models API reports the 256k text window).
74 */
75export function parseSambanovaModelModalities(
76 markdown: string,
77): Map<string, SambanovaModalities> {
78 const rows = markdownTableRows(markdown)
79 const header = rows.find(
80 (cells) =>
81 cells.some((cell) => /model id/i.test(cellText(cell))) &&
82 cells.some((cell) => /modalit/i.test(cellText(cell))),
83 )
84 if (!header) {
85 throw new Error('sambanova models page: no modalities table')
86 }
87 const idCol = header.findIndex((cell) => /model id/i.test(cellText(cell)))
88 const modCol = header.findIndex((cell) => /modalit/i.test(cellText(cell)))
89 const out = new Map<string, SambanovaModalities>()
90 for (const cells of rows) {
91 if (cells === header) continue
92 const id = cellText(cells[idCol] ?? '')
93 if (!id || /model id/i.test(id)) continue
94 const parts = cellText(cells[modCol] ?? '')
95 .split(/[,/]/)
96 .map((part) => part.trim().toLowerCase())
97 .filter((part) => part.length > 0)
98 if (parts.length === 0) {
99 throw new Error(
100 `sambanova models page: ${id} has an empty modalities cell`,
101 )
102 }
103 const input = parts.map((part) => {
104 const modality = MODALITY[part]
105 if (!modality) {
106 throw new Error(
107 `sambanova models page: ${id} has unknown modality "${part}"`,
108 )
109 }
110 return modality
111 })
112 // Chat rows that omit text were a bad store (input image/audio only).
113 if (!input.includes('text')) {
114 throw new Error(`sambanova models page: ${id} modalities omit text`)
115 }
116 // The column is inputs (the gemma note). Chat completions return text.
117 out.set(id, { input, output: ['text'] })
118 }
119 return out
120}
121
122/** Ids under `## Supported models` on the function-calling page. */
123export function parseSambanovaToolModels(markdown: string): Set<string> {
124 const section = markdownSection(`\n${markdown}`, 'Supported models')
125 if (!section) {
126 throw new Error('sambanova function calling: no Supported models section')
127 }
128 const ids = [...section.matchAll(/^\* `([^`]+)`/gm)]
129 .map((match) => match[1])
130 .filter((id): id is string => typeof id === 'string' && id.length > 0)
131 if (ids.length === 0) {
132 throw new Error('sambanova function calling: no model ids')
133 }
134 return new Set(ids)
135}
136
137function descriptionOf(property: Record<string, unknown>): string {
138 return typeof property.description === 'string' ? property.description : ''
139}
140
141/** Implemented request fields. Unimplemented and "some models" stay out. */
142export function parseSambanovaChatFlags(spec: unknown): Array<string> {
143 if (!isRecord(spec) || !isRecord(spec.components)) {
144 throw new Error('sambanova chat spec: no components')
145 }
146 const schemas = spec.components.schemas
147 if (!isRecord(schemas) || !isRecord(schemas.ChatCompletionRequest)) {
148 throw new Error('sambanova chat spec: no ChatCompletionRequest')
149 }
150 const properties = schemas.ChatCompletionRequest.properties
151 if (!isRecord(properties)) {
152 throw new Error(
153 'sambanova chat spec: ChatCompletionRequest has no properties',
154 )
155 }
156 const flags = new Set<string>()
157 let responseFormat = ''
158 for (const [name, property] of Object.entries(properties)) {
159 if (!isRecord(property)) continue
160 const description = descriptionOf(property)
161 if (
162 /not currently implemented|only supported for some models/i.test(
163 description,
164 )
165 ) {
166 continue
167 }
168 const flag = PROPERTY_FLAGS[name]
169 if (flag) flags.add(flag)
170 if (name === 'response_format') responseFormat = description
171 }
172 if (/json_schema/i.test(responseFormat)) flags.add('structured_outputs')
173 if (flags.size === 0) {
174 throw new Error('sambanova chat spec: parsed 0 capability flags')
175 }
176 return FLAG_ORDER.filter((flag) => flags.has(flag))
177}
178
179export interface SambanovaDocsLoad {
180 modalities: {
181 byId: Record<string, SambanovaModalities>
182 hash: string
183 } | null
184 flags: { flags: Array<string>; hash: string } | null
185 tools: { ids: Array<string>; hash: string } | null
186}
187
188function docsSource(
189 url: string,
190 hash: string,
191 path: string,
192 derivation: FactSource['derivation'],
193): FactSource {
194 return { derivation, sourceUrl: url, sourceHash: hash, path }
195}
196
197/**
198 * One row's docs patch. A source that failed to load is `unavailable`
199 * (keep the stored fact). A model the models table does not name keeps
200 * null modalities. `seed` is omitted unless the table says the row is
201 * text-only.
202 */
203export function sambanovaDocsPatch(
204 rawId: string,
205 loaded: SambanovaDocsLoad,
206): Partial<ModelInfo> {
207 const absent: NonNullable<ModelInfo['absent']> = {}
208 const patch: Partial<ModelInfo> = {}
209 const factSources: NonNullable<ModelInfo['factSources']> = {}
210
211 if (!loaded.modalities) {
212 absent.modalities = 'unavailable'
213 } else {
214 const modalities = loaded.modalities.byId[rawId]
215 if (modalities) {
216 patch.modalities = modalities
217 factSources.modalities = docsSource(
218 SAMBANOVA_MODELS_DOCS_URL,
219 loaded.modalities.hash,
220 'Supported modalities',
221 'docs-derived',
222 )
223 }
224 }
225
226 if (!loaded.flags || !loaded.tools) {
227 absent.capabilities = 'unavailable'
228 } else {
229 const toolIds = new Set(loaded.tools.ids)
230 const modalities = loaded.modalities?.byId[rawId]
231 const textOnly =
232 modalities !== undefined &&
233 modalities.input.every((modality) => modality === 'text')
234 const capabilities = loaded.flags.flags.filter((flag) => {
235 if ((flag === 'tools' || flag === 'tool_choice') && !toolIds.has(rawId)) {
236 return false
237 }
238 if (flag === 'seed' && !textOnly) return false
239 return true
240 })
241 const capSources: Record<string, FactSource> = {}
242 for (const flag of capabilities) {
243 if (flag === 'tools' || flag === 'tool_choice') {
244 capSources[flag] = docsSource(
245 SAMBANOVA_TOOLS_DOCS_URL,
246 loaded.tools.hash,
247 'Supported models',
248 'docs-derived',
249 )
250 } else {
251 capSources[flag] = docsSource(
252 SAMBANOVA_SPEC_URL,
253 loaded.flags.hash,
254 flag === 'structured_outputs'
255 ? 'ChatCompletionRequest.properties.response_format'
256 : `ChatCompletionRequest.properties.${flag}`,
257 'upstream-spec',
258 )
259 }
260 }
261 patch.capabilities = capabilities
262 patch.exactCapabilities = true
263 factSources.capabilities = capSources
264 }
265
266 if (Object.keys(factSources).length > 0) patch.factSources = factSources
267 if (Object.keys(absent).length > 0) patch.absent = absent
268 return patch
269}