Skip to content

Commit 722eff1

Browse files
authored
docs(azure): update example and readme to use Entra ID (#857)
1 parent 8477a70 commit 722eff1

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed

README.md

+9-2
Original file line numberDiff line numberDiff line change
@@ -367,11 +367,18 @@ To use this library with [Azure OpenAI](https://learn.microsoft.com/en-us/azure/
367367
class instead of the `OpenAI` class.
368368

369369
> [!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
371371
> won't always be correct.
372372
373373
```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 });
375382

376383
const result = await openai.chat.completions.create({
377384
model: 'gpt-4-1106-preview',

examples/azure.ts

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
#!/usr/bin/env -S npm run tsn -T
22

33
import { AzureOpenAI } from 'openai';
4+
import { getBearerTokenProvider, DefaultAzureCredential } from '@azure/identity';
45

56
// Corresponds to your Model deployment within your OpenAI resource, e.g. gpt-4-1106-preview
67
// Navigate to the Azure OpenAI Studio to deploy a model.
78
const deployment = 'gpt-4-1106-preview';
89

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 });
1217

1318
async function main() {
1419
console.log('Non-streaming:');

examples/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
"express": "^4.18.2",
1010
"next": "^14.1.1",
1111
"openai": "file:..",
12-
"zod-to-json-schema": "^3.21.4"
12+
"zod-to-json-schema": "^3.21.4",
13+
"@azure/identity": "^4.2.0"
1314
},
1415
"devDependencies": {
1516
"@types/body-parser": "^1.19.3",

0 commit comments

Comments
 (0)