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