Skip to content

Commit 2a09d4c

Browse files
committed
indent code blocks
1 parent 30135ae commit 2a09d4c

File tree

1 file changed

+151
-151
lines changed

1 file changed

+151
-151
lines changed

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

Lines changed: 151 additions & 151 deletions
Original file line numberDiff line numberDiff line change
@@ -44,85 +44,85 @@ Your app's _package.json_ file will be updated with the dependencies.
4444
#### [TypeScript](#tab/typescript)
4545

4646
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 "@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 apiKey = process.env["AZURE_OPENAI_API_KEY"];
56-
const searchEndpoint = process.env["AZURE_AI_SEARCH_ENDPOINT"];
57-
const searchKey = process.env["AZURE_AI_SEARCH_API_KEY"];
58-
const searchIndex = process.env["AZURE_AI_SEARCH_INDEX"];
59-
60-
// Required Azure OpenAI deployment name and API version
61-
const deploymentName = "gpt-4";
62-
const apiVersion = "2024-07-01-preview";
63-
64-
function getClient(): AzureOpenAI {
65-
return new AzureOpenAI({
66-
endpoint,
67-
apiKey: apiKey,
68-
deployment: deploymentName,
69-
apiVersion,
70-
});
71-
}
72-
73-
async function main() {
74-
const client = getClient();
75-
76-
const messages = [
77-
{ role: "user", content: "What are my available health plans?" },
78-
];
79-
80-
console.log(`Message: ${messages.map((m) => m.content).join("\n")}`);
81-
82-
const events = await client.chat.completions.create({
83-
stream: true,
84-
messages: [
85-
{
86-
role: "user",
87-
content:
88-
"What's the most common feedback we received from our customers about the product?",
89-
},
90-
],
91-
max_tokens: 128,
92-
model: "",
93-
data_sources: [
94-
{
95-
type: "azure_search",
96-
parameters: {
97-
endpoint: searchEndpoint,
98-
index_name: searchIndex,
99-
authentication: {
100-
type: "api_key",
101-
key: searchKey,
47+
48+
```typescript
49+
import "dotenv/config";
50+
import { AzureOpenAI } from "openai";
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 apiKey = process.env["AZURE_OPENAI_API_KEY"];
56+
const searchEndpoint = process.env["AZURE_AI_SEARCH_ENDPOINT"];
57+
const searchKey = process.env["AZURE_AI_SEARCH_API_KEY"];
58+
const searchIndex = process.env["AZURE_AI_SEARCH_INDEX"];
59+
60+
// Required Azure OpenAI deployment name and API version
61+
const deploymentName = "gpt-4";
62+
const apiVersion = "2024-07-01-preview";
63+
64+
function getClient(): AzureOpenAI {
65+
return new AzureOpenAI({
66+
endpoint,
67+
apiKey: apiKey,
68+
deployment: deploymentName,
69+
apiVersion,
70+
});
71+
}
72+
73+
async function main() {
74+
const client = getClient();
75+
76+
const messages = [
77+
{ role: "user", content: "What are my available health plans?" },
78+
];
79+
80+
console.log(`Message: ${messages.map((m) => m.content).join("\n")}`);
81+
82+
const events = await client.chat.completions.create({
83+
stream: true,
84+
messages: [
85+
{
86+
role: "user",
87+
content:
88+
"What's the most common feedback we received from our customers about the product?",
89+
},
90+
],
91+
max_tokens: 128,
92+
model: "",
93+
data_sources: [
94+
{
95+
type: "azure_search",
96+
parameters: {
97+
endpoint: searchEndpoint,
98+
index_name: searchIndex,
99+
authentication: {
100+
type: "api_key",
101+
key: searchKey,
102+
},
103+
},
102104
},
103-
},
104-
},
105-
],
106-
});
107-
108-
let response = "";
109-
for await (const event of events) {
110-
for (const choice of event.choices) {
111-
const newText = choice.delta?.content;
112-
if (newText) {
113-
response += newText;
114-
// To see streaming results as they arrive, uncomment line below
115-
// console.log(newText);
105+
],
106+
});
107+
108+
let response = "";
109+
for await (const event of events) {
110+
for (const choice of event.choices) {
111+
const newText = choice.delta?.content;
112+
if (newText) {
113+
response += newText;
114+
// To see streaming results as they arrive, uncomment line below
115+
// console.log(newText);
116+
}
117+
}
116118
}
119+
console.log(response);
117120
}
118-
}
119-
console.log(response);
120-
}
121-
122-
main().catch((err) => {
123-
console.error("The sample encountered an error:", err);
124-
});
125-
```
121+
122+
main().catch((err) => {
123+
console.error("The sample encountered an error:", err);
124+
});
125+
```
126126

127127
The `@azure/openai/types` dependency is included to extend the OpenAI model for the `data_sources` property. This import is only necessary for TypeScript.
128128

@@ -141,84 +141,84 @@ The `@azure/openai/types` dependency is included to extend the OpenAI model for
141141
#### [JavaScript](#tab/javascript)
142142

143143
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: 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,
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: 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+
},
198200
},
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);
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+
}
212214
}
215+
console.log(response);
213216
}
214-
}
215-
console.log(response);
216-
}
217-
218-
main().catch((err) => {
219-
console.error("The sample encountered an error:", err);
220-
});
221-
```
217+
218+
main().catch((err) => {
219+
console.error("The sample encountered an error:", err);
220+
});
221+
```
222222

223223
1. Run the application with the following command:
224224

0 commit comments

Comments
 (0)