Skip to content

Commit 53fbb50

Browse files
committed
acrolinx
1 parent 91ec1fc commit 53fbb50

File tree

1 file changed

+24
-24
lines changed

1 file changed

+24
-24
lines changed

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

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: How to migrate to OpenAI Javascript v4.x
2+
title: How to migrate to OpenAI JavaScript v4.x
33
titleSuffix: Azure OpenAI Service
44
description: Learn about migrating to the latest release of the OpenAI JavaScript library with Azure OpenAI.
55
author: mrbullwinkle
@@ -13,15 +13,15 @@ manager: nitinme
1313

1414
# Migrating to the OpenAI JavaScript API library 4.x
1515

16-
As of June 2024, we recommend migrating to the OpenAI JavaScript API library 4.x, the latest version of the official OpenAI JavaScript client library that supports the Azure OpenAI Service API version `2022-12-01` and later. This article will help you bring you up to speed on the changes specific to Azure OpenAI.
16+
As of June 2024, we recommend migrating to the OpenAI JavaScript API library 4.x, the latest version of the official OpenAI JavaScript client library that supports the Azure OpenAI Service API version `2022-12-01` and later. This article helps you bring you up to speed on the changes specific to Azure OpenAI.
1717

1818
## Authenticating the client
1919

2020
There are several ways to authenticate API requests to Azure OpenAI. We highly recommend using Microsoft Entra ID tokens. See the [Azure Identity documentation](https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/identity/identity/README.md) for more information.
2121

22-
### Microsoft Entra ID (previously Azure Active Directory)
22+
### Microsoft Entra ID
2323

24-
There are several ways to authenticate with the Azure OpenAI service using Microsoft Entra ID tokens. The default way is to use the `DefaultAzureCredential` class from the `@azure/identity` package, but your application may be using a different credential class. For the purposes of this guide, we will assume that you are using the `DefaultAzureCredential` class. A credential can be created as follows:
24+
There are several ways to authenticate with the Azure OpenAI service using Microsoft Entra ID tokens. The default way is to use the `DefaultAzureCredential` class from the `@azure/identity` package, but your application might be using a different credential class. For the purposes of this guide, we assume that you are using the `DefaultAzureCredential` class. A credential can be created as follows:
2525

