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

cerebras.ts

91 lines2.7 KB

1/**
2 * Cerebras Inference — first-party OpenAPI 3.1 spec at
3 * inference-docs.cerebras.ai/api-reference/openapi.yaml (public). The hinted
4 * chat-completions docs page is HTML; this is the machine-readable document
5 * published next to it. Models list requires CEREBRAS_API_KEY.
6 */
7import type { Activity } from '#/db/schema.ts'
8import { cerebrasModelFacts } from '../cerebras-pricing.ts'
9import { cerebrasChatFacts } from '../cerebras-reasoning.ts'
10import {
11 cerebrasModelActivity,
12 compatGenerationEndpointId,
13} from '../model-meta.ts'
14import { listOpenAiCompatibleModels } from '../openai-compat.ts'
15import { listsReasoning, overlayModelFacts } from '../reasoning-config.ts'
16import { fetchOpenApi } from '../types.ts'
17import type {
18 ListModelsResult,
19 ProviderConfig,
20 ProviderSecrets,
21 SpecFetchResult,
22} from '../types.ts'
23​
24const CEREBRAS_OPENAPI_URL =
25 'https://inference-docs.cerebras.ai/api-reference/openapi.yaml'
26const CEREBRAS_MODELS_URL = 'https://api.cerebras.ai/v1/models'
27​
28/**
29 * Cerebras is OpenAI-compatible chat. Completions is the same activity;
30 * models, files, batches, and dedicated-endpoint admin classify null.
31 */
32function classify(path: string): Activity | null {
33 const bare = path.startsWith('/v1/') ? path.slice(3) : path
34 if (bare === '/chat/completions' || bare === '/completions') return 'chat'
35 return null
37​
38async function fetchSpec(_env: ProviderSecrets): Promise<SpecFetchResult> {
39 const { spec, hash } = await fetchOpenApi(CEREBRAS_OPENAPI_URL)
40 return {
41 specs: [spec],
42 sources: [{ url: CEREBRAS_OPENAPI_URL, hash }],
43 outputStrategy: 'post-200',
44 }
46​
47async function listModels(
48 env: ProviderSecrets,
49 kv?: KVNamespace,
50): Promise<ListModelsResult> {
51 const listed = await listOpenAiCompatibleModels({
52 providerId: 'cerebras',
53 url: CEREBRAS_MODELS_URL,
54 env,
55 envVar: 'CEREBRAS_API_KEY',
56 activity: cerebrasModelActivity,
57 })
58 if (listed.models.length === 0) return listed
59 const [facts, chat] = await Promise.all([
60 cerebrasModelFacts(kv),
61 cerebrasChatFacts(kv),
62 ])
63 return {
64 ...listed,
65 models: listed.models.map((model) => {
66 const page = facts(model.rawId)
67 return overlayModelFacts(
68 model,
69 page,
70 chat(
71 model.rawId,
72 listsReasoning(page.capabilities ?? model.capabilities),
73 ),
74 )
75 }),
76 }
78​
79export const provider: ProviderConfig = {
80 id: 'cerebras',
81 displayName: 'Cerebras',
82 authEnvVar: 'CEREBRAS_API_KEY',
83 specSourceUrl: CEREBRAS_OPENAPI_URL,
84 modelsEndpoint: CEREBRAS_MODELS_URL,
85 defaultDerivation: 'upstream-spec',
86 fetchSpec,
87 listModels,
88 classify,
89 generationEndpointId: ({ activity }) =>
90 compatGenerationEndpointId(activity, 'v1/'),