Skip to content

Commit d729796

Browse files
committed
edits
1 parent 7f2bafe commit d729796

File tree

2 files changed

+8
-275
lines changed

2 files changed

+8
-275
lines changed

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

Lines changed: 4 additions & 136 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ Your app's _package.json_ file will be updated with the dependencies.
6868

6969
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.
7070

71-
## [**TypeScript (Entra ID)**](#tab/typescript-keyless)
71+
## [**TypeScript (Microsoft Entra ID)**](#tab/typescript-keyless)
7272

7373
```typescript
7474
import { AzureOpenAI } from "openai";
@@ -81,10 +81,6 @@ import type {
8181
ChatCompletionCreateParamsNonStreaming,
8282
} from "openai/resources/index";
8383

84-
// Load the .env file if it exists
85-
const dotenv = require("dotenv");
86-
dotenv.config();
87-
8884
// You will need to set these environment variables or edit the following values
8985
const endpoint = process.env["AZURE_OPENAI_ENDPOINT"] || "<endpoint>";
9086
const apiKey = process.env["AZURE_OPENAI_API_KEY"] || "<api key>";
@@ -153,18 +149,14 @@ Run the script with the following command:
153149
node.exe Completion.js
154150
```
155151

156-
## [**JavaScript (Entra id)**](#tab/javascript-keyless)
152+
## [**JavaScript (Microsoft Entra ID)**](#tab/javascript-keyless)
157153

158154
```javascript
159155
const { AzureOpenAI } = require("openai");
160-
import {
156+
const {
161157
DefaultAzureCredential,
162158
getBearerTokenProvider
163-
} from "@azure/identity";
164-
165-
// Load the .env file if it exists
166-
const dotenv = require("dotenv");
167-
dotenv.config();
159+
} = require("@azure/identity");
168160

169161
// You will need to set these environment variables or edit the following values
170162
const endpoint = process.env["AZURE_OPENAI_ENDPOINT"] || "<endpoint>";
@@ -218,10 +210,6 @@ import type {
218210
ChatCompletionCreateParamsNonStreaming,
219211
} from "openai/resources/index";
220212

221-
// Load the .env file if it exists
222-
const dotenv = require("dotenv");
223-
dotenv.config();
224-
225213
// You will need to set these environment variables or edit the following values
226214
const endpoint = process.env["AZURE_OPENAI_ENDPOINT"] || "<endpoint>";
227215
const apiKey = process.env["AZURE_OPENAI_API_KEY"] || "<api key>";
@@ -290,10 +278,6 @@ node.exe Completion.js
290278
```javascript
291279
const { AzureOpenAI } = require("openai");
292280

