Skip to content

Commit 10ce029

Browse files
Merge pull request #959 from diberry/diberry/1022-chat
Quickstart - JS Chat & Completions - add entra for JS & TS
2 parents 8dd469e + 53bd9fb commit 10ce029

File tree

7 files changed

+540
-371
lines changed

7 files changed

+540
-371
lines changed

articles/ai-services/openai/chatgpt-quickstart.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ Use this article to get started using Azure OpenAI.
5454

5555
::: zone-end
5656

57+
::: zone pivot="programming-language-typescript"
58+
59+
[!INCLUDE [TypeScript quickstart](includes/chatgpt-typescript.md)]
60+
61+
::: zone-end
62+
5763
::: zone pivot="programming-language-python"
5864

5965
[!INCLUDE [Python SDK quickstart](includes/chatgpt-python.md)]

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

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

1414
[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/main/sdk/openai/openai/samples)
@@ -18,26 +18,14 @@ ms.date: 05/21/2024
1818
1919
## Prerequisites
2020

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)
25-
- [TypeScript](https://www.typescriptlang.org/download/)
26-
- 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-
3321
- An Azure subscription - [Create one for free](https://azure.microsoft.com/free/cognitive-services?azure-portal=true)
3422
- [LTS versions of Node.js](https://github.com/nodejs/release#release-schedule)
23+
- [Azure CLI](/cli/azure/install-azure-cli) used for passwordless authentication in a local development environment, create the necessary context by signing in with the Azure CLI.
3524
- 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).
3625

3726
> [!div class="nextstepaction"]
3827
> [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)
3928
40-
---
4129

4230
## Set up
4331

@@ -54,7 +42,7 @@ In a console window (such as cmd, PowerShell, or Bash), create a new directory f
5442
Install the required packages for JavaScript with npm from within the context of your new directory:
5543

5644
```console
57-
npm install openai dotenv @azure/identity
45+
npm install openai @azure/identity
5846
```
5947

6048
Your app's _package.json_ file will be updated with the dependencies.
@@ -66,94 +54,69 @@ Your app's _package.json_ file will be updated with the dependencies.
6654

6755
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.
6856

69-
## [**TypeScript**](#tab/typescript)
7057

71-
```typescript
72-
import "dotenv/config";
73-
import { AzureOpenAI } from "openai";
74-
import type {
75-
ChatCompletion,
76-
ChatCompletionCreateParamsNonStreaming,
77-
} from "openai/resources/index";
58+
## [Microsoft Entra ID](#tab/javascript-keyless)
59+
60+
```javascript
61+
const { AzureOpenAI } = require("openai");
62+
const {
63+
DefaultAzureCredential,
64+
getBearerTokenProvider
65+
} = require("@azure/identity");
7866

7967
// You will need to set these environment variables or edit the following values
8068
const endpoint = process.env["AZURE_OPENAI_ENDPOINT"] || "<endpoint>";
81-
const apiKey = process.env["AZURE_OPENAI_API_KEY"] || "<api key>";
69+
const apiVersion = "2024-05-01-preview";
70+
const deployment = "gpt-4o"; //This must match your deployment name.
8271

83-
// Required Azure OpenAI deployment name and API version
84-
const apiVersion = "2024-08-01-preview";
85-
const deploymentName = "gpt-4o-mini"; //This must match your deployment name.
8672

87-
function getClient(): AzureOpenAI {
88-
return new AzureOpenAI({
89-
endpoint,
90-
apiKey,
91-
apiVersion,
92-
deployment: deploymentName,
93-
});
94-
}
73+
// keyless authentication
74+
const credential = new DefaultAzureCredential();
75+
const scope = "https://cognitiveservices.azure.com/.default";
76+
const azureADTokenProvider = getBearerTokenProvider(credential, scope);
9577

96-
function createMessages(): ChatCompletionCreateParamsNonStreaming {
97-
return {
78+
async function main() {
79+
80+
const client = new AzureOpenAI({ endpoint, apiKey, azureADTokenProvider, deployment });
81+
const result = await client.chat.completions.create({
9882
messages: [
99-
{ role: "system", content: "You are a helpful assistant." },
100-
{
101-
role: "user",
102-
content: "Does Azure OpenAI support customer managed keys?",
103-
},
104-
{
105-
role: "assistant",
106-
content: "Yes, customer managed keys are supported by Azure OpenAI?",
107-
},
108-
{ role: "user", content: "Do other Azure AI services support this too?" },
83+
{ role: "system", content: "You are a helpful assistant." },
84+
{ role: "user", content: "Does Azure OpenAI support customer managed keys?" },
85+
{ role: "assistant", content: "Yes, customer managed keys are supported by Azure OpenAI?" },
86+
{ role: "user", content: "Do other Azure AI services support this too?" },
10987
],
11088
model: "",
111-
};
112-
}
113-
async function printChoices(completion: ChatCompletion): Promise<void> {
114-
for (const choice of completion.choices) {
89+
});
90+
91+
for (const choice of result.choices) {
11592
console.log(choice.message);
11693
}
11794
}
118-
export async function main() {
119-
const client = getClient();
120-
const messages = createMessages();
121-
const result = await client.chat.completions.create(messages);
122-
await printChoices(result);
123-
}
12495

12596
main().catch((err) => {
12697
console.error("The sample encountered an error:", err);
12798
});
128-
```
129-
130-
Build the script with the following command:
13199

132-
```cmd
133-
tsc
100+
module.exports = { main };
134101
```
135102

136103
Run the script with the following command:
137104

138105
```cmd
139-
node.exe Completion.js
106+
node.exe ChatCompletion.js
140107
```
141108

142-
## [**JavaScript**](#tab/javascript)
109+
110+
## [API key](#tab/javascript-key)
143111

144112
```javascript
145113
const { AzureOpenAI } = require("openai");
146114

147-
// Load the .env file if it exists
148-
const dotenv = require("dotenv");
149-
dotenv.config();
150-
151115
// You will need to set these environment variables or edit the following values
152116
const endpoint = process.env["AZURE_OPENAI_ENDPOINT"] || "<endpoint>";
153117
const apiKey = process.env["AZURE_OPENAI_API_KEY"] || "<api key>";
154118
const apiVersion = "2024-05-01-preview";
155119
const deployment = "gpt-4o"; //This must match your deployment name.
156-
require("dotenv/config");
157120

158121
async function main() {
159122

@@ -198,122 +161,6 @@ node.exe ChatCompletion.js
198161
}
199162
```
200163

201-
## Microsoft Entra ID
202-
203-
> [!IMPORTANT]
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).
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-
import type {
216-
ChatCompletion,
217-
ChatCompletionCreateParamsNonStreaming,
218-
} from "openai/resources/index";
219-
220-
// You will need to set these environment variables or edit the following values
221-
const endpoint = process.env["AZURE_OPENAI_ENDPOINT"] || "<endpoint>";
222-
223-
// Required Azure OpenAI deployment name and API version
224-
const apiVersion = "2024-08-01-preview";
225-
const deploymentName = "gpt-4o-mini"; //This must match your deployment name.
226-
227-
function getClient(): AzureOpenAI {
228-
const scope = "https://cognitiveservices.azure.com/.default";
229-
const azureADTokenProvider = getBearerTokenProvider(
230-
new DefaultAzureCredential(),
231-
scope
232-
);
233-
return new AzureOpenAI({
234-
endpoint,
235-
azureADTokenProvider,
236-
deployment: deploymentName,
237-
apiVersion,
238-
});
239-
}
240-
241-
function createMessages(): ChatCompletionCreateParamsNonStreaming {
242-
return {
243-
messages: [
244-
{ role: "system", content: "You are a helpful assistant." },
245-
{
246-
role: "user",
247-
content: "Does Azure OpenAI support customer managed keys?",
248-
},
249-
{
250-
role: "assistant",
251-
content: "Yes, customer managed keys are supported by Azure OpenAI?",
252-
},
253-
{ role: "user", content: "Do other Azure AI services support this too?" },
254-
],
255-
model: "",
256-
};
257-
}
258-
async function printChoices(completion: ChatCompletion): Promise<void> {
259-
for (const choice of completion.choices) {
260-
console.log(choice.message);
261-
}
262-
}
263-
export async function main() {
264-
const client = getClient();
265-
const messages = createMessages();
266-
const result = await client.chat.completions.create(messages);
267-
await printChoices(result);
268-
}
269-
270-
main().catch((err) => {
271-
console.error("The sample encountered an error:", err);
272-
});
273-
```
274-
275-
276-
## [**JavaScript**](#tab/javascript)
277-
278-
```javascript
279-
const { AzureOpenAI } = require("openai");
280-
const { DefaultAzureCredential, getBearerTokenProvider } = require("@azure/identity");
281-
282-
// Set AZURE_OPENAI_ENDPOINT to the endpoint of your
283-
// OpenAI resource. You can find this in the Azure portal.
284-
// Load the .env file if it exists
285-
require("dotenv/config");
286-
287-
async function main() {
288-
console.log("== Chat Completions Sample ==");
289-
290-
const scope = "https://cognitiveservices.azure.com/.default";
291-
const azureADTokenProvider = getBearerTokenProvider(new DefaultAzureCredential(), scope);
292-
const deployment = "gpt-35-turbo";
293-
const apiVersion = "2024-04-01-preview";
294-
const client = new AzureOpenAI({ azureADTokenProvider, deployment, apiVersion });
295-
const result = await client.chat.completions.create({
296-
messages: [
297-
{ role: "system", content: "You are a helpful assistant." },
298-
{ role: "user", content: "Does Azure OpenAI support customer managed keys?" },
299-
{ role: "assistant", content: "Yes, customer managed keys are supported by Azure OpenAI?" },
300-
{ role: "user", content: "Do other Azure AI services support this too?" },
301-
],
302-
model: "",
303-
});
304-
305-
for (const choice of result.choices) {
306-
console.log(choice.message);
307-
}
308-
}
309-
310-
main().catch((err) => {
311-
console.error("The sample encountered an error:", err);
312-
});
313-
314-
module.exports = { main };
315-
```
316-
317164
---
318165

319166
> [!NOTE]

0 commit comments

Comments
 (0)