Skip to content

Commit 8ad777c

Browse files
committed
adding tabs
1 parent 8768ded commit 8ad777c

File tree

1 file changed

+114
-69
lines changed

1 file changed

+114
-69
lines changed

articles/ai-services/openai/how-to/migration-javascript.md

Lines changed: 114 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ ms.author: mbullwin
77
ms.service: azure-ai-openai
88
ms.custom: devx-track-python
99
ms.topic: how-to
10-
ms.date: 02/26/2024
10+
ms.date: 07/11/2024
1111
manager: nitinme
1212
---
1313

@@ -245,7 +245,17 @@ The following examples show how to migrate some of the `AssistantsClient` method
245245

246246
#### Assistant creation
247247

248-
Original code:
248+
# [OpenAI JavaScript (new)](#tab/javascript-new)
249+
250+
```typescript
251+
const options = ...;
252+
const assistantResponse = await assistantsClient.beta.assistants.create(
253+
options
254+
);
255+
```
256+
257+
# [Azure OpenAI JavaScript (previous)](#tab/javascript-old)
258+
249259
```typescript
250260
const options = {
251261
model: azureOpenAIDeployment,
@@ -257,46 +267,39 @@ const options = {
257267
const assistantResponse = await assistantsClient.createAssistant(options);
258268
```
259269

260-
Migrated code:
261-
```typescript
262-
const options = ...;
263-
const assistantResponse = await assistantsClient.beta.assistants.create(
264-
options
265-
);
266-
```
270+
---
271+
272+
Note that:
267273

268-
Notice that:
269274
- The `createAssistant` method has been replaced with the `beta.assistants.create` method
270275

271276
#### Thread creation
272277

273278
The following example shows how to migrate the `createThread` method call.
274279

275-
Original code:
280+
# [OpenAI JavaScript (new)](#tab/javascript-new)
281+
276282
```typescript
277-
const assistantThread = await assistantsClient.createThread();
283+
const assistantThread = await assistantsClient.beta.threads.create();
278284
```
279285

280-
Migration code:
286+
# [Azure OpenAI JavaScript (previous)](#tab/javascript-old)
287+
281288
```typescript
282-
const assistantThread = await assistantsClient.beta.threads.create();
289+
const assistantThread = await assistantsClient.createThread();
283290
```
284291

285-
Notice that:
292+
---
293+
294+
Note that:
295+
286296
- The `createThread` method has been replaced with the `beta.threads.create` method
287297

288298
#### Message creation
289299

290300
The following example shows how to migrate the `createMessage` method call.
291301

292-
Original code:
293-
```typescript
294-
const threadResponse = await assistantsClient.createMessage(
295-
assistantThread.id,
296-
role,
297-
message
298-
);
299-
```
302+
# [OpenAI JavaScript (new)](#tab/javascript-new)
300303

301304
Migration code:
302305
```typescript
@@ -309,14 +312,44 @@ const threadResponse = await assistantsClient.beta.threads.messages.create(
309312
);
310313
```
311314

312-
Notice that:
315+
# [Azure OpenAI JavaScript (previous)](#tab/javascript-old)
316+
317+
Original code:
318+
```typescript
319+
const threadResponse = await assistantsClient.createMessage(
320+
assistantThread.id,
321+
role,
322+
message
323+
);
324+
```
325+
326+
---
327+
328+
Note that:
313329
- The `createMessage` method has been replaced with the `beta.threads.messages.create` method
314330
- The message specification has been moved from a parameter list to an options object
315331

316332
#### Runs
317333

318334
To run an assistant on a thread, the `createRun` method is used to create a run and then a loop is used to poll the run status until it is in a terminal state. The following example shows how to migrate the run creation and polling.
319335

