Commit 722eff1 1 parent 8477a70 commit 722eff1 Copy full SHA for 722eff1
File tree 3 files changed +19
-6
lines changed
3 files changed +19
-6
lines changed Original file line number Diff line number Diff line change @@ -367,11 +367,18 @@ To use this library with [Azure OpenAI](https://learn.microsoft.com/en-us/azure/
367
367
class instead of the ` OpenAI ` class.
368
368
369
369
> [ !IMPORTANT]
370
- > The Azure API shape differs from the core API shape which means that the static types for responses / params
370
+ > The Azure API shape slightly differs from the core API shape which means that the static types for responses / params
371
371
> won't always be correct.
372
372
373
373
``` ts
374
- const openai = new AzureOpenAI ();
374
+ import { AzureOpenAI } from ' openai' ;
375
+ import { getBearerTokenProvider , DefaultAzureCredential } from ' @azure/identity' ;
376
+
377
+ const credential = new DefaultAzureCredential ();
378
+ const scope = ' https://cognitiveservices.azure.com/.default' ;
379
+ const azureADTokenProvider = getBearerTokenProvider (credential , scope );
380
+
381
+ const openai = new AzureOpenAI ({ azureADTokenProvider });
375
382
376
383
const result = await openai .chat .completions .create ({
377
384
model: ' gpt-4-1106-preview' ,
Original file line number Diff line number Diff line change 1
1
#!/usr/bin/env -S npm run tsn -T
2
2
3
3
import { AzureOpenAI } from 'openai' ;
4
+ import { getBearerTokenProvider , DefaultAzureCredential } from '@azure/identity' ;
4
5
5
6
// Corresponds to your Model deployment within your OpenAI resource, e.g. gpt-4-1106-preview
6
7
// Navigate to the Azure OpenAI Studio to deploy a model.
7
8
const deployment = 'gpt-4-1106-preview' ;
8
9
9
- // Make sure to set both AZURE_OPENAI_ENDPOINT with the endpoint of your Azure resource and AZURE_OPENAI_API_KEY with the API key.
10
- // You can find both information in the Azure Portal.
11
- const openai = new AzureOpenAI ( ) ;
10
+ const credential = new DefaultAzureCredential ( ) ;
11
+ const scope = 'https://cognitiveservices.azure.com/.default' ;
12
+ const azureADTokenProvider = getBearerTokenProvider ( credential , scope ) ;
13
+
14
+ // Make sure to set AZURE_OPENAI_ENDPOINT with the endpoint of your Azure resource.
15
+ // You can find it in the Azure Portal.
16
+ const openai = new AzureOpenAI ( { azureADTokenProvider } ) ;
12
17
13
18
async function main ( ) {
14
19
console . log ( 'Non-streaming:' ) ;
Original file line number Diff line number Diff line change 9
9
"express" : " ^4.18.2" ,
10
10
"next" : " ^14.1.1" ,
11
11
"openai" : " file:.." ,
12
- "zod-to-json-schema" : " ^3.21.4"
12
+ "zod-to-json-schema" : " ^3.21.4" ,
13
+ "@azure/identity" : " ^4.2.0"
13
14
},
14
15
"devDependencies" : {
15
16
"@types/body-parser" : " ^1.19.3" ,
You can’t perform that action at this time.
0 commit comments