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
> This article has been updated to use the [latest OpenAI npm package](https://www.npmjs.com/package/openai) which now fully supports Azure OpenAI. If you are looking for code examples for the legacy Azure OpenAI JavaScript SDK they are currently still [available in this repo](https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/openai/openai/samples/v2-beta/javascript).
18
18
19
19
## Prerequisites
20
20
21
+
## [**TypeScript**](#tab/typescript)
22
+
23
+
- An Azure subscription - [Create one for free](https://azure.microsoft.com/free/cognitive-services?azure-portal=true)
24
+
-[LTS versions of Node.js](https://github.com/nodejs/release#release-schedule)
- An Azure OpenAI Service resource with a `gpt-35-turbo` or `gpt-4` series models deployed. For more information about model deployment, see the [resource deployment guide](../how-to/create-resource.md).
27
+
28
+
> [!div class="nextstepaction"]
29
+
> [I ran into an issue with the prerequisites.](https://microsoft.qualtrics.com/jfe/form/SV_0Cl5zkG3CnDjq6O?PLanguage=JAVASCRIPT&Pillar=AOAI&Product=Chatgpt&Page=quickstart&Section=Prerequisites)
30
+
31
+
## [**JavaScript**](#tab/javascript)
32
+
21
33
- An Azure subscription - [Create one for free](https://azure.microsoft.com/free/cognitive-services?azure-portal=true)
22
34
-[LTS versions of Node.js](https://github.com/nodejs/release#release-schedule)
23
35
- An Azure OpenAI Service resource with either a `gpt-35-turbo` or `gpt-4` series models deployed. For more information about model deployment, see the [resource deployment guide](../how-to/create-resource.md).
24
36
25
37
> [!div class="nextstepaction"]
26
38
> [I ran into an issue with the prerequisites.](https://microsoft.qualtrics.com/jfe/form/SV_0Cl5zkG3CnDjq6O?PLanguage=JAVASCRIPT&Pillar=AOAI&Product=Chatgpt&Page=quickstart&Section=Prerequisites)
@@ -52,6 +66,81 @@ Your app's _package.json_ file will be updated with the dependencies.
52
66
53
67
Open a command prompt where you want the new project, and create a new file named ChatCompletion.js. Copy the following code into the ChatCompletion.js file.
54
68
69
+
## [**TypeScript**](#tab/typescript)
70
+
71
+
```typescript
72
+
import"dotenv/config";
73
+
import { AzureOpenAI } from"openai";
74
+
importtype {
75
+
ChatCompletion,
76
+
ChatCompletionCreateParamsNonStreaming,
77
+
} from"openai/resources/index";
78
+
79
+
// You will need to set these environment variables or edit the following values
const result =awaitclient.chat.completions.create(messages);
122
+
awaitprintChoices(result);
123
+
}
124
+
125
+
main().catch((err) => {
126
+
console.error("The sample encountered an error:", err);
127
+
});
128
+
```
129
+
130
+
Build the script with the following command:
131
+
132
+
```cmd
133
+
tsc
134
+
```
135
+
136
+
Run the script with the following command:
137
+
138
+
```cmd
139
+
node.exe Completion.js
140
+
```
141
+
142
+
## [**JavaScript**](#tab/javascript)
143
+
55
144
```javascript
56
145
const { AzureOpenAI } =require("openai");
57
146
@@ -97,6 +186,8 @@ Run the script with the following command:
97
186
node.exe ChatCompletion.js
98
187
```
99
188
189
+
---
190
+
100
191
## Output
101
192
102
193
```output
@@ -112,6 +203,78 @@ node.exe ChatCompletion.js
112
203
> [!IMPORTANT]
113
204
> In the previous example we are demonstrating key-based authentication. Once you have tested with key-based authentication successfully, we recommend using the more secure [Microsoft Entra ID](/entra/fundamentals/whatis) for authentication which is demonstrated in the next code sample. Getting started with [Microsoft Entra ID] will require some additional [prerequisites](https://www.npmjs.com/package/@azure/identity).
114
205
206
+
## [**TypeScript**](#tab/typescript)
207
+
208
+
```typescript
209
+
import {
210
+
DefaultAzureCredential,
211
+
getBearerTokenProvider,
212
+
} from"@azure/identity";
213
+
import"dotenv/config";
214
+
import { AzureOpenAI } from"openai";
215
+
importtype {
216
+
ChatCompletion,
217
+
ChatCompletionCreateParamsNonStreaming,
218
+
} from"openai/resources/index";
219
+
220
+
// You will need to set these environment variables or edit the following values
> If your receive the error: *Error occurred: OpenAIError: The `apiKey` and `azureADTokenProvider` arguments are mutually exclusive; only one can be passed at a time.* You may need to remove a pre-existing environment variable for the API key from your system. Even though the Microsoft Entra ID code sample is not explicitly referencing the API key environment variable, if one is present on the system executing this sample, this error will still be generated.
0 commit comments