Skip to content

Commit b07bc55

Browse files
committed
Quickstart - JS Use your data - add entra for JS & TS
1 parent b2a8256 commit b07bc55

File tree

2 files changed

+197
-6
lines changed

2 files changed

+197
-6
lines changed

articles/ai-services/openai/includes/use-your-data-javascript.md

Lines changed: 194 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ author: glharper
55
ms.author: glharper
66
ms.service: azure-ai-openai
77
ms.topic: include
8-
ms.date: 09/06/2024
8+
ms.date: 10/22/2024
99
---
1010

1111
[!INCLUDE [Set up required variables](./use-your-data-common-variables.md)]
@@ -41,9 +41,199 @@ npm install @azure/openai @azure/identity
4141

4242
Your app's _package.json_ file will be updated with the dependencies.
4343

44-
## Create a sample application
44+
#### [TypeScript (Entra ID)](#tab/typescript-keyless)
4545

46-
#### [TypeScript](#tab/typescript)
46+
1. Open a command prompt where you want the new project, and create a new file named `ChatWithOwnData.ts`. Copy the following code into the `ChatWithOwnData.ts` file.
47+
48+
```typescript
49+
import "dotenv/config";
50+
import { AzureOpenAI } from "openai";
51+
import { DefaultAzureCredential, getBearerTokenProvider } from "@azure/identity";
52+
import "@azure/openai/types";
53+
54+
// Set the Azure and AI Search values from environment variables
55+
const endpoint = process.env["AZURE_OPENAI_ENDPOINT"];
56+
const searchEndpoint = process.env["AZURE_AI_SEARCH_ENDPOINT"];
57+
const searchIndex = process.env["AZURE_AI_SEARCH_INDEX"];
58+
59+
// keyless authentication
60+
const credential = new DefaultAzureCredential();
61+
const scope = "https://cognitiveservices.azure.com/.default";
62+
const azureADTokenProvider = getBearerTokenProvider(credential, scope);
63+
64+
// Required Azure OpenAI deployment name and API version
65+
const deploymentName = "gpt-4";
66+
const apiVersion = "2024-07-01-preview";
67+
68+
function getClient(): AzureOpenAI {
69+
return new AzureOpenAI({
70+
endpoint,
71+
azureADTokenProvider,
72+
deployment: deploymentName,
73+
apiVersion,
74+
});
75+
}
76+
77+
async function main() {
78+
const client = getClient();
79+
80+
const messages = [
81+
{ role: "user", content: "What are my available health plans?" },
82+
];
83+
84+
console.log(`Message: ${messages.map((m) => m.content).join("\n")}`);
85+
86+
const events = await client.chat.completions.create({
87+
stream: true,
88+
messages: [
89+
{
90+
role: "user",
91+
content:
92+
"What's the most common feedback we received from our customers about the product?",
93+
},
94+
],
95+
max_tokens: 128,
96+
model: "",
97+
data_sources: [
98+
{
99+
type: "azure_search",
100+
parameters: {
101+
endpoint: searchEndpoint,
102+
index_name: searchIndex,
103+
authentication: {
104+
type: "api_key",
105+
key: searchKey,
106+
},
107+
},
108+
},
109+
],
110+
});
111+
112+
let response = "";
113+
for await (const event of events) {
114+
for (const choice of event.choices) {
115+
const newText = choice.delta?.content;
116+
if (newText) {
117+
response += newText;
118+
// To see streaming results as they arrive, uncomment line below
119+
// console.log(newText);
120+
}
121+
}
122+
}
123+
console.log(response);
124+
}
125+
126+
main().catch((err) => {
127+
console.error("The sample encountered an error:", err);
128+
});
129+
```
130+
131+
1. Build the application with the following command:
132+
133+
```console
134+
tsc
135+
```
136+
137+
1. Run the application with the following command:
138+
139+
```console
140+
node ChatWithOwnData.js
141+
```
142+
143+
#### [JavaScript (Entra ID)](#tab/javascript-keyless)
144+
145+
1. Open a command prompt where you want the new project, and create a new file named `ChatWithOwnData.js`. Copy the following code into the `ChatWithOwnData.js` file.
146+
147+
```javascript
148+
require("dotenv/config");
149+
import { DefaultAzureCredential, getBearerTokenProvider } from "@azure/identity";
150+
const { AzureOpenAI } = require("openai");
151+
152+
// Set the Azure and AI Search values from environment variables
153+
const endpoint = process.env["AZURE_OPENAI_ENDPOINT"];
154+
const searchEndpoint = process.env["AZURE_AI_SEARCH_ENDPOINT"];
155+
const searchIndex = process.env["AZURE_AI_SEARCH_INDEX"];
156+
157+
// keyless authentication
158+
const credential = new DefaultAzureCredential();
159+
const scope = "https://cognitiveservices.azure.com/.default";
160+
const azureADTokenProvider = getBearerTokenProvider(credential, scope);
161+
162+
// Required Azure OpenAI deployment name and API version
163+
const deploymentName = "gpt-4";
164+
const apiVersion = "2024-07-01-preview";
165+
166+
function getClient() {
167+
return new AzureOpenAI({
168+
endpoint,
169+
azureADTokenProvider,
170+
deployment: deploymentName,
171+
apiVersion,
172+
});
173+
}
174+
175+
async function main() {
176+
const client = getClient();
177+
178+
const messages = [
179+
{ role: "user", content: "What are my available health plans?" },
180+
];
181+
182+
console.log(`Message: ${messages.map((m) => m.content).join("\n")}`);
183+
184+
const events = await client.chat.completions.create({
185+
stream: true,
186+
messages: [
187+
{
188+
role: "user",
189+
content:
190+
"What's the most common feedback we received from our customers about the product?",
191+
},
192+
],
193+
max_tokens: 128,
194+
model: "",
195+
data_sources: [
196+
{
197+
type: "azure_search",
198+
parameters: {
199+
endpoint: searchEndpoint,
200+
index_name: searchIndex,
201+
authentication: {
202+
type: "api_key",
203+
key: searchKey,
204+
},
205+
},
206+
},
207+
],
208+
});
209+
210+
let response = "";
211+
for await (const event of events) {
212+
for (const choice of event.choices) {
213+
const newText = choice.delta?.content;
214+
if (newText) {
215+
response += newText;
216+
// To see streaming results as they arrive, uncomment line below
217+
// console.log(newText);
218+
}
219+
}
220+
}
221+
console.log(response);
222+
}
223+
224+
main().catch((err) => {
225+
console.error("The sample encountered an error:", err);
226+
});
227+
```
228+
229+
1. Run the application with the following command:
230+
231+
```console
232+
node ChatWithOwnData.js
233+
```
234+
235+
236+
#### [TypeScript (API Key)](#tab/typescript-key)
47237

