@@ -110,7 +110,7 @@ In our code we're going to specify the following values:
110
110
111
111
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
112
113
- #### [TypeScript keyless (Recommended )](#tab/typescript-keyless)
113
+ #### [TypeScript (Microsoft Entra ID )](#tab/typescript-keyless)
114
114
115
115
1. Create the `index.ts` file with the following code:
116
116
@@ -244,76 +244,78 @@ An individual assistant can access up to 128 tools including `code interpreter`,
244
244
node index.js
245
245
```
246
246
247
- #### [TypeScript with API key](#tab/typescript-key)
248
247
249
- 1. Create the `index.ts` file with the following code:
248
+ #### [JavaScript (Microsoft Entra ID)](#tab/javascript-keyless)
250
249
251
- ```typescript
252
- import { AzureOpenAI } from "openai";
253
- import {
254
- Assistant,
255
- AssistantCreateParams,
256
- AssistantTool,
257
- } from "openai/resources/beta/assistants";
258
- import { Message, MessagesPage } from "openai/resources/beta/threads/messages";
259
- import { Run } from "openai/resources/beta/threads/runs/runs";
260
- import { Thread } from "openai/resources/beta/threads/threads";
250
+ 1. Create the `index.js` file with the following code:
251
+
252
+ ```javascript
253
+ const { AzureOpenAI } = require("openai");
254
+ const {
255
+ DefaultAzureCredential,
256
+ getBearerTokenProvider,
257
+ } = require("@azure/identity");
261
258
262
259
// Get environment variables
263
- const azureOpenAIKey = process.env.AZURE_OPENAI_KEY as string;
264
- const azureOpenAIEndpoint = process.env.AZURE_OPENAI_ENDPOINT as string;
265
- const azureOpenAIDeployment = process.env
266
- .AZURE_OPENAI_DEPLOYMENT_NAME as string;
267
- const openAIVersion = process.env.OPENAI_API_VERSION as string;
260
+ const azureOpenAIEndpoint = process.env.AZURE_OPENAI_ENDPOINT;
261
+ const azureOpenAIDeployment = process.env.AZURE_OPENAI_DEPLOYMENT_NAME;
262
+ const azureOpenAIVersion = process.env.OPENAI_API_VERSION;
268
263
269
264
// Check env variables
270
- if (!azureOpenAIKey || ! azureOpenAIEndpoint || !azureOpenAIDeployment || !openAIVersion ) {
265
+ if (!azureOpenAIEndpoint || !azureOpenAIDeployment || !azureOpenAIVersion ) {
271
266
throw new Error(
272
- "Please set AZURE_OPENAI_KEY and AZURE_OPENAI_ENDPOINT and AZURE_OPENAI_DEPLOYMENT_NAME in your environment variables."
267
+ "Please ensure to set AZURE_OPENAI_DEPLOYMENT_NAME and AZURE_OPENAI_ENDPOINT in your environment variables."
273
268
);
274
269
}
275
270
276
271
// Get Azure SDK client
277
- const getClient = (): AzureOpenAI => {
272
+ const getClient = () => {
273
+ const credential = new DefaultAzureCredential();
274
+ const scope = "https://cognitiveservices.azure.com/.default";
275
+ const azureADTokenProvider = getBearerTokenProvider(credential, scope);
276
+
278
277
const assistantsClient = new AzureOpenAI({
279
278
endpoint: azureOpenAIEndpoint,
280
- apiVersion: openAIVersion ,
281
- apiKey: azureOpenAIKey ,
279
+ apiVersion: azureOpenAIVersion ,
280
+ azureADTokenProvider ,
282
281
});
283
282
return assistantsClient;
284
283
};
285
284
286
285
const assistantsClient = getClient();
287
286
288
- const options: AssistantCreateParams = {
287
+ const options = {
289
288
model: azureOpenAIDeployment, // Deployment name seen in Azure AI Studio
290
289
name: "Math Tutor",
291
290
instructions:
292
291
"You are a personal math tutor. Write and run JavaScript code to answer math questions.",
293
- tools: [{ type: "code_interpreter" } as AssistantTool ],
292
+ tools: [{ type: "code_interpreter" }],
294
293
};
295
294
const role = "user";
296
295
const message = "I need to solve the equation `3x + 11 = 14`. Can you help me?";
297
296
298
297
// Create an assistant
299
- const assistantResponse: Assistant =
300
- await assistantsClient.beta.assistants.create(options);
298
+ const assistantResponse = await assistantsClient.beta.assistants.create(
299
+ options
300
+ );
301
301
console.log(`Assistant created: ${JSON.stringify(assistantResponse)}`);
302
302
303
303
// Create a thread
304
- const assistantThread: Thread = await assistantsClient.beta.threads.create({});
304
+ const assistantThread = await assistantsClient.beta.threads.create({});
305
305
console.log(`Thread created: ${JSON.stringify(assistantThread)}`);
306
306
307
307
// Add a user question to the thread
308
- const threadResponse: Message =
309
- await assistantsClient.beta.threads.messages.create(assistantThread.id, {
308
+ const threadResponse = await assistantsClient.beta.threads.messages.create(
309
+ assistantThread.id,
310
+ {
310
311
role,
311
312
content: message,
312
- });
313
+ }
314
+ );
313
315
console.log(`Message created: ${JSON.stringify(threadResponse)}`);
314
316
315
317
// Run the thread and poll it until it is in a terminal state
316
- const runResponse: Run = await assistantsClient.beta.threads.runs.createAndPoll(
318
+ const runResponse = await assistantsClient.beta.threads.runs.createAndPoll(
317
319
assistantThread.id,
318
320
{
319
321
assistant_id: assistantResponse.id,
@@ -323,8 +325,9 @@ An individual assistant can access up to 128 tools including `code interpreter`,
323
325
console.log(`Run created: ${JSON.stringify(runResponse)}`);
324
326
325
327
// Get the messages
326
- const runMessages: MessagesPage =
327
- await assistantsClient.beta.threads.messages.list(assistantThread.id);
328
+ const runMessages = await assistantsClient.beta.threads.messages.list(
329
+ assistantThread.id
330
+ );
328
331
for await (const runMessageDatum of runMessages) {
329
332
for (const item of runMessageDatum.content) {
330
333
// types are: "image_file" or "text"
@@ -334,107 +337,90 @@ An individual assistant can access up to 128 tools including `code interpreter`,
334
337
}
335
338
}
336
339
```
337
-
338
340
339
- 1. Create the `tsconfig.json` file to transpile the TypeScript code and copy the following code for ECMAScript.
340
-
341
- ```json
342
- {
343
- "compilerOptions": {
344
- "module": "NodeNext",
345
- "target": "ES2022", // Supports top-level await
346
- "moduleResolution": "NodeNext",
347
- "skipLibCheck": true, // Avoid type errors from node_modules
348
- "strict": true // Enable strict type-checking options
349
- },
350
- "include": ["*.ts"]
351
- }
352
- ```
353
-
354
- 1. Transpile from TypeScript to JavaScript.
341
+ 1. Sign in to Azure with the following command:
355
342
356
343
```shell
357
- tsc
344
+ az login
358
345
```
359
346
360
- 1. Run the code with the following command:
347
+ 1. Run the JavaScript file.
361
348
362
349
```shell
363
350
node index.js
364
351
```
365
352
366
- #### [JavaScript keyless](#tab/javascript-keyless)
367
353
368
- 1. Create the `index.js` file with the following code:
354
+ #### [TypeScript with API key](#tab/typescript-key)
369
355
370
- ```nodejs
356
+ 1. Create the `index.ts` file with the following code:
357
+
358
+ ```typescript
371
359
import { AzureOpenAI } from "openai";
372
-
373
- // Add `Cognitive Services User` to identity for Azure OpenAI resource
374
360
import {
375
- DefaultAzureCredential,
376
- getBearerTokenProvider,
377
- } from "@azure/identity";
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";
378
368
379
369
// Get environment variables
380
- const azureOpenAIEndpoint = process.env.AZURE_OPENAI_ENDPOINT;
381
- const azureOpenAIDeployment = process.env.AZURE_OPENAI_DEPLOYMENT_NAME;
382
- const azureOpenAIVersion = process.env.OPENAI_API_VERSION;
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;
383
375
384
376
// Check env variables
385
- if (!azureOpenAIEndpoint || !azureOpenAIDeployment || !azureOpenAIVersion ) {
377
+ if (!azureOpenAIKey || ! azureOpenAIEndpoint || !azureOpenAIDeployment || !openAIVersion ) {
386
378
throw new Error(
387
- "Please ensure to set AZURE_OPENAI_DEPLOYMENT_NAME and AZURE_OPENAI_ENDPOINT in your environment variables."
379
+ "Please set AZURE_OPENAI_KEY and AZURE_OPENAI_ENDPOINT and AZURE_OPENAI_DEPLOYMENT_NAME in your environment variables."
388
380
);
389
381
}
390
382
391
383
// Get Azure SDK client
392
- const getClient = () => {
393
- const credential = new DefaultAzureCredential();
394
- const scope = "https://cognitiveservices.azure.com/.default";
395
- const azureADTokenProvider = getBearerTokenProvider(credential, scope);
384
+ const getClient = (): AzureOpenAI => {
396
385
const assistantsClient = new AzureOpenAI({
397
386
endpoint: azureOpenAIEndpoint,
398
- apiVersion: azureOpenAIVersion ,
399
- azureADTokenProvider ,
387
+ apiVersion: openAIVersion ,
388
+ apiKey: azureOpenAIKey ,
400
389
});
401
390
return assistantsClient;
402
391
};
403
392
404
393
const assistantsClient = getClient();
405
394
406
- const options = {
395
+ const options: AssistantCreateParams = {
407
396
model: azureOpenAIDeployment, // Deployment name seen in Azure AI Studio
408
397
name: "Math Tutor",
409
398
instructions:
410
399
"You are a personal math tutor. Write and run JavaScript code to answer math questions.",
411
- tools: [{ type: "code_interpreter" }],
400
+ tools: [{ type: "code_interpreter" } as AssistantTool ],
412
401
};
413
402
const role = "user";
414
403
const message = "I need to solve the equation `3x + 11 = 14`. Can you help me?";
415
404
416
405
// Create an assistant
417
- const assistantResponse = await assistantsClient.beta.assistants.create(
418
- options
419
- );
406
+ const assistantResponse: Assistant =
407
+ await assistantsClient.beta.assistants.create(options);
420
408
console.log(`Assistant created: ${JSON.stringify(assistantResponse)}`);
421
409
422
410
// Create a thread
423
- const assistantThread = await assistantsClient.beta.threads.create({});
411
+ const assistantThread: Thread = await assistantsClient.beta.threads.create({});
424
412
console.log(`Thread created: ${JSON.stringify(assistantThread)}`);
425
413
426
414
// Add a user question to the thread
427
- const threadResponse = await assistantsClient.beta.threads.messages.create(
428
- assistantThread.id,
429
- {
415
+ const threadResponse: Message =
416
+ await assistantsClient.beta.threads.messages.create(assistantThread.id, {
430
417
role,
431
418
content: message,
432
- }
433
- );
419
+ });
434
420
console.log(`Message created: ${JSON.stringify(threadResponse)}`);
435
421
436
422
// Run the thread and poll it until it is in a terminal state
437
- const runResponse = await assistantsClient.beta.threads.runs.createAndPoll(
423
+ const runResponse: Run = await assistantsClient.beta.threads.runs.createAndPoll(
438
424
assistantThread.id,
439
425
{
440
426
assistant_id: assistantResponse.id,
@@ -444,9 +430,8 @@ An individual assistant can access up to 128 tools including `code interpreter`,
444
430
console.log(`Run created: ${JSON.stringify(runResponse)}`);
445
431
446
432
// Get the messages
447
- const runMessages = await assistantsClient.beta.threads.messages.list(
448
- assistantThread.id
449
- );
433
+ const runMessages: MessagesPage =
434
+ await assistantsClient.beta.threads.messages.list(assistantThread.id);
450
435
for await (const runMessageDatum of runMessages) {
451
436
for (const item of runMessageDatum.content) {
452
437
// types are: "image_file" or "text"
@@ -456,14 +441,30 @@ An individual assistant can access up to 128 tools including `code interpreter`,
456
441
}
457
442
}
458
443
```
444
+
459
445
460
- 1. Sign in to Azure with the following command:
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.
461
462
462
463
```shell
463
- az login
464
+ tsc
464
465
```
465
466
466
- 1. Run the JavaScript file.
467
+ 1. Run the code with the following command:
467
468
468
469
```shell
469
470
node index.js
@@ -473,7 +474,7 @@ An individual assistant can access up to 128 tools including `code interpreter`,
473
474
474
475
1. Create the `index.js` file with the following code:
475
476
476
- ```nodejs
477
+ ```javascript
477
478
import { AzureOpenAI } from "openai";
478
479
479
480
// Get environment variables
0 commit comments