2626
```typescript
2727
import { DefaultAzureCredential } from "@azure/identity";
@@ -66,7 +66,7 @@ const client = new AzureOpenAI(options);
6666

6767
The endpoint of the Azure OpenAI resource can be specified by setting the `endpoint` option but it can also be loaded by the client from the environment variable `AZURE_OPENAI_ENDPOINT`. This is the recommended way to set the endpoint because it allows the client to be used in different environments without changing the code and also to protect the endpoint from being exposed in the code.
6868

69-
Note that the API version is required to be specified, this is necessary to ensure that existing code doesn't break between preview API versions. Refer to [API versioning documentation](../api-version-deprecation.md) to learn more about Azure OpenAI API versions. Additionally, the `deployment` property is not required but it is recommended to be set. Once `deployment` is set, it is used as the default deployment for all operations that require it. If the client is not created with the `deployment` option, the `model` property in the options object should be set with the deployment name. However, audio operations such as `audio.transcriptions.create` require the client to be created with the `deployment` option set to the deployment name.
69+
The API version is required to be specified, this is necessary to ensure that existing code doesn't break between preview API versions. Refer to [API versioning documentation](../api-version-deprecation.md) to learn more about Azure OpenAI API versions. Additionally, the `deployment` property isn't required but it's recommended to be set. Once `deployment` is set, it's used as the default deployment for all operations that require it. If the client isn't created with the `deployment` option, the `model` property in the options object should be set with the deployment name. However, audio operations such as `audio.transcriptions.create` require the client to be created with the `deployment` option set to the deployment name.
7070

7171
# [Azure OpenAI JavaScript (previous)](#tab/javascript-old)
7272

@@ -111,7 +111,7 @@ Note the following:
111111
- The `getChatCompletions` method has been replaced with the `chat.completions.create` method.
112112
- The `messages` parameter is now passed in the options object with the `messages` property.
113113
- The `maxTokens` property has been renamed to `max_tokens` and the `deploymentName` parameter has been removed. Generally, the names of the properties in the `options` object are the same as in the Azure OpenAI service API, following the snake case convention instead of the camel case convention used in the `AssistantsClient`. This is true for all the properties across all requests and responses in the `AzureOpenAI` client.
114-
- 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.
114+
- The `deploymentName` parameter isn't 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.
115115

116116
### Streaming chat completions
117117

@@ -172,7 +172,7 @@ const result = await client.getChatCompletions(deploymentName, messages, { azure
172172

173173
---
174174

175-
Note that:
175+
176176
- The `azureExtensionOptions` property has been replaced with the inner `data_sources` property.
177177
- The `parameters` property has been added to wrap the parameters of the extension, which mirrors the schema of the Azure OpenAI service API.
178178
- Camel case properties have been replaced with snake case properties.
@@ -202,12 +202,12 @@ const result = await client.getAudioTranscription(deploymentName, audio);
202202

203203
---
204204

205-
Note that:
205+
206206

207207
- The `getAudioTranscription` method has been replaced with the `audio.transcriptions.create` method
208208
- The `AzureOpenAI` has to be constructed with the `deployment` option set to the deployment name in order to use audio operations such as `audio.transcriptions.create`
209-
- The `model` property is required to be set in the options object but its value is not used in the operation so feel free to set it to any value
210-
- The `file` property accepts a variety of types including `Buffer`, `fs.ReadaStream`, and `Blob` but in this example, a file is streamed from disk using `fs.createReadStream`
209+
- The `model` property is required to be set in the options object but its value isn't used in the operation so feel free to set it to any value
210+
- The `file` property accepts various types including `Buffer`, `fs.ReadaStream`, and `Blob` but in this example, a file is streamed from disk using `fs.createReadStream`
211211

212212
### Audio translation
213213

@@ -234,7 +234,7 @@ const result = await client.getAudioTranslation(deploymentName, audio);
234234

235235
---
236236

237-
Note that:
237+
238238
- The `getAudioTranslation` method has been replaced with the `audio.translations.create` method.
239239
- All other changes are the same as in the audio transcription example.
240240

@@ -268,7 +268,7 @@ const assistantResponse = await assistantsClient.createAssistant(options);
268268

269269
---
270270

271-
Note that:
271+
272272

273273
- The `createAssistant` method has been replaced with the `beta.assistants.create` method
274274

@@ -290,7 +290,7 @@ const assistantThread = await assistantsClient.createThread();
290290

291291
---
292292

293-
Note that:
293+
294294

295295
- The `createThread` method has been replaced with the `beta.threads.create` method
296296

@@ -322,17 +322,17 @@ const threadResponse = await assistantsClient.createMessage(
322322

323323
---
324324

325-
Note that:
325+
326326
- The `createMessage` method has been replaced with the `beta.threads.messages.create` method
327327
- The message specification has been moved from a parameter list to an object
328328

329329
#### Runs
330330

331-
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.
331+
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.
332332

333333
# [OpenAI JavaScript (new)](#tab/javascript-new)
334334

335-
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.
335+
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.
336336

337337
```typescript
338338
const runResponse = await assistantsClient.beta.threads.runs.createAndPoll(
@@ -365,7 +365,7 @@ do {
365365
---
366366
367367
368-
Note that:
368+
369369
- The `createRun` method has been replaced with the `beta.threads.runs.create` and `createAndPoll` methods
370370
- The `createAndPoll` method is used to create a run and poll it until it is in a terminal state
371371
@@ -416,10 +416,10 @@ const embeddings = await client.getEmbeddings(deploymentName, input);
416416
417417
---
418418
419-
Note that:
419+
420420
- The `getEmbeddings` method has been replaced with the `embeddings.create` method
421421
- The `input` parameter is now passed in the options object with the `input` property
422-
- 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
422+
- The `deploymentName` parameter has been removed. The `deploymentName` parameter isn't 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
423423
424424
### Image generation
425425
@@ -440,10 +440,10 @@ const results = await client.getImages(deploymentName, prompt, { n, size });
440440
---
441441
442442
443-
Note that:
443+
444444
- The `getImages` method has been replaced with the `images.generate` method
445445
- The `prompt` parameter is now passed in the options object with the `prompt` property
446-
- 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
446+
- The `deploymentName` parameter has been removed. The `deploymentName` parameter isn't 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
447447
448448
### Content filter
449449
@@ -490,9 +490,9 @@ However `AzureOpenAI` does not have a direct equivalent to the `contentFilterRes
490490
491491
---
492492
493-
Note that:
493+
494494
- camel case properties have been replaced with snake case properties
495-
- 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
495+
- A cast to `any` is used to access the `content_filter_results` property because it isn't part of the `ChatCompletion.Choice` interface, see the [Azure types](#azure-types) section for more information
496496
497497
## Comparing Types
498498
@@ -556,7 +556,7 @@ The following table explores several type names from `@azure/openai` and shows t
556556
557557
## Azure types
558558
559-
`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.
559+
`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 are 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.
560560
561561
## Next steps
562562

0 commit comments

Comments
 (0)