Skip to content

Commit 6cd14d6

Browse files
committed
Quickstart JS - entra for JS/TS
1 parent 85a168b commit 6cd14d6

File tree

2 files changed

+330
-8
lines changed

2 files changed

+330
-8
lines changed

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

Lines changed: 136 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ ms.service: azure-ai-openai
88
ms.topic: include
99
author: PatrickFarley
1010
ms.author: pafarley
11-
ms.date: 09/06/2024
11+
ms.date: 10/23/2024
1212
---
1313

1414
Use this guide to get started generating images with the Azure OpenAI SDK for JavaScript.
@@ -22,13 +22,15 @@ Use this guide to get started generating images with the Azure OpenAI SDK for Ja
2222
- An Azure subscription - [Create one for free](https://azure.microsoft.com/free/cognitive-services?azure-portal=true)
2323
- [LTS versions of Node.js](https://github.com/nodejs/release#release-schedule)
2424
- [TypeScript](https://www.typescriptlang.org/download/)
25+
- [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.
2526
- An Azure OpenAI resource created in a supported region (see [Region availability](/azure/ai-services/openai/concepts/models#model-summary-table-and-region-availability)). For more information, see [Create a resource and deploy a model with Azure OpenAI](../how-to/create-resource.md).
2627

2728

2829
#### [JavaScript](#tab/javascript)
2930

3031
- An Azure subscription - [Create one for free](https://azure.microsoft.com/free/cognitive-services?azure-portal=true)
3132
- [LTS versions of Node.js](https://github.com/nodejs/release#release-schedule)
33+
- [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.
3234
- An Azure OpenAI resource created in a supported region (see [Region availability](/azure/ai-services/openai/concepts/models#model-summary-table-and-region-availability)). For more information, see [Create a resource and deploy a model with Azure OpenAI](../how-to/create-resource.md).
3335

3436
---
@@ -62,7 +64,137 @@ Your app's _package.json_ file will be updated with the dependencies.
6264

6365
Create a new file named _ImageGeneration.js_ and open it in your preferred code editor. Copy the following code into the _ImageGeneration.js_ file:
6466

65-
#### [TypeScript](#tab/typescript)
67+
#### [TypeScript (Microsoft Entra Id)](#tab/typescript-keyless)
68+
69+
```typescript
70+
import "dotenv/config";
71+
import { AzureOpenAI } from "openai";
72+
import {
73+
DefaultAzureCredential,
74+
getBearerTokenProvider
75+
} from "@azure/identity";
76+
77+
// You will need to set these environment variables or edit the following values
78+
const endpoint = process.env["AZURE_OPENAI_ENDPOINT"];
79+
80+
// Required Azure OpenAI deployment name and API version
81+
const apiVersion = "2024-07-01";
82+
const deploymentName = "dall-e-3";
83+
84+
// keyless authentication
85+
const credential = new DefaultAzureCredential();
86+
const scope = "https://cognitiveservices.azure.com/.default";
87+
const azureADTokenProvider = getBearerTokenProvider(credential, scope);
88+
89+
function getClient(): AzureOpenAI {
90+
return new AzureOpenAI({
91+
endpoint,
92+
azureADTokenProvider,
93+
apiVersion,
94+
deployment: deploymentName,
95+
});
96+
}
97+
async function main() {
98+
console.log("== Image Generation ==");
99+
100+
const client = getClient();
101+
102+
const results = await client.images.generate({
103+
prompt,
104+
size: "1024x1024",
105+
n: numberOfImagesToGenerate,
106+
model: "",
107+
style: "vivid", // or "natural"
108+
});
109+
110+
for (const image of results.data) {
111+
console.log(`Image generation result URL: ${image.url}`);
112+
}
113+
}
114+
115+
main().catch((err) => {
116+
console.error("The sample encountered an error:", err);
117+
});
118+
```
119+
120+
1. Build the application with the following command:
121+
122+
```console
123+
tsc
124+
```
125+
126+
1. Run the application with the following command:
127+
128+
```console
129+
node ImageGeneration.js
130+
```
131+
132+
133+
#### [JavaScript (API key)](#tab/javascript-key)
134+
135+
```javascript
136+
require("dotenv/config");
137+
const { AzureOpenAI } = require("openai");
138+
const {
139+
DefaultAzureCredential,
140+
getBearerTokenProvider
141+
} = require("@azure/identity");
142+
143+
// You will need to set these environment variables or edit the following values
144+
const endpoint = process.env["AZURE_OPENAI_ENDPOINT"];
145+
146+
// Required Azure OpenAI deployment name and API version
147+
const apiVersion = "2024-07-01";
148+
const deploymentName = "dall-e-3";
149+
150+
// The prompt to generate images from
151+
const prompt = "a monkey eating a banana";
152+
const numberOfImagesToGenerate = 1;
153+
154+
// keyless authentication
155+
const credential = new DefaultAzureCredential();
156+
const scope = "https://cognitiveservices.azure.com/.default";
157+
const azureADTokenProvider = getBearerTokenProvider(credential, scope);
158+
159+
function getClient(): AzureOpenAI {
160+
return new AzureOpenAI({
161+
endpoint,
162+
azureADTokenProvider,
163+
apiVersion,
164+
deployment: deploymentName,
165+
});
166+
}
167+
async function main() {
168+
console.log("== Image Generation ==");
169+
170+
const client = getClient();
171+
172+
const results = await client.images.generate({
173+
prompt,
174+
size: "1024x1024",
175+
n: numberOfImagesToGenerate,
176+
model: "",
177+
style: "vivid", // or "natural"
178+
});
179+
180+
for (const image of results.data) {
181+
console.log(`Image generation result URL: ${image.url}`);
182+
}
183+
}
184+
185+
main().catch((err) => {
186+
console.error("The sample encountered an error:", err);
187+
});
188+
```
189+
190+
Run the script with the following command:
191+
192+
```console
193+
node ImageGeneration.js
194+
```
195+
196+
197+
#### [TypeScript (API key)](#tab/typescript-key)
66198

67199
```typescript
68200
import "dotenv/config";
@@ -124,7 +256,7 @@ main().catch((err) => {
124256
```
125257

126258

127-
#### [JavaScript](#tab/javascript)
259+
#### [JavaScript (API key)](#tab/javascript-key)
128260

129261
```javascript
130262
require("dotenv/config");
@@ -178,6 +310,7 @@ Run the script with the following command:
178310
```console
179311
node ImageGeneration.js
180312
```
313+
181314
---
182315

183316
## Output

0 commit comments

Comments
 (0)