Skip to content

Commit 86291e3

Browse files
committed
code added
1 parent 8c87166 commit 86291e3

File tree

1 file changed

+45
-13
lines changed

1 file changed

+45
-13
lines changed

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

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ ms.date: 09/06/2024
1313

1414
Use this guide to get started generating images with the Azure OpenAI SDK for JavaScript.
1515

16-
[Library source code](https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/openai/openai) | [Package (npm)](https://www.npmjs.com/package/@azure/openai) | [Samples](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/openai/Azure.AI.OpenAI/tests/Samples)
16+
[Reference documentation](https://platform.openai.com/docs/api-reference/images/create) | [Source code](https://github.com/openai/openai-node) | [Package (npm)](https://www.npmjs.com/package/openai) | [Samples](https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/openai/openai/samples)
1717

1818
## Prerequisites
1919

@@ -52,22 +52,10 @@ npm init
5252

5353
Install the client libraries with:
5454

55-
## [**TypeScript**](#tab/typescript)
56-
57-
```console
58-
npm install openai @azure/openai @azure/identity
59-
```
60-
61-
The `@azure/openai` package provides the types the Azure service objects.
62-
63-
## [**JavaScript**](#tab/javascript)
64-
6555
```console
6656
npm install openai @azure/identity
6757
```
6858

69-
---
70-
7159
Your app's _package.json_ file will be updated with the dependencies.
7260

7361
## Generate images with DALL-E
@@ -77,6 +65,50 @@ Create a new file named _ImageGeneration.js_ and open it in your preferred code
7765
#### [TypeScript](#tab/typescript)
7866

7967
```typescript
68+
import "dotenv/config";
69+
import { AzureOpenAI } from "openai";
70+
71+
// You will need to set these environment variables or edit the following values
72+
const endpoint = process.env["AZURE_OPENAI_ENDPOINT"];
73+
const apiKey = process.env["AZURE_OPENAI_API_KEY"];
74+
75+
// Required Azure OpenAI deployment name and API version
76+
const apiVersion = "2024-07-01";
77+
const deploymentName = "dall-e-3";
78+
79+
// The prompt to generate images from
80+
const prompt = "a monkey eating a banana";
81+
const numberOfImagesToGenerate = 1;
82+
83+
function getClient(): AzureOpenAI {
84+
return new AzureOpenAI({
85+
endpoint,
86+
apiKey,
87+
apiVersion,
88+
deployment: deploymentName,
89+
});
90+
}
91+
async function main() {
92+
console.log("== Image Generation ==");
93+
94+
const client = getClient();
95+
96+
const results = await client.images.generate({
97+
prompt,
98+
size: "1024x1024",
99+
n: numberOfImagesToGenerate,
100+
model: "",
101+
style: "vivid", // or "natural"
102+
});
103+
104+
for (const image of results.data) {
105+
console.log(`Image generation result URL: ${image.url}`);
106+
}
107+
}
108+
109+
main().catch((err) => {
110+
console.error("The sample encountered an error:", err);
111+
});
80112
```
81113

82114
1. Build the application with the following command:

0 commit comments

Comments
 (0)