Open History to see the last commit
1/**
2 * Azure OpenAI. Models and their facts come from two Microsoft Learn
3 * articles, prices from the Azure Retail Prices API, and schemas from the
4 * v1 OpenAPI document in Azure's REST API specs repo. All four are public.
5 * Deployment lists on a resource need a key and are not called.
6 */
7import {
8 AZURE_MODELS_URL,
9 AZURE_REASONING_URL,
10 azureChatMaxTokensField,
11 azureModelInfo,
12 parseAzureEffortRules,
13 parseAzureFeatureMatrix,
14 parseAzureModels,
15} from '../azure-models.ts'
16import {
17 AZURE_PRICES_URL,
18 azureMetered,
19 azureModelPricing,
20 fetchAzurePrices,
21} from '../azure-pricing.ts'
22import {
23 assertParsed,
24 cachedDocs,
25 docsReport,
26 docsRun,
27 tryDocs,
28 unavailable,
29} from '../model-facts.ts'
30import { classifyOpenAiCompat } from '../openai-compat.ts'
31import { fetchOpenApi, fetchText, sha256Text } from '../types.ts'
32import type {
33 ListModelsResult,
34 ProviderConfig,
35 ProviderSecrets,
36 SpecFetchResult,
37} from '../types.ts'
38
39export const AZURE_SPEC_URL =
40 'https://raw.githubusercontent.com/Azure/azure-rest-api-specs/main/specification/ai/data-plane/OpenAI.v1/azure-v1-v1-generated.json'
41
42/** A Learn article as markdown, parsed, with the hash of what was read. */
43function learnDoc<T>(
44 kv: KVNamespace | undefined,
45 url: string,
46 parse: (markdown: string) => Map<string, T>,
47): Promise<{ rows: Record<string, T>; hash: string }> {
48 return cachedDocs(kv, url, async () => {
49 const markdown = await fetchText(url, {
50 headers: { Accept: 'text/markdown' },
51 })
52 const rows = parse(markdown)
53 assertParsed(rows, `azure ${url}`)
54 return { rows: Object.fromEntries(rows), hash: await sha256Text(markdown) }
55 })
56}
57
58async function listModels(
59 _env: ProviderSecrets,
60 kv?: KVNamespace,
61): Promise<ListModelsResult> {
62 const docs = docsRun()
63 const [models, reasoning, prices, chatTokens] = await Promise.all([
64 learnDoc(kv, AZURE_MODELS_URL, parseAzureModels),
65 cachedDocs(kv, AZURE_REASONING_URL, async () => {
66 const markdown = await fetchText(AZURE_REASONING_URL, {
67 headers: { Accept: 'text/markdown' },
68 })
69 const matrix = parseAzureFeatureMatrix(markdown)
70 assertParsed(matrix, `azure ${AZURE_REASONING_URL}`)
71 return {
72 rows: Object.fromEntries(matrix),
73 rules: parseAzureEffortRules(markdown),
74 columnIds: [...matrix.keys()],
75 hash: await sha256Text(markdown),
76 }
77 }),
78 // Each price-list page retries 429 and 5xx. When every attempt on a
79 // page fails the load throws, which keeps the stored cards. The models
80 // article stays outside this wrapper.
81 tryDocs(docs, AZURE_PRICES_URL, (cached) =>
82 cached(kv, AZURE_PRICES_URL, fetchAzurePrices),
83 ),
84 cachedDocs(kv, AZURE_SPEC_URL, async () => {
85 const { spec, hash } = await fetchOpenApi(AZURE_SPEC_URL)
86 return { maxTokensField: azureChatMaxTokensField(spec), hash }
87 }),
88 ])
89 const hashes = { models: models.hash, reasoning: reasoning.hash }
90 const columnIds = new Set(reasoning.columnIds)
91 const tabulated = Object.values(models.rows)
92 .filter((row) => row.tabulated)
93 .map((row) => row.rawId)
94 return {
95 models: Object.values(models.rows)
96 .filter(
97 (row) =>
98 row.tabulated ||
99 (prices !== null &&
100 azureMetered(prices.meters, row.rawId, tabulated)),
101 )
102 .map((row) => {
103 const info = azureModelInfo(row, reasoning.rows[row.rawId], hashes, {
104 rules: reasoning.rules,
105 columnIds,
106 chatMaxTokens: chatTokens.maxTokensField,
107 })
108 if (row.activity !== 'chat') return info
109 if (!prices) return { ...info, ...unavailable('pricing') }
110 const priced = azureModelPricing(prices, row.rawId, row.version)
111 return {
112 ...info,
113 ...priced,
114 factSources: { ...info.factSources, ...priced.factSources },
115 }
116 }),
117 docsFailures: docsReport(docs),
118 }
119}
120
121async function fetchSpec(_env: ProviderSecrets): Promise<SpecFetchResult> {
122 const { spec, hash } = await fetchOpenApi(AZURE_SPEC_URL)
123 return {
124 specs: [spec],
125 sources: [{ url: AZURE_SPEC_URL, hash }],
126 outputStrategy: 'post-200',
127 // Floating `main` URL: the document hash is the revision id.
128 specRevision: hash,
129 }
130}
131
132export const provider: ProviderConfig = {
133 id: 'azure',
134 // Azure OpenAI serves OpenAI models under OpenAI's ids; an id with no
135 // OpenAI row simply stays unlinked.
136 upstreamModelIdentity: (rawId) => ({
137 providerNamespace: 'openai',
138 rawId,
139 source: {
140 derivation: 'docs-derived',
141 sourceUrl: AZURE_MODELS_URL,
142 path: 'model id',
143 },
144 }),
145 displayName: 'Azure OpenAI',
146 specSourceUrl: AZURE_SPEC_URL,
147 modelsEndpoint: AZURE_MODELS_URL,
148 defaultDerivation: 'upstream-spec',
149 // Azure's v1 data plane: https://{resource}.openai.azure.com/openai/v1.
150 // Either an `api-key` header or a Microsoft Entra bearer token.
151 connect: {
152 servers: [
153 {
154 url: 'https://{resource}.openai.azure.com/openai/v1',
155 variables: {
156 resource: {
157 default: 'my-resource',
158 description: 'The Azure OpenAI resource name.',
159 },
160 },
161 },
162 ],
163 securitySchemes: {
164 apiKey: {
165 type: 'apiKey',
166 in: 'header',
167 name: 'api-key',
168 description: 'An Azure OpenAI resource key.',
169 },
170 bearerAuth: {
171 type: 'http',
172 scheme: 'bearer',
173 description:
174 'A Microsoft Entra token for https://cognitiveservices.azure.com/.default as `Authorization: Bearer <token>`.',
175 },
176 },
177 security: [{ apiKey: [] }, { bearerAuth: [] }],
178 },
179 fetchSpec,
180 listModels,
181 classify: classifyOpenAiCompat,
182}