@@ -110,7 +110,7 @@ In our code we're going to specify the following values:
110110
111111An individual assistant can access up to 128 tools including `code interpreter`, and any custom tools you create via [functions](../how-to/assistant-functions.md).
112112
113- #### [TypeScript keyless (Recommended )](#tab/typescript-keyless)
113+ #### [TypeScript (Microsoft Entra ID )](#tab/typescript-keyless)
114114
1151151. Create the `index.ts` file with the following code:
116116
@@ -244,76 +244,78 @@ An individual assistant can access up to 128 tools including `code interpreter`,
244244 node index.js
245245 ```
246246
247- #### [TypeScript with API key](#tab/typescript-key)
248247
249- 1. Create the `index.ts` file with the following code:
248+ #### [JavaScript (Microsoft Entra ID)](#tab/javascript-keyless)
250249
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");
261258
262259 // 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;
268263
269264 // Check env variables
270- if (!azureOpenAIKey || ! azureOpenAIEndpoint || !azureOpenAIDeployment || !openAIVersion ) {
265+ if (!azureOpenAIEndpoint || !azureOpenAIDeployment || !azureOpenAIVersion ) {
271266 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."
273268 );
274269 }
275270
276271 // 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+
278277 const assistantsClient = new AzureOpenAI({
279278 endpoint: azureOpenAIEndpoint,
280- apiVersion: openAIVersion ,
281- apiKey: azureOpenAIKey ,
279+ apiVersion: azureOpenAIVersion ,
280+ azureADTokenProvider ,
282281 });
283282 return assistantsClient;
284283 };
285284
286285 const assistantsClient = getClient();
287286
288- const options: AssistantCreateParams = {
287+ const options = {
289288 model: azureOpenAIDeployment, // Deployment name seen in Azure AI Studio
290289 name: "Math Tutor",
291290 instructions:
292291 "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" }],
294293 };
295294 const role = "user";
296295 const message = "I need to solve the equation `3x + 11 = 14`. Can you help me?";
297296
298297 // 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+ );
301301 console.log(`Assistant created: ${JSON.stringify(assistantResponse)}`);
302302
303303 // Create a thread
304- const assistantThread: Thread = await assistantsClient.beta.threads.create({});
304+ const assistantThread = await assistantsClient.beta.threads.create({});
305305 console.log(`Thread created: ${JSON.stringify(assistantThread)}`);
306306
307307 // 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+ {
310311 role,
311312 content: message,
312- });
313+ }
314+ );
313315 console.log(`Message created: ${JSON.stringify(threadResponse)}`);
314316
315317 // 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(
317319 assistantThread.id,
318320 {
319321 assistant_id: assistantResponse.id,
@@ -323,8 +325,9 @@ An individual assistant can access up to 128 tools including `code interpreter`,
323325 console.log(`Run created: ${JSON.stringify(runResponse)}`);
324326
325327 // 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+ );
328331 for await (const runMessageDatum of runMessages) {
329332 for (const item of runMessageDatum.content) {
330333 // types are: "image_file" or "text"
@@ -334,107 +337,90 @@ An individual assistant can access up to 128 tools including `code interpreter`,
334337 }
335338 }
336339 ```
337-
338340
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:
355342
356343 ```shell
357- tsc
344+ az login
358345 ```
359346
360- 1. Run the code with the following command:
347+ 1. Run the JavaScript file.
361348
362349 ```shell
363350 node index.js
364351 ```
365352
366- #### [JavaScript keyless](#tab/javascript-keyless)
367353
368- 1. Create the `index.js` file with the following code:
354+ #### [TypeScript with API key](#tab/typescript-key)
369355
370- ```nodejs
356+ 1. Create the `index.ts` file with the following code:
357+
358+ ```typescript
371359 import { AzureOpenAI } from "openai";
372-
373- // Add `Cognitive Services User` to identity for Azure OpenAI resource
374360 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";
378368
379369 // 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;
383375
384376 // Check env variables
385- if (!azureOpenAIEndpoint || !azureOpenAIDeployment || !azureOpenAIVersion ) {
377+ if (!azureOpenAIKey || ! azureOpenAIEndpoint || !azureOpenAIDeployment || !openAIVersion ) {
386378 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."
388380 );
389381 }
390382
391383 // 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 => {
396385 const assistantsClient = new AzureOpenAI({
397386 endpoint: azureOpenAIEndpoint,
398- apiVersion: azureOpenAIVersion ,
399- azureADTokenProvider ,
387+ apiVersion: openAIVersion ,
388+ apiKey: azureOpenAIKey ,
400389 });
401390 return assistantsClient;
402391 };
403392
404393 const assistantsClient = getClient();
405394
406- const options = {
395+ const options: AssistantCreateParams = {
407396 model: azureOpenAIDeployment, // Deployment name seen in Azure AI Studio
408397 name: "Math Tutor",
409398 instructions:
410399 "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 ],
412401 };
413402 const role = "user";
414403 const message = "I need to solve the equation `3x + 11 = 14`. Can you help me?";
415404
416405 // 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);
420408 console.log(`Assistant created: ${JSON.stringify(assistantResponse)}`);
421409
422410 // Create a thread
423- const assistantThread = await assistantsClient.beta.threads.create({});
411+ const assistantThread: Thread = await assistantsClient.beta.threads.create({});
424412 console.log(`Thread created: ${JSON.stringify(assistantThread)}`);
425413
426414 // 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, {
430417 role,
431418 content: message,
432- }
433- );
419+ });
434420 console.log(`Message created: ${JSON.stringify(threadResponse)}`);
435421
436422 // 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(
438424 assistantThread.id,
439425 {
440426 assistant_id: assistantResponse.id,
@@ -444,9 +430,8 @@ An individual assistant can access up to 128 tools including `code interpreter`,
444430 console.log(`Run created: ${JSON.stringify(runResponse)}`);
445431
446432 // 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);
450435 for await (const runMessageDatum of runMessages) {
451436 for (const item of runMessageDatum.content) {
452437 // types are: "image_file" or "text"
@@ -456,14 +441,30 @@ An individual assistant can access up to 128 tools including `code interpreter`,
456441 }
457442 }
458443 ```
444+
459445
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.
461462
462463 ```shell
463- az login
464+ tsc
464465 ```
465466
466- 1. Run the JavaScript file.
467+ 1. Run the code with the following command:
467468
468469 ```shell
469470 node index.js
@@ -473,7 +474,7 @@ An individual assistant can access up to 128 tools including `code interpreter`,
473474
4744751. Create the `index.js` file with the following code:
475476
476- ```nodejs
477+ ```javascript
477478 import { AzureOpenAI } from "openai";
478479
479480 // Get environment variables
0 commit comments