@@ -7,8 +7,8 @@ author: mrbullwinkle
7
7
ms.author : mbullwin
8
8
ms.service : azure-ai-openai
9
9
ms.topic : include
10
- ms.date : 10/09 /2024
11
- ms.custom : passwordless-js , devex-track-javascript
10
+ ms.date : 10/28 /2024
11
+ ms.custom : passwordless-ts , devex-track-js
12
12
---
13
13
14
14
<a href =" /javascript/api/@azure/openai-assistants " target =" _blank " >Reference documentation</a > | <a href =" https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/openai/openai " target =" _blank " >Library source code</a > | <a href =" https://www.npmjs.com/package/@azure/openai-assistants " target =" _blank " >Package (npm)</a > |
@@ -17,15 +17,14 @@ ms.custom: passwordless-js, devex-track-javascript
17
17
18
18
- An Azure subscription - <a href =" https://azure.microsoft.com/free/cognitive-services " target =" _blank " >Create one for free</a >
19
19
- <a href =" https://nodejs.org/ " target =" _blank " >Node.js LTS or ESM support.</a >
20
- - [ TypeScript] ( https://www.typescriptlang.org/download/ ) installed globally
21
20
- [ Azure CLI] ( /cli/azure/install-azure-cli ) used for passwordless authentication in a local development environment, create the necessary context by signing in with the Azure CLI.
22
21
- An Azure OpenAI resource with a [ compatible model in a supported region] ( ../concepts/models.md#assistants-preview ) .
23
22
- We recommend reviewing the [ Responsible AI transparency note] ( /legal/cognitive-services/openai/transparency-note?context=%2Fazure%2Fai-services%2Fopenai%2Fcontext%2Fcontext&tabs=text ) and other [ Responsible AI resources] ( /legal/cognitive-services/openai/overview?context=%2Fazure%2Fai-services%2Fopenai%2Fcontext%2Fcontext ) to familiarize yourself with the capabilities and limitations of the Azure OpenAI Service.
24
23
- An Azure OpenAI resource with the ` gpt-4 (1106-preview) ` model deployed was used testing this example.
25
24
26
- ## Passwordless authentication is recommended
25
+ ## Microsoft Entra ID authentication is recommended
27
26
28
- For passwordless authentication, you need to
27
+ For _ keyless _ authentication, you need to
29
28
30
29
1 . Use the ` @azure/identity ` package.
31
30
1 . Assign the ` Cognitive Services User ` role to your user account. This can be done in the Azure portal under ** Access control (IAM)** > ** Add role assignment** .
@@ -70,22 +69,12 @@ For passwordless authentication, you need to
70
69
> [! CAUTION]
71
70
> To use the recommended keyless authentication with the SDK, make sure that the ` AZURE_OPENAI_API_KEY` environment variable isn' t set.
72
71
73
- #### [TypeScript keyless (Recommended) ](#tab/typescript -keyless)
72
+ #### [Microsoft Entra ID ](#tab/javascript -keyless)
74
73
75
74
[!INCLUDE [assistants-keyless-environment-variables](assistants-env-var-without-key.md)]
76
75
77
76
78
- #### [TypeScript with API key](#tab/typescript-key)
79
-
80
- [!INCLUDE [assistants-key-environment-variables](assistants-env-var-key.md)]
81
-
82
-
83
- #### [JavaScript keyless](#tab/javascript-keyless)
84
-
85
- [!INCLUDE [assistants-keyless-environment-variables](assistants-env-var-without-key.md)]
86
-
87
-
88
- #### [JavaScript with API key](#tab/javascript-key)
77
+ #### [API key](#tab/javascript-key)
89
78
90
79
[!INCLUDE [assistants-key-environment-variables](assistants-env-var-key.md)]
91
80
@@ -109,143 +98,10 @@ In our code we're going to specify the following values:
109
98
### Tools
110
99
111
100
An individual assistant can access up to 128 tools including `code interpreter`, and any custom tools you create via [functions](../how-to/assistant-functions.md).
112
-
113
- #### [TypeScript (Microsoft Entra ID)](#tab/typescript-keyless)
114
-
115
- 1. Create the `index.ts` file with the following code:
116
-
117
- ```typescript
118
- import { AzureOpenAI } from "openai";
119
- import {
120
- Assistant,
121
- AssistantCreateParams,
122
- AssistantTool,
123
- } from "openai/resources/beta/assistants";
124
- import { Message, MessagesPage } from "openai/resources/beta/threads/messages";
125
- import { Run } from "openai/resources/beta/threads/runs/runs";
126
- import { Thread } from "openai/resources/beta/threads/threads";
127
-
128
- // Add `Cognitive Services User` to identity for Azure OpenAI resource
129
- import {
130
- DefaultAzureCredential,
131
- getBearerTokenProvider,
132
- } from "@azure/identity";
133
-
134
- // Get environment variables
135
- const azureOpenAIEndpoint = process.env.AZURE_OPENAI_ENDPOINT as string;
136
- const azureOpenAIDeployment = process.env
137
- .AZURE_OPENAI_DEPLOYMENT_NAME as string;
138
- const openAIVersion = process.env.OPENAI_API_VERSION as string;
139
-
140
- // Check env variables
141
- if (!azureOpenAIEndpoint || !azureOpenAIDeployment || !openAIVersion) {
142
- throw new Error(
143
- "Please ensure to set AZURE_OPENAI_DEPLOYMENT_NAME and AZURE_OPENAI_ENDPOINT in your environment variables."
144
- );
145
- }
146
-
147
- // Get Azure SDK client
148
- const getClient = (): AzureOpenAI => {
149
- const credential = new DefaultAzureCredential();
150
- const scope = "https://cognitiveservices.azure.com/.default";
151
- const azureADTokenProvider = getBearerTokenProvider(credential, scope);
152
- const assistantsClient = new AzureOpenAI({
153
- endpoint: azureOpenAIEndpoint,
154
- apiVersion: openAIVersion,
155
- azureADTokenProvider,
156
- });
157
- return assistantsClient;
158
- };
159
-
160
- const assistantsClient = getClient();
161
-
162
- const options: AssistantCreateParams = {
163
- model: azureOpenAIDeployment, // Deployment name seen in Azure AI Studio
164
- name: "Math Tutor",
165
- instructions:
166
- "You are a personal math tutor. Write and run JavaScript code to answer math questions.",
167
- tools: [{ type: "code_interpreter" } as AssistantTool],
168
- };
169
- const role = "user";
170
- const message = "I need to solve the equation `3x + 11 = 14`. Can you help me?";
171
-
172
- // Create an assistant
173
- const assistantResponse: Assistant =
174
- await assistantsClient.beta.assistants.create(options);
175
- console.log(`Assistant created: ${JSON.stringify(assistantResponse)}`);
176
-
177
- // Create a thread
178
- const assistantThread: Thread = await assistantsClient.beta.threads.create({});
179
- console.log(`Thread created: ${JSON.stringify(assistantThread)}`);
180
-
181
- // Add a user question to the thread
182
- const threadResponse: Message =
183
- await assistantsClient.beta.threads.messages.create(assistantThread.id, {
184
- role,
185
- content: message,
186
- });
187
- console.log(`Message created: ${JSON.stringify(threadResponse)}`);
188
-
189
- // Run the thread and poll it until it is in a terminal state
190
- const runResponse: Run = await assistantsClient.beta.threads.runs.createAndPoll(
191
- assistantThread.id,
192
- {
193
- assistant_id: assistantResponse.id,
194
- },
195
- { pollIntervalMs: 500 }
196
- );
197
- console.log(`Run created: ${JSON.stringify(runResponse)}`);
198
-
199
- // Get the messages
200
- const runMessages: MessagesPage =
201
- await assistantsClient.beta.threads.messages.list(assistantThread.id);
202
- for await (const runMessageDatum of runMessages) {
203
- for (const item of runMessageDatum.content) {
204
- // types are: "image_file" or "text"
205
- if (item.type === "text") {
206
- console.log(`Message content: ${JSON.stringify(item.text?.value)}`);
207
- }
208
- }
209
- }
210
- ```
211
101
102
+ ## Create a new JavaScript application
212
103
213
-
214
- 1. Create the `tsconfig.json` file to transpile the TypeScript code and copy the following code for ECMAScript.
215
-
216
- ```json
217
- {
218
- "compilerOptions": {
219
- "module": "NodeNext",
220
- "target": "ES2022", // Supports top-level await
221
- "moduleResolution": "NodeNext",
222
- "skipLibCheck": true, // Avoid type errors from node_modules
223
- "strict": true // Enable strict type-checking options
224
- },
225
- "include": ["*.ts"]
226
- }
227
- ```
228
-
229
- 1. Transpile from TypeScript to JavaScript.
230
-
231
- ```shell
232
- tsc
233
- ```
234
-
235
- 1. Sign in to Azure with the following command:
236
-
237
- ```shell
238
- az login
239
- ```
240
-
241
- 1. Run the code with the following command:
242
-
243
- ```shell
244
- node index.js
245
- ```
246
-
247
-
248
- #### [JavaScript (Microsoft Entra ID)](#tab/javascript-keyless)
104
+ #### [Microsoft Entra ID](#tab/javascript-keyless)
249
105
250
106
1. Create the `index.js` file with the following code:
251
107
@@ -351,126 +207,8 @@ An individual assistant can access up to 128 tools including `code interpreter`,
351
207
```
352
208
353
209
354
- #### [TypeScript (API key)](#tab/typescript-key)
355
-
356
- 1. Create the `index.ts` file with the following code:
357
-
358
- ```typescript
359
- import { AzureOpenAI } from "openai";
360
- import {
361
- Assistant,
362
- AssistantCreateParams,
363
- AssistantTool,
364
- } from "openai/resources/beta/assistants";
365
- import { Message, MessagesPage } from "openai/resources/beta/threads/messages";
366
- import { Run } from "openai/resources/beta/threads/runs/runs";
367
- import { Thread } from "openai/resources/beta/threads/threads";
368
-
369
- // Get environment variables
370
- const azureOpenAIKey = process.env.AZURE_OPENAI_KEY as string;
371
- const azureOpenAIEndpoint = process.env.AZURE_OPENAI_ENDPOINT as string;
372
- const azureOpenAIDeployment = process.env
373
- .AZURE_OPENAI_DEPLOYMENT_NAME as string;
374
- const openAIVersion = process.env.OPENAI_API_VERSION as string;
375
-
376
- // Check env variables
377
- if (!azureOpenAIKey || !azureOpenAIEndpoint || !azureOpenAIDeployment || !openAIVersion) {
378
- throw new Error(
379
- "Please set AZURE_OPENAI_KEY and AZURE_OPENAI_ENDPOINT and AZURE_OPENAI_DEPLOYMENT_NAME in your environment variables."
380
- );
381
- }
382
-
383
- // Get Azure SDK client
384
- const getClient = (): AzureOpenAI => {
385
- const assistantsClient = new AzureOpenAI({
386
- endpoint: azureOpenAIEndpoint,
387
- apiVersion: openAIVersion,
388
- apiKey: azureOpenAIKey,
389
- });
390
- return assistantsClient;
391
- };
392
-
393
- const assistantsClient = getClient();
394
-
395
- const options: AssistantCreateParams = {
396
- model: azureOpenAIDeployment, // Deployment name seen in Azure AI Studio
397
- name: "Math Tutor",
398
- instructions:
399
- "You are a personal math tutor. Write and run JavaScript code to answer math questions.",
400
- tools: [{ type: "code_interpreter" } as AssistantTool],
401
- };
402
- const role = "user";
403
- const message = "I need to solve the equation `3x + 11 = 14`. Can you help me?";
404
-
405
- // Create an assistant
406
- const assistantResponse: Assistant =
407
- await assistantsClient.beta.assistants.create(options);
408
- console.log(`Assistant created: ${JSON.stringify(assistantResponse)}`);
409
-
410
- // Create a thread
411
- const assistantThread: Thread = await assistantsClient.beta.threads.create({});
412
- console.log(`Thread created: ${JSON.stringify(assistantThread)}`);
413
-
414
- // Add a user question to the thread
415
- const threadResponse: Message =
416
- await assistantsClient.beta.threads.messages.create(assistantThread.id, {
417
- role,
418
- content: message,
419
- });
420
- console.log(`Message created: ${JSON.stringify(threadResponse)}`);
421
-
422
- // Run the thread and poll it until it is in a terminal state
423
- const runResponse: Run = await assistantsClient.beta.threads.runs.createAndPoll(
424
- assistantThread.id,
425
- {
426
- assistant_id: assistantResponse.id,
427
- },
428
- { pollIntervalMs: 500 }
429
- );
430
- console.log(`Run created: ${JSON.stringify(runResponse)}`);
431
-
432
- // Get the messages
433
- const runMessages: MessagesPage =
434
- await assistantsClient.beta.threads.messages.list(assistantThread.id);
435
- for await (const runMessageDatum of runMessages) {
436
- for (const item of runMessageDatum.content) {
437
- // types are: "image_file" or "text"
438
- if (item.type === "text") {
439
- console.log(`Message content: ${JSON.stringify(item.text?.value)}`);
440
- }
441
- }
442
- }
443
- ```
444
-
445
-
446
- 1. Create the `tsconfig.json` file to transpile the TypeScript code and copy the following code for ECMAScript.
447
-
448
- ```json
449
- {
450
- "compilerOptions": {
451
- "module": "NodeNext",
452
- "target": "ES2022", // Supports top-level await
453
- "moduleResolution": "NodeNext",
454
- "skipLibCheck": true, // Avoid type errors from node_modules
455
- "strict": true // Enable strict type-checking options
456
- },
457
- "include": ["*.ts"]
458
- }
459
- ```
460
-
461
- 1. Transpile from TypeScript to JavaScript.
462
-
463
- ```shell
464
- tsc
465
- ```
466
-
467
- 1. Run the code with the following command:
468
-
469
- ```shell
470
- node index.js
471
- ```
472
210
473
- #### [JavaScript ( API key) ](#tab/javascript-key)
211
+ #### [API key](#tab/javascript-key)
474
212
475
213
1. Create the `index.js` file with the following code:
476
214
0 commit comments