|
1 | 1 | import { ProviderAPIConfig } from '../types';
|
2 |
| -import { getAccessTokenFromEntraId } from './utils'; |
| 2 | +import { getAccessTokenFromEntraId, getAzureManagedIdentityToken } from './utils'; |
3 | 3 |
|
4 | 4 | const AzureOpenAIAPIConfig: ProviderAPIConfig = {
|
5 | 5 | getBaseURL: ({ providerOptions }) => {
|
6 | 6 | const { resourceName, deploymentId } = providerOptions;
|
7 | 7 | return `https://${resourceName}.openai.azure.com/openai/deployments/${deploymentId}`;
|
8 | 8 | },
|
9 | 9 | headers: async ({ providerOptions, fn }) => {
|
| 10 | + const { apiKey, azureAuthMode } = providerOptions; |
| 11 | + |
| 12 | + if (azureAuthMode === 'entra') { |
| 13 | + const { azureEntraTenantId, azureEntraClientId, azureEntraClientSecret } = |
| 14 | + providerOptions; |
| 15 | + if (azureEntraTenantId && azureEntraClientId && azureEntraClientSecret) { |
| 16 | + const scope = 'https://cognitiveservices.azure.com/.default'; |
| 17 | + const accessToken = await getAccessTokenFromEntraId( |
| 18 | + azureEntraTenantId, |
| 19 | + azureEntraClientId, |
| 20 | + azureEntraClientSecret, |
| 21 | + scope |
| 22 | + ); |
| 23 | + return { |
| 24 | + Authorization: `Bearer ${accessToken}`, |
| 25 | + }; |
| 26 | + } |
| 27 | + } |
| 28 | + if (azureAuthMode === 'managed') { |
| 29 | + const { azureManagedClientId } = providerOptions; |
| 30 | + const resource = 'https://cognitiveservices.azure.com/'; |
| 31 | + const accessToken = await getAzureManagedIdentityToken( |
| 32 | + resource, |
| 33 | + azureManagedClientId |
| 34 | + ); |
| 35 | + return { |
| 36 | + Authorization: `Bearer ${accessToken}`, |
| 37 | + }; |
| 38 | + } |
10 | 39 | const headersObj: Record<string, string> = {
|
11 |
| - 'api-key': `${providerOptions.apiKey}`, |
| 40 | + 'api-key': `${apiKey}`, |
12 | 41 | };
|
13 | 42 | if (fn === 'createTranscription' || fn === 'createTranslation')
|
14 | 43 | headersObj['Content-Type'] = 'multipart/form-data';
|
15 |
| - const { azureEntraClientId, azureEntraClientSecret, azureEntraTenantId } = |
16 |
| - providerOptions; |
17 |
| - if (azureEntraClientId && azureEntraClientSecret && azureEntraTenantId) { |
18 |
| - const accessToken = await getAccessTokenFromEntraId( |
19 |
| - azureEntraTenantId, |
20 |
| - azureEntraClientId, |
21 |
| - azureEntraClientSecret, |
22 |
| - 'https://cognitiveservices.azure.com/decision/.default' |
23 |
| - ); |
24 |
| - headersObj['Authorization'] = `Bearer ${accessToken}`; |
25 |
| - } |
26 |
| - |
27 | 44 | return headersObj;
|
28 | 45 | },
|
29 | 46 | getEndpoint: ({ providerOptions, fn }) => {
|
|
0 commit comments