Skip to content

Commit a4036b4

Browse files
committed
edits
1 parent 6aa3252 commit a4036b4

File tree

4 files changed

+268
-215
lines changed

4 files changed

+268
-215
lines changed

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

Lines changed: 3 additions & 205 deletions
Original file line numberDiff line numberDiff line change
@@ -23,123 +23,15 @@ 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-
3626
```console
3727
npm install @azure/openai @azure/identity
3828
```
3929

40-
---
41-
4230
Your app's _package.json_ file will be updated with the dependencies.
4331

44-
#### [TypeScript (Microsoft Entra ID)](#tab/typescript-keyless)
45-
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 { AzureOpenAI } from "openai";
50-
import { DefaultAzureCredential, getBearerTokenProvider } from "@azure/identity";
51-
import "@azure/openai/types";
52-
53-
// Set the Azure and AI Search values from environment variables
54-
const endpoint = process.env["AZURE_OPENAI_ENDPOINT"];
55-
const searchEndpoint = process.env["AZURE_AI_SEARCH_ENDPOINT"];
56-
const searchIndex = process.env["AZURE_AI_SEARCH_INDEX"];
57-
58-
// keyless authentication
59-
const credential = new DefaultAzureCredential();
60-
const scope = "https://cognitiveservices.azure.com/.default";
61-
const azureADTokenProvider = getBearerTokenProvider(credential, scope);
62-
63-
// Required Azure OpenAI deployment name and API version
64-
const deploymentName = "gpt-4";
65-
const apiVersion = "2024-07-01-preview";
66-
67-
function getClient(): AzureOpenAI {
68-
return new AzureOpenAI({
69-
endpoint,
70-
azureADTokenProvider,
71-
deployment: deploymentName,
72-
apiVersion,
73-
});
74-
}
75-
76-
async function main() {
77-
const client = getClient();
78-
79-
const messages = [
80-
{ role: "user", content: "What are my available health plans?" },
81-
];
82-
83-
console.log(`Message: ${messages.map((m) => m.content).join("\n")}`);
84-
85-
const events = await client.chat.completions.create({
86-
stream: true,
87-
messages: [
88-
{
89-
role: "user",
90-
content:
91-
"What's the most common feedback we received from our customers about the product?",
92-
},
93-
],
94-
max_tokens: 128,
95-
model: "",
96-
data_sources: [
97-
{
98-
type: "azure_search",
99-
parameters: {
100-
endpoint: searchEndpoint,
101-
index_name: searchIndex,
102-
authentication: {
103-
type: "api_key",
104-
key: searchKey,
105-
},
106-
},
107-
},
108-
],
109-
});
110-
111-
let response = "";
112-
for await (const event of events) {
113-
for (const choice of event.choices) {
114-
const newText = choice.delta?.content;
115-
if (newText) {
116-
response += newText;
117-
// To see streaming results as they arrive, uncomment line below
118-
// console.log(newText);
119-
}
120-
}
121-
}
122-
console.log(response);
123-
}
124-
125-
main().catch((err) => {
126-
console.error("The sample encountered an error:", err);
127-
});
128-
```
129-
130-
1. Build the application with the following command:
131-
132-
```console
133-
tsc
134-
```
135-
136-
1. Run the application with the following command:
137-
138-
```console
139-
node ChatWithOwnData.js
140-
```
32+
## Create a new JavaScript application
14133

142-
#### [JavaScript (Microsoft Entra ID)](#tab/javascript-keyless)
34+
#### [Microsoft Entra ID](#tab/javascript-keyless)
14335

14436
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.
14537

@@ -231,101 +123,7 @@ Your app's _package.json_ file will be updated with the dependencies.
231123
```
232124
233125
234-
#### [TypeScript (API key)](#tab/typescript-key)
235-
236-
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.
237-
238-
```typescript
239-
import { AzureOpenAI } from "openai";
240-
import "@azure/openai/types";
241-
242-
// Set the Azure and AI Search values from environment variables
243-
const endpoint = process.env["AZURE_OPENAI_ENDPOINT"];
244-
const apiKey = process.env["AZURE_OPENAI_API_KEY"];
245-
const searchEndpoint = process.env["AZURE_AI_SEARCH_ENDPOINT"];
246-
const searchKey = process.env["AZURE_AI_SEARCH_API_KEY"];
247-
const searchIndex = process.env["AZURE_AI_SEARCH_INDEX"];
248-
249-
// Required Azure OpenAI deployment name and API version
250-
const deploymentName = "gpt-4";
251-
const apiVersion = "2024-07-01-preview";
252-
253-
function getClient(): AzureOpenAI {
254-
return new AzureOpenAI({
255-
endpoint,
256-
apiKey,
257-
deployment: deploymentName,
258-
apiVersion,
259-
});
260-
}
261-
262-
async function main() {
263-
const client = getClient();
264-
265-
const messages = [
266-
{ role: "user", content: "What are my available health plans?" },
267-
];
268-
269-
console.log(`Message: ${messages.map((m) => m.content).join("\n")}`);
270-
271-
const events = await client.chat.completions.create({
272-
stream: true,
273-
messages: [
274-
{
275-
role: "user",
276-
content:
277-
"What's the most common feedback we received from our customers about the product?",
278-
},
279-
],
280-
max_tokens: 128,
281-
model: "",
282-
data_sources: [
283-
{
284-
type: "azure_search",
285-
parameters: {
286-
endpoint: searchEndpoint,
287-
index_name: searchIndex,
288-
authentication: {
289-
type: "api_key",
290-
key: searchKey,
291-
},
292-
},
293-
},
294-
],
295-
});
296-
297-
let response = "";
298-
for await (const event of events) {
299-
for (const choice of event.choices) {
300-
const newText = choice.delta?.content;
301-
if (newText) {
302-
response += newText;
303-
// To see streaming results as they arrive, uncomment line below
304-
// console.log(newText);
305-
}
306-
}
307-
}
308-
console.log(response);
309-
}
310-
311-
main().catch((err) => {
312-
console.error("The sample encountered an error:", err);
313-
});
314-
```
315-
316-
1. Build the application with the following command:
317-
318-
```console
319-
tsc
320-
```
321-
322-
1. Run the application with the following command:
323-
324-
```console
325-
node ChatWithOwnData.js
326-
```
327-
328-
#### [JavaScript (API key)](#tab/javascript-key)
126+
#### [API key](#tab/javascript-key)
329127
330128
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.
331129

0 commit comments

Comments
 (0)