You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: articles/ai-services/openai/how-to/migration-javascript.md
+24-24Lines changed: 24 additions & 24 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,5 @@
1
1
---
2
-
title: How to migrate to OpenAI Javascript v4.x
2
+
title: How to migrate to OpenAI JavaScript v4.x
3
3
titleSuffix: Azure OpenAI Service
4
4
description: Learn about migrating to the latest release of the OpenAI JavaScript library with Azure OpenAI.
5
5
author: mrbullwinkle
@@ -13,15 +13,15 @@ manager: nitinme
13
13
14
14
# Migrating to the OpenAI JavaScript API library 4.x
15
15
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.
17
17
18
18
## Authenticating the client
19
19
20
20
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.
21
21
22
-
### Microsoft Entra ID (previously Azure Active Directory)
22
+
### Microsoft Entra ID
23
23
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:
@@ -66,7 +66,7 @@ const client = new AzureOpenAI(options);
66
66
67
67
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.
68
68
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.
- The `getChatCompletions` method has been replaced with the `chat.completions.create` method.
112
112
- The `messages` parameter is now passed in the options object with the `messages` property.
113
113
- 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.
- The `azureExtensionOptions` property has been replaced with the inner `data_sources` property.
177
177
- The `parameters` property has been added to wrap the parameters of the extension, which mirrors the schema of the Azure OpenAI service API.
178
178
- Camel case properties have been replaced with snake case properties.
@@ -202,12 +202,12 @@ const result = await client.getAudioTranscription(deploymentName, audio);
202
202
203
203
---
204
204
205
-
Note that:
205
+
206
206
207
207
- The `getAudioTranscription` method has been replaced with the `audio.transcriptions.create` method
208
208
- 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`
211
211
212
212
### Audio translation
213
213
@@ -234,7 +234,7 @@ const result = await client.getAudioTranslation(deploymentName, audio);
234
234
235
235
---
236
236
237
-
Note that:
237
+
238
238
- The `getAudioTranslation` method has been replaced with the `audio.translations.create` method.
239
239
- All other changes are the same as in the audio transcription example.
- The `createMessage` method has been replaced with the `beta.threads.messages.create` method
327
327
- The message specification has been moved from a parameter list to an object
328
328
329
329
#### Runs
330
330
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.
332
332
333
333
# [OpenAI JavaScript (new)](#tab/javascript-new)
334
334
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.
- The `getEmbeddings` method has been replaced with the `embeddings.create` method
421
421
- 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
- The `getImages` method has been replaced with the `images.generate` method
445
445
- 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
447
447
448
448
### Content filter
449
449
@@ -490,9 +490,9 @@ However `AzureOpenAI` does not have a direct equivalent to the `contentFilterRes
490
490
491
491
---
492
492
493
-
Note that:
493
+
494
494
- 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
496
496
497
497
## Comparing Types
498
498
@@ -556,7 +556,7 @@ The following table explores several type names from `@azure/openai` and shows t
556
556
557
557
## Azure types
558
558
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.
0 commit comments