336+
# [OpenAI JavaScript (new)](#tab/javascript-new)
337+
338+
This code can be migrated and simplified by using the `createAndPoll` method which creates a run and polls it until it is in a terminal state.
339+
340+
Migration code:
341+
```typescript
342+
const runResponse = await assistantsClient.beta.threads.runs.createAndPoll(
343+
assistantThread.id,
344+
{
345+
assistant_id: assistantResponse.id,
346+
},
347+
{ pollIntervalMs: 500 }
348+
);
349+
```
350+
351+
# [Azure OpenAI JavaScript (previous)](#tab/javascript-old)
352+
320353
Original code:
321354
```typescript
322355
let runResponse = await assistantsClient.createRun(assistantThread.id, {
@@ -334,62 +367,61 @@ do {
334367
runResponse.status === "in_progress"
335368
```
336369
337-
This code can be migrated and simplified by using the `createAndPoll` method which creates a run and polls it until it is in a terminal state.
370+
---
338371
339-
Migration code:
340-
```typescript
341-
const runResponse = await assistantsClient.beta.threads.runs.createAndPoll(
342-
assistantThread.id,
343-
{
344-
assistant_id: assistantResponse.id,
345-
},
346-
{ pollIntervalMs: 500 }
347-
);
348-
```
349372
350-
Notice that:
373+
Note that:
351374
- The `createRun` method has been replaced with the `beta.threads.runs.create` and `createAndPoll` methods
352375
- The `createAndPoll` method is used to create a run and poll it until it is in a terminal state
353376
354377
#### Processing Run results
355378
356-
Without paging, results had to be accessed manually page by page using the `data` property of the response object. For instance, accessing the first page can be done as follows:
379+
# [OpenAI JavaScript (new)](#tab/javascript-new)
380+
381+
Pages can be looped through by using the `for await` loop.
357382
358-
Original code:
359383
```typescript
360-
for (const runMessageDatum of runMessages.data) {
384+
for await (const runMessageDatum of runMessages) {
361385
for (const item of runMessageDatum.content) {
362386
...
363387
}
364388
}
365389
```
366390
367-
Pages can be looped through by using the `for await` loop.
391+
# [Azure OpenAI JavaScript (previous)](#tab/javascript-old)
392+
393+
Without paging, results had to be accessed manually page by page using the `data` property of the response object. For instance, accessing the first page can be done as follows:
368394
369-
Migration code:
370395
```typescript
371-
for await (const runMessageDatum of runMessages) {
396+
for (const runMessageDatum of runMessages.data) {
372397
for (const item of runMessageDatum.content) {
373398
...
374399
}
375400
}
376401
```
377402
403+
---
404+
405+
378406
### Embeddings
379407
380408
The following example shows how to migrate the `getEmbeddings` method call.
381409
382-
Original code:
410+
# [OpenAI JavaScript (new)](#tab/javascript-new)
411+
383412
```typescript
384-
const embeddings = await client.getEmbeddings(deploymentName, input);
413+
const embeddings = await client.embeddings.create({ input, model: '' });
385414
```
386415
387-
Migrated code:
416+
# [Azure OpenAI JavaScript (previous)](#tab/javascript-old)
417+
388418
```typescript
389-
const embeddings = await client.embeddings.create({ input, model: '' });
419+
const embeddings = await client.getEmbeddings(deploymentName, input);
390420
```
391421
392-
Notice that:
422+
---
423+
424+
Note that:
393425
- The `getEmbeddings` method has been replaced with the `embeddings.create` method
394426
- The `input` parameter is now passed in the options object with the `input` property
395427
- The `deploymentName` parameter has been removed. The `deploymentName` parameter is not needed if the client was created with the `deployment` option. If the client was not created with the `deployment` option, the `model` property in the option object should be set with the deployment name
@@ -398,17 +430,22 @@ Notice that:
398430
399431
The following example shows how to migrate the `getImages` method call.
400432
401-
Original code:
433+
# [OpenAI JavaScript (new)](#tab/javascript-new)
434+
402435
```typescript
403-
const results = await client.getImages(deploymentName, prompt, { n, size });
436+
const results = await client.images.generate({ prompt, model: '', n, size });
404437
```
405438
406-
Migrated code:
439+
# [Azure OpenAI JavaScript (previous)](#tab/javascript-old)
440+
407441
```typescript
408-
const results = await client.images.generate({ prompt, model: '', n, size });
442+
const results = await client.getImages(deploymentName, prompt, { n, size });
409443
```
410444
411-
Notice that:
445+
---
446+
447+
448+
Note that:
412449
- The `getImages` method has been replaced with the `images.generate` method
413450
- The `prompt` parameter is now passed in the options object with the `prompt` property
414451
- The `deploymentName` parameter has been removed. The `deploymentName` parameter is not needed if the client was created with the `deployment` option. If the client was not created with the `deployment` option, the `model` property in the option object should be set with the deployment name
@@ -417,51 +454,55 @@ Notice that:
417454
418455
Content filter results is part of the chat completions response types in `OpenAIClient`. The following example shows how to access the content filter results.
419456
420-
Original code:
457+
# [OpenAI JavaScript (new)](#tab/javascript-new)
458+
421459
```typescript
422-
const results = await client.getChatCompletions(deploymentName, messages);
460+
const result = await client.chat.completions.create({ model: '', messages });
423461
for (const choice of results.choices) {
424-
if (!choice.contentFilterResults) {
462+
const filterResults = (choice as any).content_filter_results;
463+
if (!filterResults) {
425464
console.log("No content filter is found");
426465
return;
427466
}
428-
if (choice.contentFilterResults.error) {
467+
if (filterResults.error) {
429468
console.log(
430-
`Content filter ran into the error ${choice.contentFilterResults.error.code}: ${choice.contentFilterResults.error.message}`);
469+
`Content filter ran into the error ${filterResults.error.code}: ${filterResults.error.message}`);
431470
}
432-
const { hate, sexual, selfHarm, violence } = choice.contentFilterResults;
471+
const { hate, sexual, self_harm, violence } = filterResults;
433472
...
434473
}
435474
```
436-
However `AzureOpenAI` does not have a direct equivalent to the `contentFilterResults` property in the `ChatCompletion.Choice` interface. The content filter results can be accessed by casting the `results` object to `any` and accessing the `content_filter_results` property.
437475
438-
Migrated code:
476+
# [Azure OpenAI JavaScript (previous)](#tab/javascript-old)
477+
439478
```typescript
440-
const result = await client.chat.completions.create({ model: '', messages });
479+
const results = await client.getChatCompletions(deploymentName, messages);
441480
for (const choice of results.choices) {
442-
const filterResults = (choice as any).content_filter_results;
443-
if (!filterResults) {
481+
if (!choice.contentFilterResults) {
444482
console.log("No content filter is found");
445483
return;
446484
}
447-
if (filterResults.error) {
485+
if (choice.contentFilterResults.error) {
448486
console.log(
449-
`Content filter ran into the error ${filterResults.error.code}: ${filterResults.error.message}`);
487+
`Content filter ran into the error ${choice.contentFilterResults.error.code}: ${choice.contentFilterResults.error.message}`);
450488
}
451-
const { hate, sexual, self_harm, violence } = filterResults;
489+
const { hate, sexual, selfHarm, violence } = choice.contentFilterResults;
452490
...
453491
}
454492
```
455493
456-
Notice that:
494+
However `AzureOpenAI` does not have a direct equivalent to the `contentFilterResults` property in the `ChatCompletion.Choice` interface. The content filter results can be accessed by casting the `results` object to `any` and accessing the `content_filter_results` property.
495+
496+
---
497+
498+
Note that:
457499
- camel case properties have been replaced with snake case properties
458500
- A cast to `any` is used to access the `content_filter_results` property because it is not part of the `ChatCompletion.Choice` interface, see the [Azure types](#azure-types) section for more information
459501
460502
## Comparing Types
461503
462504
The following table explores several type names from `@azure/openai` and shows their nearest `openai` equivalent. The names differences illustrate several of the above-mentioned changes. This table provides an overview, and more detail and code samples are provided in the following sections.
463505
464-
<!-- prettier-ignore -->
465506
| Old Type Name | Nearest New Type | Symbol Type | Change description |
466507
| ------------------------- | ------------------------- | ----------- | ----------------------------- |
467508
| `OpenAIClient` | `AzureOpenAI` | Class | This class replaces the former and has no methods in common with it. See the section on `AzureOpenAI` below. |
@@ -520,4 +561,8 @@ The following table explores several type names from `@azure/openai` and shows t
520561
521562
## Azure types
522563
523-
`AzureOpenAI` connects to the Azure OpenAI service and can call all the operations available in the service. However, the types of the requests and responses are inherited from the `OpenAI` and are not yet updated to reflect the additional features supported exclusively by the Azure OpenAI service. TypeScript users will be required to cast to a more permissive type such as `Record<string, any>` to access those features. Examples in [the Migration examples](#migration-examples) section show how to do this.
564+
`AzureOpenAI` connects to the Azure OpenAI service and can call all the operations available in the service. However, the types of the requests and responses are inherited from the `OpenAI` and are not yet updated to reflect the additional features supported exclusively by the Azure OpenAI service. TypeScript users will be required to cast to a more permissive type such as `Record<string, any>` to access those features. Examples in [the Migration examples](#migration-examples) section show how to do this.
565+
566+
# Next steps
567+
568+
- [Azure OpenAI Assistants](../concepts/assistants.md)

0 commit comments

Comments
 (0)