Skip to content

Commit c8cd452

Browse files
authored
Merge pull request #167 from diberry/diberry/0906-data
TS: Use your data quickstart
2 parents c51b893 + e02f992 commit c8cd452

File tree

2 files changed

+214
-62
lines changed

2 files changed

+214
-62
lines changed

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

Lines changed: 192 additions & 59 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: 03/04/2024
8+
ms.date: 09/06/2024
99
---
1010

1111
[!INCLUDE [Set up required variables](./use-your-data-common-variables.md)]
@@ -23,81 +23,214 @@ npm init
2323

2424
Install the Azure OpenAI client and Azure Identity libraries for JavaScript with npm:
2525

26+
#### [TypeScript](#tab/typescript)
27+
28+
```console
29+
npm install openai @azure/identity @azure/openai
30+
```
31+
32+
The `@azure/openai/types` dependency is included to extend the Azure OpenAI model for the `data_sources` property. This import is only necessary for TypeScript.
33+
34+
#### [JavaScript](#tab/javascript)
35+
2636
```console
2737
npm install @azure/openai @azure/identity
2838
```
2939

40+
---
41+
3042
Your app's _package.json_ file will be updated with the dependencies.
3143

3244
## Create a sample application
3345

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

223+
1. Run the application with the following command:
90224

225+
```console
226+
node ChatWithOwnData.js
227+
```
91228

92-
module.exports = { main };
93-
```
229+
---
94230

95231
> [!IMPORTANT]
96232
> For production, use a secure way of storing and accessing your credentials like [Azure Key Vault](/azure/key-vault/general/overview). For more information about credential security, see the Azure AI services [security](../../security-features.md) article.
97233

98-
```cmd
99-
node.exe ChatWithOwnData.js
100-
```
101234

102235
## Output
103236

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

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ description: Use this article to import and use your data in Azure OpenAI.
55
#services: cognitive-services
66
manager: nitinme
77
ms.service: azure-ai-openai
8-
ms.custom: devx-track-dotnet, devx-track-extended-java, devx-track-js, devx-track-go, devx-track-python
8+
ms.custom: devx-track-dotnet, devx-track-extended-java, devx-track-js, devx-track-ts, devx-track-go, devx-track-python
99
ms.topic: quickstart
1010
author: aahill
1111
ms.author: aahi
@@ -24,7 +24,7 @@ zone_pivot_groups: openai-use-your-data
2424

2525
::: zone pivot="programming-language-javascript"
2626

27-
[Reference](/javascript/api/@azure/openai) | [Source code](https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/openai/openai) | [Package (npm)](https://www.npmjs.com/package/@azure/openai) | [Samples](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/openai/Azure.AI.OpenAI/tests/Samples)
27+
[Reference documentation](https://platform.openai.com/docs/api-reference/chat) | [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)
2828

2929
::: zone-end
3030

@@ -46,16 +46,35 @@ In this quickstart, you can use your own data with Azure OpenAI models. Using Az
4646

4747
## Prerequisites
4848

49+
#### [TypeScript](#tab/typescript)
50+
4951
- An Azure subscription - <a href="https://azure.microsoft.com/free/cognitive-services" target="_blank">Create one for free</a>.
5052

53+
- [LTS versions of Node.js](https://github.com/nodejs/release#release-schedule)
54+
- [TypeScript](https://www.typescriptlang.org/download/)
55+
5156
- An Azure OpenAI resource deployed in a [supported region and with a supported model](./concepts/use-your-data.md#regional-availability-and-model-support).
5257

53-
- Be sure that you are assigned at least the [Cognitive Services Contributor](./how-to/role-based-access-control.md#cognitive-services-contributor) role for the Azure OpenAI resource.
58+
- 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.
5459

5560
- Download the example data from [GitHub](https://github.com/Azure-Samples/cognitive-services-sample-data-files/blob/master/openai/contoso_benefits_document_example.pdf) if you don't have your own data.
5661

62+
#### [JavaScript](#tab/javascript)
63+
64+
- An Azure subscription - <a href="https://azure.microsoft.com/free/cognitive-services" target="_blank">Create one for free</a>.
65+
- [LTS versions of Node.js](https://github.com/nodejs/release#release-schedule)
66+
- An Azure OpenAI resource deployed in a [supported region and with a supported model](./concepts/use-your-data.md#regional-availability-and-model-support).
67+
68+
- 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.
69+
70+
- Download the example data from [GitHub](https://github.com/Azure-Samples/cognitive-services-sample-data-files/blob/master/openai/contoso_benefits_document_example.pdf) if you don't have your own data.
71+
72+
---
73+
5774
::: zone pivot="programming-language-javascript"
5875

76+
77+
5978
- [Long Term Support (LTS) versions of Node.js](https://github.com/nodejs/release#release-schedule)
6079

6180
::: zone-end

0 commit comments

Comments
 (0)