293-
// Load the .env file if it exists
294-
const dotenv = require("dotenv");
295-
dotenv.config();
296-
297281
// You will need to set these environment variables or edit the following values
298282
const endpoint = process.env["AZURE_OPENAI_ENDPOINT"] || "<endpoint>";
299283
const apiKey = process.env["AZURE_OPENAI_API_KEY"] || "<api key>";
@@ -343,122 +327,6 @@ node.exe ChatCompletion.js
343327
}
344328
```
345329

346-
## Microsoft Entra ID
347-
348-
> [!IMPORTANT]
349-
> 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).
350-
351-
## [**TypeScript**](#tab/typescript)
352-
353-
```typescript
354-
import {
355-
DefaultAzureCredential,
356-
getBearerTokenProvider,
357-
} from "@azure/identity";
358-
import "dotenv/config";
359-
import { AzureOpenAI } from "openai";
360-
import type {
361-
ChatCompletion,
362-
ChatCompletionCreateParamsNonStreaming,
363-
} from "openai/resources/index";
364-
365-
// You will need to set these environment variables or edit the following values
366-
const endpoint = process.env["AZURE_OPENAI_ENDPOINT"] || "<endpoint>";
367-
368-
// Required Azure OpenAI deployment name and API version
369-
const apiVersion = "2024-08-01-preview";
370-
const deploymentName = "gpt-4o-mini"; //This must match your deployment name.
371-
372-
function getClient(): AzureOpenAI {
373-
const scope = "https://cognitiveservices.azure.com/.default";
374-
const azureADTokenProvider = getBearerTokenProvider(
375-
new DefaultAzureCredential(),
376-
scope
377-
);
378-
return new AzureOpenAI({
379-
endpoint,
380-
azureADTokenProvider,
381-
deployment: deploymentName,
382-
apiVersion,
383-
});
384-
}
385-
386-
function createMessages(): ChatCompletionCreateParamsNonStreaming {
387-
return {
388-
messages: [
389-
{ role: "system", content: "You are a helpful assistant." },
390-
{
391-
role: "user",
392-
content: "Does Azure OpenAI support customer managed keys?",
393-
},
394-
{
395-
role: "assistant",
396-
content: "Yes, customer managed keys are supported by Azure OpenAI?",
397-
},
398-
{ role: "user", content: "Do other Azure AI services support this too?" },
399-
],
400-
model: "",
401-
};
402-
}
403-
async function printChoices(completion: ChatCompletion): Promise<void> {
404-
for (const choice of completion.choices) {
405-
console.log(choice.message);
406-
}
407-
}
408-
export async function main() {
409-
const client = getClient();
410-
const messages = createMessages();
411-
const result = await client.chat.completions.create(messages);
412-
await printChoices(result);
413-
}
414-
415-
main().catch((err) => {
416-
console.error("The sample encountered an error:", err);
417-
});
418-
```
419-
420-
421-
## [**JavaScript**](#tab/javascript)
422-
423-
```javascript
424-
const { AzureOpenAI } = require("openai");
425-
const { DefaultAzureCredential, getBearerTokenProvider } = require("@azure/identity");
426-
427-
// Set AZURE_OPENAI_ENDPOINT to the endpoint of your
428-
// OpenAI resource. You can find this in the Azure portal.
429-
// Load the .env file if it exists
430-
require("dotenv/config");
431-
432-
async function main() {
433-
console.log("== Chat Completions Sample ==");
434-
435-
const scope = "https://cognitiveservices.azure.com/.default";
436-
const azureADTokenProvider = getBearerTokenProvider(new DefaultAzureCredential(), scope);
437-
const deployment = "gpt-35-turbo";
438-
const apiVersion = "2024-04-01-preview";
439-
const client = new AzureOpenAI({ azureADTokenProvider, deployment, apiVersion });
440-
const result = await client.chat.completions.create({
441-
messages: [
442-
{ role: "system", content: "You are a helpful assistant." },
443-
{ role: "user", content: "Does Azure OpenAI support customer managed keys?" },
444-
{ role: "assistant", content: "Yes, customer managed keys are supported by Azure OpenAI?" },
445-
{ role: "user", content: "Do other Azure AI services support this too?" },
446-
],
447-
model: "",
448-
});
449-
450-
for (const choice of result.choices) {
451-
console.log(choice.message);
452-
}
453-
}
454-
455-
main().catch((err) => {
456-
console.error("The sample encountered an error:", err);
457-
});
458-
459-
module.exports = { main };
460-
```
461-
462330
---
463331

464332
> [!NOTE]

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

Lines changed: 4 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ Your app's _package.json_ file will be updated with the dependencies.
6666

6767
Open a command prompt where you created the new project, and create a new file named Completion.js. Copy the following code into the Completion.js file.
6868

69-
## [**TypeScript (Entra id)**](#tab/typescript-keyless)
69+
## [**TypeScript (Microsoft Entra ID)**](#tab/typescript-keyless)
7070

7171
```typescript
7272
import {
@@ -76,13 +76,8 @@ import {
7676
import { AzureOpenAI } from "openai";
7777
import { type Completion } from "openai/resources/index";
7878

79-
// Load the .env file if it exists
80-
const dotenv = require("dotenv");
81-
dotenv.config();
82-
8379
// You will need to set these environment variables or edit the following values
8480
const endpoint = process.env["AZURE_OPENAI_ENDPOINT"] || "<endpoint>";
85-
const apiKey = process.env["AZURE_OPENAI_API_KEY"] || "<api key>";
8681

8782
// Required Azure OpenAI deployment name and API version
8883
const apiVersion = "2024-08-01-preview";
@@ -146,22 +141,17 @@ Run the script with the following command:
146141
node.exe Completion.js
147142
```
148143

149-
## [**JavaScript (Entra id)**](#tab/javascript-keyless)
144+
## [**JavaScript (Microsoft Entra ID)**](#tab/javascript-keyless)
150145

151146
```javascript
152147
const { AzureOpenAI } = require("openai");
153-
import {
148+
const {
154149
DefaultAzureCredential,
155150
getBearerTokenProvider
156-
} from "@azure/identity";
157-
158-
// Load the .env file if it exists
159-
const dotenv = require("dotenv");
160-
dotenv.config();
151+
} = require("@azure/identity");
161152

162153
// You will need to set these environment variables or edit the following values
163154
const endpoint = process.env["AZURE_OPENAI_ENDPOINT"] || "<endpoint>";
164-
const apiKey = process.env["AZURE_OPENAI_API_KEY"] || "<api key>";
165155
const apiVersion = "2024-04-01-preview";
166156
const deployment = "gpt-35-turbo-instruct"; //The deployment name for your completions API model. The instruct model is the only new model that supports the legacy API.
167157

@@ -200,7 +190,6 @@ node.exe Completion.js
200190
## [**TypeScript (API key)**](#tab/typescript-key)
201191

202192
```typescript
203-
import "dotenv/config";
204193
import { AzureOpenAI } from "openai";
205194
import { type Completion } from "openai/resources/index";
206195

@@ -270,10 +259,6 @@ node.exe Completion.js
270259
```javascript
271260
const { AzureOpenAI } = require("openai");
272261

273-
// Load the .env file if it exists
274-
const dotenv = require("dotenv");
275-
dotenv.config();
276-
277262
// You will need to set these environment variables or edit the following values
278263
const endpoint = process.env["AZURE_OPENAI_ENDPOINT"] || "<endpoint>";
279264
const apiKey = process.env["AZURE_OPENAI_API_KEY"] || "<api key>";
@@ -317,126 +302,6 @@ node.exe Completion.js
317302
Microsoft was founded on April 4, 1975.
318303
```
319304

320-
## Microsoft Entra ID
321-
322-
> [!IMPORTANT]
323-
> 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).
324-
325-
## [**TypeScript**](#tab/typescript)
326-
327-
```typescript
328-
import {
329-
DefaultAzureCredential,
330-
getBearerTokenProvider,
331-
} from "@azure/identity";
332-
import "dotenv/config";
333-
import { AzureOpenAI } from "openai";
334-
import { type Completion } from "openai/resources/index";
335-
336-
// You will need to set these environment variables or edit the following values
337-
const endpoint = process.env["AZURE_OPENAI_ENDPOINT"] || "<endpoint>";
338-
339-
// Required Azure OpenAI deployment name and API version
340-
const apiVersion = "2024-08-01-preview";
341-
const deploymentName = "gpt-35-turbo-instruct";
342-
343-
// Chat prompt and max tokens
344-
const prompt = ["When was Microsoft founded?"];
345-
const maxTokens = 128;
346-
347-
function getClient(): AzureOpenAI {
348-
const scope = "https://cognitiveservices.azure.com/.default";
349-
const azureADTokenProvider = getBearerTokenProvider(
350-
new DefaultAzureCredential(),
351-
scope
352-
);
353-
return new AzureOpenAI({
354-
endpoint,
355-
azureADTokenProvider,
356-
deployment: deploymentName,
357-
apiVersion,
358-
});
359-
}
360-
async function getCompletion(
361-
client: AzureOpenAI,
362-
prompt: string[],
363-
max_tokens: number
364-
): Promise<Completion> {
365-
return client.completions.create({
366-
prompt,
367-
model: "",
368-
max_tokens,
369-
});
370-
}
371-
async function printChoices(completion: Completion): Promise<void> {
372-
for (const choice of completion.choices) {
373-
console.log(choice.text);
374-
}
375-
}
376-
export async function main() {
377-
console.log("== Get completions Sample ==");
378-
379-
const client = getClient();
380-
const completion = await getCompletion(client, prompt, maxTokens);
381-
await printChoices(completion);
382-
}
383-
384-
main().catch((err) => {
385-
console.error("Error occurred:", err);
386-
});
387-
388-
```
389-
390-
Build the script with the following command:
391-
392-
```cmd
393-
tsc
394-
```
395-
396-
Run the script with the following command:
397-
398-
```cmd
399-
node.exe Completion.js
400-
```
401-
402-
403-
## [**JavaScript**](#tab/javascript)
404-
405-
```javascript
406-
const { AzureOpenAI } = require("openai");
407-
const { DefaultAzureCredential, getBearerTokenProvider } = require("@azure/identity");
408-
409-
// Set AZURE_OPENAI_ENDPOINT to the endpoint of your
410-
// OpenAI resource. You can find this in the Azure portal.
411-
// Load the .env file if it exists
412-
require("dotenv/config");
413-
414-
const prompt = ["When was Microsoft founded?"];
415-
416-
async function main() {
417-
console.log("== Get completions Sample ==");
418-
419-
const scope = "https://cognitiveservices.azure.com/.default";
420-
const azureADTokenProvider = getBearerTokenProvider(new DefaultAzureCredential(), scope);
421-
const deployment = "gpt-35-turbo-instruct";
422-
const apiVersion = "2024-04-01-preview";
423-
const client = new AzureOpenAI({ azureADTokenProvider, deployment, apiVersion });
424-
const result = await client.completions.create({ prompt, model: deployment, max_tokens: 128 });
425-
426-
for (const choice of result.choices) {
427-
console.log(choice.text);
428-
}
429-
}
430-
431-
main().catch((err) => {
432-
console.error("Error occurred:", err);
433-
});
434-
435-
module.exports = { main };
436-
```
437-
438-
---
439-
440305
> [!NOTE]
441306
> If your receive the error: *Error occurred: OpenAIError: The `apiKey` and `azureADTokenProvider` arguments are mutually exclusive; only one can be passed at a time.* You may need to remove a pre-existing environment variable for the API key from your system. Even though the Microsoft Entra ID code sample is not explicitly referencing the API key environment variable, if one is present on the system executing this sample, this error will still be generated.
442307

0 commit comments

Comments
 (0)