Skip to content

Commit 1f3a5f1

Browse files
committed
feat: add support for Microsoft Entre ID
1 parent 936912c commit 1f3a5f1

File tree

5 files changed

+27
-586
lines changed

5 files changed

+27
-586
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,12 @@ Note that the documents are uploaded automatically when deploying the sample to
9494
- **Azure account**. If you're new to Azure, [get an Azure account for free](https://azure.microsoft.com/free) to get free Azure credits to get started. If you're a student, you can also get free credits with [Azure for Students](https://aka.ms/azureforstudents).
9595
- **Azure subscription with access enabled for the Azure OpenAI service**. You can request access with [this form](https://aka.ms/oaiapply).
9696

97+
### Setup and configure Microsoft Entra ID
98+
99+
This sample is using Microsoft Entra ID for authentication. To set up Microsoft Entra ID, follow these steps:
100+
101+
TODO
102+
97103
#### Deploy the sample
98104

99105
1. Open a terminal and navigate to the root of the project.

api/package-lock.json

Lines changed: 10 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
},
1010
"dependencies": {
1111
"@azure/functions": "^4.0.0",
12-
"@azure/identity": "^4.1.0",
12+
"@azure/identity": "^4.2.0",
1313
"dotenv": "^16.4.5",
14-
"openai": "^4.38.5"
14+
"openai": "^4.45.0"
1515
},
1616
"devDependencies": {
1717
"azure-functions-core-tools": "^4.x"

api/src/functions/assistant.js

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,43 +4,24 @@ dotenv.config();
44
const { app } = require("@azure/functions");
55
app.setup({ enableHttpStream: true });
66

7-
const OpenAI = require("openai");
7+
const { AzureOpenAI } = require("openai");
8+
const { DefaultAzureCredential, getBearerTokenProvider } = require("@azure/identity");
89

910
const {
1011
ASSISTANT_ID,
11-
AZURE_OPENAI_API_KEY,
12-
AZURE_OPENAI_API_VERSION,
13-
AZURE_OPENAI_ENDPOINT,
1412
AZURE_DEPLOYMENT_NAME,
15-
// TODO: uncomment this when https://github.com/openai/openai-node/pull/718 is merged
16-
// AZURE_CLIENT_ID,
17-
// AZURE_TENANT_ID,
1813
} = process.env;
1914

2015
// Important: Errors handlings are removed intentionally. If you are using this sample in production
21-
// please add proper errors handling.
16+
// please add proper error handling.
2217

2318
async function initAzureOpenAI() {
24-
// TODO: uncomment this when https://github.com/openai/openai-node/pull/718 is merged
25-
// console.log("Using Azure OpenAI (w/ AAD) ...");
26-
// const credential = new DefaultAzureCredential({
27-
// managedIdentityClientId: AZURE_CLIENT_ID,
28-
// tenantId: AZURE_TENANT_ID,
29-
// });
30-
// const { token } = await credential.getToken(
31-
// "https://cognitiveservices.azure.com/.default"
32-
// );
33-
// return new OpenAI({
34-
// baseURL: `${AZURE_OPENAI_ENDPOINT.replace(/\/+$/, "")}/openai`,
35-
// defaultQuery: { "api-version": AZURE_OPENAI_API_VERSION },
36-
// defaultHeaders: { Authorization: `Bearer ${token}` },
37-
// });
38-
return new OpenAI({
39-
apiKey: AZURE_OPENAI_API_KEY,
40-
baseURL: `${AZURE_OPENAI_ENDPOINT.replace(/\/+$/, "")}/openai`,
41-
defaultQuery: { "api-version": AZURE_OPENAI_API_VERSION },
42-
defaultHeaders: { "api-key": AZURE_OPENAI_API_KEY },
43-
});
19+
console.log("Using Azure OpenAI (w/ Microsoft Entra ID) ...");
20+
const credential = new DefaultAzureCredential();
21+
const azureADTokenProvider = getBearerTokenProvider(credential, "https://cognitiveservices.azure.com/.default");
22+
return new AzureOpenAI({
23+
azureADTokenProvider,
24+
});
4425
}
4526

4627
const assistantDefinition = {

0 commit comments

Comments
 (0)