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 { assertParsed, cachedDocs } from '../model-facts.ts'
23import { classifyOpenAiCompat } from '../openai-compat.ts'
24import { fetchOpenApi, fetchText, sha256Text } from '../types.ts'
25import type {
26 ListModelsResult,
27 ProviderConfig,
28 ProviderSecrets,
29 SpecFetchResult,
30} from '../types.ts'
31
32export const AZURE_SPEC_URL =
33 'https://raw.githubusercontent.com/Azure/azure-rest-api-specs/main/specification/ai/data-plane/OpenAI.v1/azure-v1-v1-generated.json'
34
35/** A Learn article as markdown, parsed, with the hash of what was read. */
36function learnDoc<T>(
37 kv: KVNamespace | undefined,
38 url: string,
39 parse: (markdown: string) => Map<string, T>,
40): Promise<{ rows: Record<string, T>; hash: string }> {
41 return cachedDocs(kv, url, async () => {
42 const markdown = await fetchText(url, {
43 headers: { Accept: 'text/markdown' },
44 })
45 const rows = parse(markdown)
46 assertParsed(rows, `azure ${url}`)
47 return { rows: Object.fromEntries(rows), hash: await sha256Text(markdown) }
48 })
49}
50
51async function listModels(
52 _env: ProviderSecrets,
53 kv?: KVNamespace,
54): Promise<ListModelsResult> {
55 const [models, reasoning, prices, chatTokens] = await Promise.all([
56 learnDoc(kv, AZURE_MODELS_URL, parseAzureModels),
57 cachedDocs(kv, AZURE_REASONING_URL, async () => {
58 const markdown = await fetchText(AZURE_REASONING_URL, {
59 headers: { Accept: 'text/markdown' },
60 })
61 const matrix = parseAzureFeatureMatrix(markdown)
62 assertParsed(matrix, `azure ${AZURE_REASONING_URL}`)
63 return {
64 rows: Object.fromEntries(matrix),
65 rules: parseAzureEffortRules(markdown),
66 columnIds: [...matrix.keys()],
67 hash: await sha256Text(markdown),
68 }
69 }),
70 cachedDocs(kv, AZURE_PRICES_URL, fetchAzurePrices),
71 cachedDocs(kv, AZURE_SPEC_URL, async () => {
72 const { spec, hash } = await fetchOpenApi(AZURE_SPEC_URL)
73 return { maxTokensField: azureChatMaxTokensField(spec), hash }
74 }),
75 ])
76 const hashes = { models: models.hash, reasoning: reasoning.hash }
77 const columnIds = new Set(reasoning.columnIds)
78 const tabulated = Object.values(models.rows)
79 .filter((row) => row.tabulated)
80 .map((row) => row.rawId)
81 return {
82 models: Object.values(models.rows)
83 .filter(
84 (row) =>
85 row.tabulated || azureMetered(prices.meters, row.rawId, tabulated),
86 )
87 .map((row) => {
88 const info = azureModelInfo(row, reasoning.rows[row.rawId], hashes, {
89 rules: reasoning.rules,
90 columnIds,
91 chatMaxTokens: chatTokens.maxTokensField,
92 })
93 if (row.activity !== 'chat') return info
94 const priced = azureModelPricing(prices, row.rawId, row.version)
95 return {
96 ...info,
97 ...priced,
98 factSources: { ...info.factSources, ...priced.factSources },
99 }
100 }),
101 }
102}
103
104async function fetchSpec(_env: ProviderSecrets): Promise<SpecFetchResult> {
105 const { spec, hash } = await fetchOpenApi(AZURE_SPEC_URL)
106 return {
107 specs: [spec],
108 sources: [{ url: AZURE_SPEC_URL, hash }],
109 outputStrategy: 'post-200',
110 // Floating `main` URL: the document hash is the revision id.
111 specRevision: hash,
112 }
113}
114
115export const provider: ProviderConfig = {
116 id: 'azure',
117 // Azure OpenAI serves OpenAI models under OpenAI's ids; an id with no
118 // OpenAI row simply stays unlinked.
119 upstreamModelIdentity: (rawId) => ({
120 providerNamespace: 'openai',
121 rawId,
122 source: {
123 derivation: 'docs-derived',
124 sourceUrl: AZURE_MODELS_URL,
125 path: 'model id',
126 },
127 }),
128 displayName: 'Azure OpenAI',
129 specSourceUrl: AZURE_SPEC_URL,
130 modelsEndpoint: AZURE_MODELS_URL,
131 defaultDerivation: 'upstream-spec',
132 fetchSpec,
133 listModels,
134 classify: classifyOpenAiCompat,
135}