48238
1. Open a command prompt where you want the new project, and create a new file named `ChatWithOwnData.ts`. Copy the following code into the `ChatWithOwnData.ts` file.
49239

@@ -138,7 +328,7 @@ Your app's _package.json_ file will be updated with the dependencies.
138328
node ChatWithOwnData.js
139329
```
140330

141-
#### [JavaScript](#tab/javascript)
331+
#### [JavaScript (API Key)](#tab/javascript-key)
142332

143333
1. Open a command prompt where you want the new project, and create a new file named `ChatWithOwnData.js`. Copy the following code into the `ChatWithOwnData.js` file.
144334

articles/ai-services/openai/use-your-data-quickstart.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ ms.custom: devx-track-dotnet, devx-track-extended-java, devx-track-js, devx-trac
99
ms.topic: quickstart
1010
author: aahill
1111
ms.author: aahi
12-
ms.date: 08/21/2024
12+
ms.date: 10/22/2024
1313
recommendations: false
1414
zone_pivot_groups: openai-use-your-data
1515
---
@@ -90,7 +90,7 @@ The following resources:
9090

9191
- [LTS versions of Node.js](https://github.com/nodejs/release#release-schedule)
9292
- [TypeScript](https://www.typescriptlang.org/download/)
93-
93+
- [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.
9494
- An Azure OpenAI resource deployed in a [supported region and with a supported model](./concepts/use-your-data.md#regional-availability-and-model-support).
9595

9696
- Be sure that you're assigned at least the [Cognitive Services Contributor](./how-to/role-based-access-control.md#cognitive-services-contributor) role for the Azure OpenAI resource.
@@ -102,6 +102,7 @@ The following resources:
102102

103103
- An Azure subscription - <a href="https://azure.microsoft.com/free/cognitive-services" target="_blank">Create one for free</a>.
104104
- [LTS versions of Node.js](https://github.com/nodejs/release#release-schedule)
105+
- [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.
105106
- An Azure OpenAI resource deployed in a [supported region and with a supported model](./concepts/use-your-data.md#regional-availability-and-model-support).
106107

107108
- Be sure that you're assigned at least the [Cognitive Services Contributor](./how-to/role-based-access-control.md#cognitive-services-contributor) role for the Azure OpenAI resource.

0 commit comments

Comments
 (0)