@@ -13,7 +13,7 @@ ms.date: 09/06/2024
13
13
14
14
Use this guide to get started generating images with the Azure OpenAI SDK for JavaScript.
15
15
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 )
17
17
18
18
## Prerequisites
19
19
@@ -52,22 +52,10 @@ npm init
52
52
53
53
Install the client libraries with:
54
54
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
-
65
55
``` console
66
56
npm install openai @azure/identity
67
57
```
68
58
69
- ---
70
-
71
59
Your app's _ package.json_ file will be updated with the dependencies.
72
60
73
61
## Generate images with DALL-E
@@ -77,6 +65,50 @@ Create a new file named _ImageGeneration.js_ and open it in your preferred code
77
65
#### [ TypeScript] ( #tab/typescript )
78
66
79
67
``` 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
+ });
80
112
```
81
113
82
114
1 . Build the application with the following command:
0 commit comments