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
@@ -39,7 +42,7 @@ In a console window (such as cmd, PowerShell, or Bash), create a new directory f
39
42
Install the required client libraries for JavaScript with npm from within the context of your new directory:
40
43
41
44
```console
42
-
npm install openai @azure/openai dotenv
45
+
npm install openai dotenv @azure/identity
43
46
```
44
47
45
48
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.
52
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." },
72
78
{ role:"user", content:"Does Azure OpenAI support customer managed keys?" },
73
79
{ role:"assistant", content:"Yes, customer managed keys are supported by Azure OpenAI?" },
74
80
{ role:"user", content:"Do other Azure AI services support this too?" },
75
-
]);
81
+
],
82
+
model:"",
83
+
});
76
84
77
85
for (constchoiceofresult.choices) {
78
86
console.log(choice.message);
79
87
}
80
88
}
81
89
82
90
main().catch((err) => {
83
-
console.error("Error occurred:", err);
91
+
console.error("The sample encountered an error:", err);
84
92
});
85
93
86
94
module.exports= { main };
87
95
```
88
96
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
97
Run the script with the following command:
93
98
94
99
```cmd
@@ -105,6 +110,52 @@ node.exe ChatCompletion.js
105
110
}
106
111
```
107
112
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).
{ 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 (constchoiceofresult.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
+
108
159
> [!div class="nextstepaction"]
109
160
> [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)
0 commit comments