File tree 2 files changed +44
-1
lines changed
2 files changed +44
-1
lines changed Original file line number Diff line number Diff line change @@ -485,7 +485,13 @@ export class AzureOpenAI extends OpenAI {
485
485
}
486
486
487
487
protected override async prepareOptions ( opts : Core . FinalRequestOptions < unknown > ) : Promise < void > {
488
- if ( opts . headers ?. [ 'Authorization' ] || opts . headers ?. [ 'api-key' ] ) {
488
+ /**
489
+ * The user should provide a bearer token provider if they want
490
+ * to use Azure AD authentication. The user shouldn't set the
491
+ * Authorization header manually because the header is overwritten
492
+ * with the Azure AD token if a bearer token provider is provided.
493
+ */
494
+ if ( opts . headers ?. [ 'api-key' ] ) {
489
495
return super . prepareOptions ( opts ) ;
490
496
}
491
497
const token = await this . _getAzureADToken ( ) ;
Original file line number Diff line number Diff line change @@ -254,6 +254,43 @@ describe('instantiate azure client', () => {
254
254
/ T h e ` a p i K e y ` a n d ` a z u r e A D T o k e n P r o v i d e r ` a r g u m e n t s a r e m u t u a l l y e x c l u s i v e ; o n l y o n e c a n b e p a s s e d a t a t i m e ./ ,
255
255
) ;
256
256
} ) ;
257
+
258
+ test ( 'AAD token is refreshed' , async ( ) => {
259
+ let fail = true ;
260
+ const testFetch = async ( url : RequestInfo , req : RequestInit | undefined ) : Promise < Response > => {
261
+ if ( fail ) {
262
+ fail = false ;
263
+ return new Response ( undefined , {
264
+ status : 429 ,
265
+ headers : {
266
+ 'Retry-After' : '0.1' ,
267
+ } ,
268
+ } ) ;
269
+ }
270
+ return new Response (
271
+ JSON . stringify ( { auth : ( req ?. headers as Record < string , string > ) [ 'authorization' ] } ) ,
272
+ { headers : { 'content-type' : 'application/json' } } ,
273
+ ) ;
274
+ } ;
275
+ let counter = 0 ;
276
+ async function azureADTokenProvider ( ) {
277
+ return `token-${ counter ++ } ` ;
278
+ }
279
+ const client = new AzureOpenAI ( {
280
+ baseURL : 'http://localhost:5000/' ,
281
+ azureADTokenProvider,
282
+ apiVersion,
283
+ fetch : testFetch ,
284
+ } ) ;
285
+ expect (
286
+ await client . chat . completions . create ( {
287
+ model,
288
+ messages : [ { role : 'system' , content : 'Hello' } ] ,
289
+ } ) ,
290
+ ) . toStrictEqual ( {
291
+ auth : 'Bearer token-1' ,
292
+ } ) ;
293
+ } ) ;
257
294
} ) ;
258
295
259
296
test ( 'with endpoint' , ( ) => {
You can’t perform that action at this time.
0 commit comments