Skip to content

Commit 2d3c5ab

Browse files
committed
add js back
1 parent 86291e3 commit 2d3c5ab

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,50 @@ main().catch((err) => {
127127
#### [JavaScript](#tab/javascript)
128128

129129
```javascript
130+
require("dotenv/config");
131+
const { AzureOpenAI } = require("openai");
132+
133+
// You will need to set these environment variables or edit the following values
134+
const endpoint = process.env["AZURE_OPENAI_ENDPOINT"];
135+
const apiKey = process.env["AZURE_OPENAI_API_KEY"];
136+
137+
// Required Azure OpenAI deployment name and API version
138+
const apiVersion = "2024-07-01";
139+
const deploymentName = "dall-e-3";
140+
141+
// The prompt to generate images from
142+
const prompt = "a monkey eating a banana";
143+
const numberOfImagesToGenerate = 1;
144+
145+
function getClient() {
146+
return new AzureOpenAI({
147+
endpoint,
148+
apiKey,
149+
apiVersion,
150+
deployment: deploymentName,
151+
});
152+
}
153+
async function main() {
154+
console.log("== Image Generation ==");
155+
156+
const client = getClient();
157+
158+
const results = await client.images.generate({
159+
prompt,
160+
size: "1024x1024",
161+
n: numberOfImagesToGenerate,
162+
model: "",
163+
style: "vivid", // or "natural"
164+
});
165+
166+
for (const image of results.data) {
167+
console.log(`Image generation result URL: ${image.url}`);
168+
}
169+
}
170+
171+
main().catch((err) => {
172+
console.error("The sample encountered an error:", err);
173+
});
130174
```
131175

132176
Run the script with the following command:

0 commit comments

Comments
 (0)