@@ -29,127 +29,144 @@ For the recommended keyless authentication with Microsoft Entra ID, you need to:
29
29
- Install the [ Azure CLI] ( /cli/azure/install-azure-cli ) used for keyless authentication with Microsoft Entra ID.
30
30
- Assign the ` Cognitive Services User ` role to your user account. You can assign roles in the Azure portal under ** Access control (IAM)** > ** Add role assignment** .
31
31
32
- ## Retrieve resource information
32
+ ## Set up
33
+
34
+ 1 . Create a new folder ` chat-quickstart ` to contain the application and open Visual Studio Code in that folder with the following command:
33
35
34
- [ !INCLUDE [ resource authentication] ( resource-authentication.md )]
36
+ ``` shell
37
+ mkdir chat-quickstart && cd chat-quickstart
38
+ ```
35
39
36
- > [ !CAUTION]
37
- > To use the recommended keyless authentication with the SDK, make sure that the ` AZURE_OPENAI_API_KEY ` environment variable isn't set.
40
+ 1. Create the ` package.json` with the following command:
38
41
42
+ ` ` ` shell
43
+ npm init -y
44
+ ` ` `
39
45
40
- ## Create a Node application
46
+ 1. Install the OpenAI client library for JavaScript with:
41
47
42
- In a console window (such as cmd, PowerShell, or Bash), create a new directory for your app, and navigate to it.
48
+ ` ` ` console
49
+ npm install openai
50
+ ` ` `
43
51
44
- ## Install the client library
52
+ 1. For the ** recommended ** passwordless authentication:
45
53
46
- Install the required packages for JavaScript with npm from within the context of your new directory:
54
+ ` ` ` console
55
+ npm install @azure/identity
56
+ ` ` `
47
57
48
- ``` console
49
- npm install openai @azure/identity
50
- ```
51
-
52
- Your app's _ package.json_ file is updated with the dependencies.
58
+ # # Retrieve resource information
53
59
54
- ## Create a sample application
60
+ [ ! INCLUDE [resource authentication](resource-authentication.md)]
55
61
56
- Open a command prompt where you want the new project, and create a new file named ChatCompletion.js. Copy the following code into the ChatCompletion.js file.
62
+ > [! CAUTION]
63
+ > To use the recommended keyless authentication with the SDK, make sure that the ` AZURE_OPENAI_API_KEY` environment variable isn' t set.
57
64
65
+ ## Create a sample application
58
66
59
67
## [Microsoft Entra ID](#tab/keyless)
60
68
61
- ``` javascript
62
- const { AzureOpenAI } = require (" openai" );
63
- const {
64
- DefaultAzureCredential,
65
- getBearerTokenProvider
66
- } = require (" @azure/identity" );
67
-
68
- // You will need to set these environment variables or edit the following values
69
- const endpoint = process .env .AZURE_OPENAI_ENDPOINT || " Your endpoint" ;
70
- const apiVersion = process .env .OPENAI_API_VERSION || " 2024-05-01-preview" ;
71
- const deployment = process .env .AZURE_OPENAI_DEPLOYMENT_NAME || " gpt-4o" ; // This must match your deployment name.
72
-
73
-
74
- // keyless authentication
75
- const credential = new DefaultAzureCredential ();
76
- const scope = " https://cognitiveservices.azure.com/.default" ;
77
- const azureADTokenProvider = getBearerTokenProvider (credential, scope);
78
-
79
- async function main () {
80
-
81
- const client = new AzureOpenAI ({ endpoint, apiKey, azureADTokenProvider, deployment });
82
- const result = await client .chat .completions .create ({
83
- messages: [
84
- { role: " system" , content: " You are a helpful assistant." },
85
- { role: " user" , content: " Does Azure OpenAI support customer managed keys?" },
86
- { role: " assistant" , content: " Yes, customer managed keys are supported by Azure OpenAI?" },
87
- { role: " user" , content: " Do other Azure AI services support this too?" },
88
- ],
89
- model: " " ,
90
- });
91
-
92
- for (const choice of result .choices ) {
93
- console .log (choice .message );
94
- }
95
- }
96
-
97
- main ().catch ((err ) => {
98
- console .error (" The sample encountered an error:" , err);
99
- });
100
-
101
- module .exports = { main };
102
- ```
103
-
104
- Run the script with the following command:
105
-
106
- ``` cmd
107
- node.exe ChatCompletion.js
108
- ```
69
+ 1. Create the `index.js` file with the following code:
70
+
71
+ ```javascript
72
+ const { AzureOpenAI } = require("openai");
73
+ const {
74
+ DefaultAzureCredential,
75
+ getBearerTokenProvider
76
+ } = require("@azure/identity");
77
+
78
+ // You will need to set these environment variables or edit the following values
79
+ const endpoint = process.env.AZURE_OPENAI_ENDPOINT || "Your endpoint";
80
+ const apiVersion = process.env.OPENAI_API_VERSION || "2024-05-01-preview";
81
+ const deployment = process.env.AZURE_OPENAI_DEPLOYMENT_NAME || "gpt-4o"; //This must match your deployment name.
82
+
83
+ // keyless authentication
84
+ const credential = new DefaultAzureCredential();
85
+ const scope = "https://cognitiveservices.azure.com/.default";
86
+ const azureADTokenProvider = getBearerTokenProvider(credential, scope);
87
+
88
+ async function main() {
89
+
90
+ const client = new AzureOpenAI({ endpoint, apiKey, azureADTokenProvider, deployment });
91
+ const result = await client.chat.completions.create({
92
+ messages: [
93
+ { role: "system", content: "You are a helpful assistant." },
94
+ { role: "user", content: "Does Azure OpenAI support customer managed keys?" },
95
+ { role: "assistant", content: "Yes, customer managed keys are supported by Azure OpenAI?" },
96
+ { role: "user", content: "Do other Azure AI services support this too?" },
97
+ ],
98
+ model: "",
99
+ });
100
+
101
+ for (const choice of result.choices) {
102
+ console.log(choice.message);
103
+ }
104
+ }
105
+
106
+ main().catch((err) => {
107
+ console.error("The sample encountered an error:", err);
108
+ });
109
+
110
+ module.exports = { main };
111
+ ```
112
+
113
+ 1. Sign in to Azure with the following command:
114
+
115
+ ```shell
116
+ az login
117
+ ```
118
+
119
+ 1. Run the JavaScript file.
120
+
121
+ ```shell
122
+ node index.js
123
+ ```
109
124
110
125
111
126
## [API key](#tab/api-key)
112
127
113
- ``` javascript
114
- const { AzureOpenAI } = require (" openai" );
115
-
116
- // You will need to set these environment variables or edit the following values
117
- const endpoint = process .env .AZURE_OPENAI_ENDPOINT || " Your endpoint" ;
118
- const apiKey = process .env .AZURE_OPENAI_API_KEY || " Your API key" ;
119
- const apiVersion = process .env .OPENAI_API_VERSION || " 2024-05-01-preview" ;
120
- const deployment = process .env .AZURE_OPENAI_DEPLOYMENT_NAME || " gpt-4o" ; // This must match your deployment name.
121
-
122
- async function main () {
123
-
124
- const client = new AzureOpenAI ({ endpoint, apiKey, apiVersion, deployment });
125
- const result = await client .chat .completions .create ({
126
- messages: [
127
- { role: " system" , content: " You are a helpful assistant." },
128
- { role: " user" , content: " Does Azure OpenAI support customer managed keys?" },
129
- { role: " assistant" , content: " Yes, customer managed keys are supported by Azure OpenAI?" },
130
- { role: " user" , content: " Do other Azure AI services support this too?" },
131
- ],
132
- model: " " ,
133
- });
134
-
135
- for (const choice of result .choices ) {
136
- console .log (choice .message );
137
- }
138
- }
139
-
140
- main ().catch ((err ) => {
141
- console .error (" The sample encountered an error:" , err);
142
- });
143
-
144
- module .exports = { main };
145
- ```
146
-
147
- Run the script with the following command:
148
-
149
- ``` cmd
150
- node.exe ChatCompletion.js
151
- ```
152
-
128
+ 1. Create the `index.js` file with the following code:
129
+
130
+ ```javascript
131
+ const { AzureOpenAI } = require("openai");
132
+
133
+ // You will need to set these environment variables or edit the following values
134
+ const endpoint = process.env.AZURE_OPENAI_ENDPOINT || "Your endpoint";
135
+ const apiKey = process.env.AZURE_OPENAI_API_KEY || "Your API key";
136
+ const apiVersion = process.env.OPENAI_API_VERSION || "2024-05-01-preview";
137
+ const deployment = process.env.AZURE_OPENAI_DEPLOYMENT_NAME || "gpt-4o"; //This must match your deployment name.
138
+
139
+ async function main() {
140
+
141
+ const client = new AzureOpenAI({ endpoint, apiKey, apiVersion, deployment });
142
+ const result = await client.chat.completions.create({
143
+ messages: [
144
+ { role: "system", content: "You are a helpful assistant." },
145
+ { role: "user", content: "Does Azure OpenAI support customer managed keys?" },
146
+ { role: "assistant", content: "Yes, customer managed keys are supported by Azure OpenAI?" },
147
+ { role: "user", content: "Do other Azure AI services support this too?" },
148
+ ],
149
+ model: "",
150
+ });
151
+
152
+ for (const choice of result.choices) {
153
+ console.log(choice.message);
154
+ }
155
+ }
156
+
157
+ main().catch((err) => {
158
+ console.error("The sample encountered an error:", err);
159
+ });
160
+
161
+ module.exports = { main };
162
+ ```
163
+
164
+ 1. Run the JavaScript file.
165
+
166
+ ```shell
167
+ node index.js
168
+ ```
169
+
153
170
---
154
171
155
172
## Output
0 commit comments