2025-09-15 10:04:47 +08:00
"use strict" ;
Object . defineProperty ( exports , "__esModule" , { value : true } ) ;
exports . AzureOpenAI = void 0 ;
const tslib _1 = require ( "./internal/tslib.js" ) ;
2025-11-21 15:33:03 +08:00
const headers _1 = require ( "./internal/headers.js" ) ;
2025-09-15 10:04:47 +08:00
const Errors = tslib _1 . _ _importStar ( require ( "./error.js" ) ) ;
const utils _1 = require ( "./internal/utils.js" ) ;
const client _1 = require ( "./client.js" ) ;
/** API Client for interfacing with the Azure OpenAI API. */
class AzureOpenAI extends client _1 . OpenAI {
/ * *
* API Client for interfacing with the Azure OpenAI API .
*
* @ param { string | undefined } [ opts . apiVersion = process . env [ 'OPENAI_API_VERSION' ] ? ? undefined ]
* @ param { string | undefined } [ opts . endpoint = process . env [ 'AZURE_OPENAI_ENDPOINT' ] ? ? undefined ] - Your Azure endpoint , including the resource , e . g . ` https://example-resource.azure.openai.com/ `
* @ param { string | undefined } [ opts . apiKey = process . env [ 'AZURE_OPENAI_API_KEY' ] ? ? undefined ]
* @ param { string | undefined } opts . deployment - A model deployment , if given , sets the base client URL to include ` /deployments/{deployment} ` .
* @ param { string | null | undefined } [ opts . organization = process . env [ 'OPENAI_ORG_ID' ] ? ? null ]
* @ param { string } [ opts . baseURL = process . env [ 'OPENAI_BASE_URL' ] ] - Sets the base URL for the API , e . g . ` https://example-resource.azure.openai.com/openai/ ` .
* @ param { number } [ opts . timeout = 10 minutes ] - The maximum amount of time ( in milliseconds ) the client will wait for a response before timing out .
* @ param { number } [ opts . httpAgent ] - An HTTP agent used to manage HTTP ( s ) connections .
* @ param { Fetch } [ opts . fetch ] - Specify a custom ` fetch ` function implementation .
* @ param { number } [ opts . maxRetries = 2 ] - The maximum number of times the client will retry a request .
* @ param { Headers } opts . defaultHeaders - Default headers to include with every request to the API .
* @ param { DefaultQuery } opts . defaultQuery - Default query parameters to include with every request to the API .
* @ param { boolean } [ opts . dangerouslyAllowBrowser = false ] - By default , client - side use of this library is not allowed , as it risks exposing your secret API credentials to attackers .
* /
constructor ( { baseURL = ( 0 , utils _1 . readEnv ) ( 'OPENAI_BASE_URL' ) , apiKey = ( 0 , utils _1 . readEnv ) ( 'AZURE_OPENAI_API_KEY' ) , apiVersion = ( 0 , utils _1 . readEnv ) ( 'OPENAI_API_VERSION' ) , endpoint , deployment , azureADTokenProvider , dangerouslyAllowBrowser , ... opts } = { } ) {
if ( ! apiVersion ) {
throw new Errors . OpenAIError ( "The OPENAI_API_VERSION environment variable is missing or empty; either provide it, or instantiate the AzureOpenAI client with an apiVersion option, like new AzureOpenAI({ apiVersion: 'My API Version' })." ) ;
}
if ( typeof azureADTokenProvider === 'function' ) {
dangerouslyAllowBrowser = true ;
}
if ( ! azureADTokenProvider && ! apiKey ) {
throw new Errors . OpenAIError ( 'Missing credentials. Please pass one of `apiKey` and `azureADTokenProvider`, or set the `AZURE_OPENAI_API_KEY` environment variable.' ) ;
}
if ( azureADTokenProvider && apiKey ) {
throw new Errors . OpenAIError ( 'The `apiKey` and `azureADTokenProvider` arguments are mutually exclusive; only one can be passed at a time.' ) ;
}
opts . defaultQuery = { ... opts . defaultQuery , 'api-version' : apiVersion } ;
if ( ! baseURL ) {
if ( ! endpoint ) {
endpoint = process . env [ 'AZURE_OPENAI_ENDPOINT' ] ;
}
if ( ! endpoint ) {
throw new Errors . OpenAIError ( 'Must provide one of the `baseURL` or `endpoint` arguments, or the `AZURE_OPENAI_ENDPOINT` environment variable' ) ;
}
baseURL = ` ${ endpoint } /openai ` ;
}
else {
if ( endpoint ) {
throw new Errors . OpenAIError ( 'baseURL and endpoint are mutually exclusive' ) ;
}
}
super ( {
2025-11-21 15:33:03 +08:00
apiKey : azureADTokenProvider ? ? apiKey ,
2025-09-15 10:04:47 +08:00
baseURL ,
... opts ,
... ( dangerouslyAllowBrowser !== undefined ? { dangerouslyAllowBrowser } : { } ) ,
} ) ;
this . apiVersion = '' ;
this . apiVersion = apiVersion ;
this . deploymentName = deployment ;
}
async buildRequest ( options , props = { } ) {
if ( _deployments _endpoints . has ( options . path ) && options . method === 'post' && options . body !== undefined ) {
if ( ! ( 0 , utils _1 . isObj ) ( options . body ) ) {
throw new Error ( 'Expected request body to be an object' ) ;
}
const model = this . deploymentName || options . body [ 'model' ] || options . _ _metadata ? . [ 'model' ] ;
if ( model !== undefined && ! this . baseURL . includes ( '/deployments' ) ) {
options . path = ` /deployments/ ${ model } ${ options . path } ` ;
}
}
return super . buildRequest ( options , props ) ;
}
async authHeaders ( opts ) {
2025-11-21 15:33:03 +08:00
if ( typeof this . _options . apiKey === 'string' ) {
return ( 0 , headers _1 . buildHeaders ) ( [ { 'api-key' : this . apiKey } ] ) ;
2025-09-15 10:04:47 +08:00
}
2025-11-21 15:33:03 +08:00
return super . authHeaders ( opts ) ;
2025-09-15 10:04:47 +08:00
}
}
exports . AzureOpenAI = AzureOpenAI ;
const _deployments _endpoints = new Set ( [
'/completions' ,
'/chat/completions' ,
'/embeddings' ,
'/audio/transcriptions' ,
'/audio/translations' ,
'/audio/speech' ,
'/images/generations' ,
'/batches' ,
'/images/edits' ,
] ) ;
//# sourceMappingURL=azure.js.map