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 [1available in this repo](https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/openai/openai/samples/v1-beta/javascript).
15
18
16
19
## Prerequisites
17
20
18
21
- An Azure subscription - [Create one for free](https://azure.microsoft.com/free/cognitive-services?azure-portal=true)
19
22
- Access granted to the Azure OpenAI service in the desired Azure subscription.
20
23
Currently, access to this service is granted only by application. You can apply for access to Azure OpenAI Service by completing the form at [https://aka.ms/oai/access](https://aka.ms/oai/access?azure-portal=true).
21
24
-[LTS versions of Node.js](https://github.com/nodejs/release#release-schedule)
22
-
- An Azure OpenAI Service resource with either the`gpt-35-turbo` or the `gpt-4` models deployed. For more information about model deployment, see the [resource deployment guide](../how-to/create-resource.md).
25
+
- 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).
23
26
24
27
> [!div class="nextstepaction"]
25
28
> [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)
@@ -32,18 +35,14 @@ ms.date: 07/26/2023
32
35
33
36
## Create a Node application
34
37
35
-
In a console window (such as cmd, PowerShell, or Bash), create a new directory for your app, and navigate to it. Then run the `npm init` command to create a node application with a _package.json_ file.
36
-
37
-
```console
38
-
npm init
39
-
```
38
+
In a console window (such as cmd, PowerShell, or Bash), create a new directory for your app, and navigate to it.
40
39
41
40
## Install the client library
42
41
43
-
Install the Azure OpenAI client library for JavaScript with npm:
42
+
Install the required packages for JavaScript with npm from within the context of your new directory:
44
43
45
44
```console
46
-
npm install @azure/openai
45
+
npm install openai dotenv @azure/identity
47
46
```
48
47
49
48
Your app's _package.json_ file will be updated with the dependencies.
@@ -56,23 +55,31 @@ Your app's _package.json_ file will be updated with the dependencies.
56
55
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.
{ role:"system", content:"You are a helpful assistant." },
77
+
{ role:"user", content:"Does Azure OpenAI support customer managed keys?" },
78
+
{ role:"assistant", content:"Yes, customer managed keys are supported by Azure OpenAI?" },
79
+
{ role:"user", content:"Do other Azure AI services support this too?" },
80
+
],
81
+
model:"",
82
+
});
76
83
77
84
for (constchoiceofresult.choices) {
78
85
console.log(choice.message);
@@ -86,9 +93,6 @@ main().catch((err) => {
86
93
module.exports= { main };
87
94
```
88
95
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
-
92
96
Run the script with the following command:
93
97
94
98
```cmd
@@ -100,10 +104,56 @@ node.exe ChatCompletion.js
100
104
```output
101
105
== Chat Completions Sample ==
102
106
{
103
-
role: 'assistant',
104
-
content: 'Yes, most Azure AI services support customer managed keys. It is always best to check the specific service documentation to confirm this.'
107
+
content: 'Yes, several other Azure AI services also support customer managed keys for enhanced security and control over encryption keys.',
108
+
role: 'assistant'
109
+
}
110
+
```
111
+
112
+
## Microsoft Entra ID
113
+
114
+
> [!IMPORTANT]
115
+
> 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).
{ role:"system", content:"You are a helpful assistant." },
137
+
{ role:"user", content:"Does Azure OpenAI support customer managed keys?" },
138
+
{ role:"assistant", content:"Yes, customer managed keys are supported by Azure OpenAI?" },
139
+
{ role:"user", content:"Do other Azure AI services support this too?" },
140
+
],
141
+
model:"",
142
+
});
143
+
144
+
for (constchoiceofresult.choices) {
145
+
console.log(choice.message);
146
+
}
105
147
}
148
+
149
+
main().catch((err) => {
150
+
console.error("The sample encountered an error:", err);
151
+
});
152
+
153
+
module.exports= { main };
106
154
```
155
+
> [!NOTE]
156
+
> 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.
107
157
108
158
> [!div class="nextstepaction"]
109
159
> [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)
> 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).
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).
constdeployment="gpt-35-turbo-instruct"; //The deployment name for your completions API model. The instruct model is the only new model that supports the legacy API.
0 commit comments