Skip to content

Commit 36b5066

Browse files
committed
update
1 parent 9beab3b commit 36b5066

File tree

1 file changed

+65
-14
lines changed

1 file changed

+65
-14
lines changed

articles/ai-services/openai/includes/chatgpt-javascript.md

Lines changed: 65 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@ ms.service: azure-ai-openai
88
ms.topic: include
99
author: mrbullwinkle
1010
ms.author: mbullwin
11-
ms.date: 05/20/2024
11+
ms.date: 05/21/2024
1212
---
1313

14-
[Source code](https://github.com/openai/openai-node) | [Package (npm)](https://www.npmjs.com/package/openai) | [Samples](https://github.com/Azure/azure-sdk-for-js/tree/openai-azure-samples/sdk/openai/openai/samples/v1-beta/javascript)
14+
[Source code](https://github.com/openai/openai-node) | [Package (npm)](https://www.npmjs.com/package/openai) | [Samples](https://github.com/Azure/azure-sdk-for-js/tree/openai-azure-samples/sdk/openai/openai/samples/v1-beta/javascript)
15+
16+
> [!NOTE]
17+
> 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 [1available in this repo](https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/openai/openai/samples/v1-beta/javascript).
1518
1619
## Prerequisites
1720

@@ -39,7 +42,7 @@ In a console window (such as cmd, PowerShell, or Bash), create a new directory f
3942
Install the required client libraries for JavaScript with npm from within the context of your new directory:
4043

4144
```console
42-
npm install openai @azure/openai dotenv
45+
npm install openai dotenv @azure/identity
4346
```
4447

4548
Your app's _package.json_ file will be updated with the dependencies.
@@ -52,43 +55,45 @@ Your app's _package.json_ file will be updated with the dependencies.
5255
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.
5356

5457
```javascript
55-
const { OpenAIClient, AzureKeyCredential } = require("@azure/openai");
58+
const { AzureOpenAI } = require("openai");
5659

5760
// Load the .env file if it exists
5861
const dotenv = require("dotenv");
5962
dotenv.config();
6063

6164
// You will need to set these environment variables or edit the following values
6265
const endpoint = process.env["ENDPOINT"] || "<endpoint>";
63-
const azureApiKey = process.env["AZURE_API_KEY"] || "<api key>";
66+
const apiKey = process.env["AZURE_API_KEY"] || "<api key>";
67+
const apiVersion = "2024-05-01-preview";
68+
const deployment = "gpt-4o"; //The deployment name for your completions API model. The instruct model is the only new model that supports the legacy API.
69+
70+
require("dotenv/config");
6471

6572
async function main() {
66-
console.log("== Chat Completions Sample ==");
6773

68-
const client = new OpenAIClient(endpoint, new AzureKeyCredential(azureApiKey));
69-
const deploymentId = "gpt-35-turbo"; //set this value to match your model deployment name
70-
const result = await client.getChatCompletions(deploymentId, [
74+
const client = new AzureOpenAI({ endpoint, apiKey, apiVersion, deployment });
75+
const result = await client.chat.completions.create({
76+
messages: [
7177
{ role: "system", content: "You are a helpful assistant." },
7278
{ role: "user", content: "Does Azure OpenAI support customer managed keys?" },
7379
{ role: "assistant", content: "Yes, customer managed keys are supported by Azure OpenAI?" },
7480
{ role: "user", content: "Do other Azure AI services support this too?" },
75-
]);
81+
],
82+
model: "",
83+
});
7684

7785
for (const choice of result.choices) {
7886
console.log(choice.message);
7987
}
8088
}
8189

8290
main().catch((err) => {
83-
console.error("Error occurred:", err);
91+
console.error("The sample encountered an error:", err);
8492
});
8593

8694
module.exports = { main };
8795
```
8896

89-
> [!IMPORTANT]
90-
> For production, use a secure way of storing and accessing your credentials like [Azure Key Vault](../../../key-vault/general/overview.md). For more information about credential security, see the Azure AI services [security](../../security-features.md) article.
91-
9297
Run the script with the following command:
9398

9499
```cmd
@@ -105,6 +110,52 @@ node.exe ChatCompletion.js
105110
}
106111
```
107112

113+
## Microsoft Entra ID
114+
115+
> [!IMPORTANT]
116+
> 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).
117+
118+
```javascript
119+
const { AzureOpenAI } = require("openai");
120+
const { DefaultAzureCredential, getBearerTokenProvider } = require("@azure/identity");
121+
122+
// Set AZURE_OPENAI_ENDPOINT to the endpoint of your
123+
// OpenAI resource. You can find this in the Azure portal.
124+
// Load the .env file if it exists
125+
require("dotenv/config");
126+
127+
async function main() {
128+
console.log("== Chat Completions Sample ==");
129+
130+
const scope = "https://cognitiveservices.azure.com/.default";
131+
const azureADTokenProvider = getBearerTokenProvider(new DefaultAzureCredential(), scope);
132+
const deployment = "gpt-35-turbo";
133+
const apiVersion = "2024-04-01-preview";
134+
const client = new AzureOpenAI({ azureADTokenProvider, deployment, apiVersion });
135+
const result = await client.chat.completions.create({
136+
messages: [
137+
{ role: "system", content: "You are a helpful assistant." },
138+
{ role: "user", content: "Does Azure OpenAI support customer managed keys?" },
139+
{ role: "assistant", content: "Yes, customer managed keys are supported by Azure OpenAI?" },
140+
{ role: "user", content: "Do other Azure AI services support this too?" },
141+
],
142+
model: "",
143+
});
144+
145+
for (const choice of result.choices) {
146+
console.log(choice.message);
147+
}
148+
}
149+
150+
main().catch((err) => {
151+
console.error("The sample encountered an error:", err);
152+
});
153+
154+
module.exports = { main };
155+
```
156+
> [!NOTE]
157+
> 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.
158+
108159
> [!div class="nextstepaction"]
109160
> [I ran into an issue when running the code sample.](https://microsoft.qualtrics.com/jfe/form/SV_0Cl5zkG3CnDjq6O?PLanguage=JAVASCRIPT&Pillar=AOAI&Product=Chatgpt&Page=quickstart&Section=Create-application)
110161

0 commit comments

Comments
 (0)