@@ -20,100 +20,225 @@ Use this guide to get started generating images with the Azure OpenAI SDK for Go
20
20
- 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 ) .
21
21
22
22
23
- ## Setup
23
+ ### Microsoft Entra ID prerequisites
24
24
25
- [ !INCLUDE [ get-key-endpoint] ( get-key-endpoint.md )]
25
+ For the recommended keyless authentication with Microsoft Entra ID, you need to:
26
+ - Install the [ Azure CLI] ( /cli/azure/install-azure-cli ) used for keyless authentication with Microsoft Entra ID.
27
+ - Assign the ` Cognitive Services User ` role to your user account. You can assign roles in the Azure portal under ** Access control (IAM)** > ** Add role assignment** .
26
28
27
- [ !INCLUDE [ environment-variables] ( environment-variables.md )]
29
+ ## Set up
30
+
31
+ 1 . Create a new folder ` dall-e-quickstart ` and go to the quickstart folder with the following command:
28
32
33
+ ``` shell
34
+ mkdir dall-e-quickstart && cd dall-e-quickstart
35
+ ```
29
36
30
- ## Create a new Go application
37
+ 1. For the ** recommended ** keyless authentication with Microsoft Entra ID, sign in to Azure with the following command:
31
38
32
- Open the command prompt and navigate to your project folder. Create a new file * sample.go* .
39
+ ` ` ` console
40
+ az login
41
+ ` ` `
33
42
34
- ## Install the Go SDK
43
+ # # Retrieve resource information
35
44
36
- Install the OpenAI Go SDK using the following command:
45
+ [ ! INCLUDE [resource authentication](resource-authentication.md)]
37
46
38
- ``` console
39
- go get github.com/Azure/azure-sdk-for-go/sdk/ai/azopenai@latest
40
- ```
41
-
42
- Or, if you use ` dep ` , within your repo run:
43
-
44
- ``` console
45
- dep ensure -add github.com/Azure/azure-sdk-for-go/sdk/ai/azopenai
46
- ```
47
-
48
- ## Generate images with DALL-E
47
+ # # Run the quickstart
49
48
50
- Open * sample.go * in your preferred code editor.
49
+ The sample code in this quickstart uses Microsoft Entra ID for the recommended keyless authentication. If you prefer to use an API key, you can replace the ` NewDefaultAzureCredential ` implementation with ` NewKeyCredential ` .
51
50
52
- Add the following code to your script:
51
+ # ### [Microsoft Entra ID](#tab/keyless)
53
52
54
53
` ` ` go
55
- package main
56
-
57
- import (
58
- " context"
59
- " fmt"
60
- " net/http"
61
- " os"
62
-
63
- " github.com/Azure/azure-sdk-for-go/sdk/ai/azopenai"
64
- " github.com/Azure/azure-sdk-for-go/sdk/azcore"
65
- " github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
66
- )
67
-
68
- func main () {
69
- azureOpenAIKey := os.Getenv (" AZURE_OPENAI_API_KEY" )
70
-
71
- // Ex: "https://<your-azure-openai-host>.openai.azure.com"
72
- azureOpenAIEndpoint := os.Getenv (" AZURE_OPENAI_ENDPOINT" )
73
-
74
- if azureOpenAIKey == " " || azureOpenAIEndpoint == " " {
75
- fmt.Fprintf (os.Stderr , " Skipping example, environment variables missing\n " )
76
- return
77
- }
78
-
79
- keyCredential := azcore.NewKeyCredential (azureOpenAIKey)
80
-
81
- client , err := azopenai.NewClientWithKeyCredential (azureOpenAIEndpoint, keyCredential, nil )
82
-
83
- if err != nil {
84
- // handle error
85
- }
86
-
87
- resp , err := client.GetImageGenerations (context.TODO (), azopenai.ImageGenerationOptions {
88
- Prompt: to.Ptr (" a painting of a cat in the style of Dali" ),
89
- ResponseFormat: to.Ptr (azopenai.ImageGenerationResponseFormatURL ),
90
- }, nil )
91
-
92
- if err != nil {
93
- // handle error
94
- }
95
-
96
- for _ , generatedImage := range resp.Data {
97
- // the underlying type for the generatedImage is dictated by the value of
98
- // ImageGenerationOptions.ResponseFormat. In this example we used `azopenai.ImageGenerationResponseFormatURL`,
99
- // so the underlying type will be ImageLocation.
100
-
101
- resp , err := http.Head (*generatedImage.URL )
54
+ azureOpenAIEndpoint := os.Getenv(" AZURE_OPENAI_ENDPOINT" )
55
+ credential, err := azidentity.NewDefaultAzureCredential(nil)
56
+ client, err := azopenai.NewClient(azureOpenAIEndpoint, credential, nil)
57
+ ` ` `
102
58
103
- if err != nil {
104
- // handle error
105
- }
59
+ # ### [API key](#tab/api-key)
106
60
107
- fmt.Fprintf (os.Stderr , " Image generated, HEAD request on URL returned %d \n Image URL: %s \n " , resp.StatusCode , *generatedImage.URL )
108
- }
109
- }
61
+ ` ` ` go
62
+ azureOpenAIEndpoint := os.Getenv(" AZURE_OPENAI_ENDPOINT" )
63
+ azureOpenAIKey := os.Getenv(" AZURE_OPENAI_API_KEY" )
64
+ credential := azcore.NewKeyCredential(azureOpenAIKey)
65
+ client, err := azopenai.NewClientWithKeyCredential(azureOpenAIEndpoint, credential, nil)
110
66
` ` `
67
+ ---
111
68
112
- Run the script using the ` go run ` command:
69
+ # ### [Microsoft Entra ID](#tab/keyless)
70
+
71
+ To run the sample:
72
+
73
+ 1. Create a new file named * quickstart.go* . Copy the following code into the * quickstart.go* file.
74
+
75
+ ` ` ` go
76
+ package main
77
+
78
+ import (
79
+ " context"
80
+ " fmt"
81
+ " net/http"
82
+ " os"
83
+ " log"
84
+
85
+ " github.com/Azure/azure-sdk-for-go/sdk/ai/azopenai"
86
+ " github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
87
+ " github.com/Azure/azure-sdk-for-go/sdk/azidentity"
88
+ )
89
+
90
+ func main () {
91
+ azureOpenAIEndpoint := os.Getenv(" AZURE_OPENAI_ENDPOINT" )
92
+ modelDeploymentID := " dall-e-3"
93
+
94
+ credential, err := azidentity.NewDefaultAzureCredential(nil)
95
+ if err ! = nil {
96
+ log.Printf(" ERROR: %s" , err)
97
+ return
98
+ }
99
+
100
+ client, err := azopenai.NewClient(
101
+ azureOpenAIEndpoint, credential, nil)
102
+ if err ! = nil {
103
+ log.Printf(" ERROR: %s" , err)
104
+ return
105
+ }
106
+
107
+ resp, err := client.GetImageGenerations(context.TODO (), azopenai.ImageGenerationOptions{
108
+ Prompt: to.Ptr(" A painting of a cat in the style of Dali." ),
109
+ ResponseFormat: to.Ptr(azopenai.ImageGenerationResponseFormatURL),
110
+ DeploymentName: to.Ptr(modelDeploymentID),
111
+ }, nil)
112
+
113
+ if err ! = nil {
114
+ // Implement application specific error handling logic.
115
+ log.Printf(" ERROR: %s" , err)
116
+ return
117
+ }
118
+
119
+ for _, generatedImage := range resp.Data {
120
+ // The underlying type for the generatedImage is determined by the value of
121
+ // ImageGenerationOptions.ResponseFormat.
122
+ // In this example we use ` azopenai.ImageGenerationResponseFormatURL` ,
123
+ // so the underlying type will be ImageLocation.
124
+
125
+ resp, err := http.Head(* generatedImage.URL)
126
+
127
+ if err ! = nil {
128
+ // Implement application specific error handling logic.
129
+ log.Printf(" ERROR: %s" , err)
130
+ return
131
+ }
132
+
133
+ fmt.Fprintf(os.Stderr, " Image generated, HEAD request on URL returned %d\nImage URL: %s\n" , resp.StatusCode, * generatedImage.URL)
134
+ }
135
+ }
136
+ ` ` `
137
+
138
+ 1. Run the following command to create a new Go module:
139
+
140
+ ` ` ` shell
141
+ go mod init quickstart.go
142
+ ` ` `
143
+
144
+ 1. Run ` go mod tidy` to install the required dependencies:
145
+
146
+ ` ` ` cmd
147
+ go mod tidy
148
+ ` ` `
149
+
150
+ 1. Run the following command to run the sample:
151
+
152
+ ` ` ` shell
153
+ go run quickstart.go
154
+ ` ` `
155
+
156
+
157
+ # ### [API key](#tab/api-key)
158
+
159
+ To run the sample:
160
+
161
+ 1. Create a new file named * quickstart.go* . Copy the following code into the * quickstart.go* file.
162
+
163
+ ` ` ` go
164
+ package main
165
+
166
+ import (
167
+ " context"
168
+ " fmt"
169
+ " net/http"
170
+ " os"
171
+ " log"
172
+
173
+ " github.com/Azure/azure-sdk-for-go/sdk/ai/azopenai"
174
+ " github.com/Azure/azure-sdk-for-go/sdk/azcore"
175
+ " github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
176
+ )
177
+
178
+ func main () {
179
+ azureOpenAIEndpoint := os.Getenv(" AZURE_OPENAI_ENDPOINT" )
180
+ modelDeploymentID := " dall-e-3"
181
+
182
+ azureOpenAIKey := os.Getenv(" AZURE_OPENAI_API_KEY" )
183
+ credential := azcore.NewKeyCredential(azureOpenAIKey)
184
+
185
+ client, err := azopenai.NewClientWithKeyCredential(
186
+ azureOpenAIEndpoint, credential, nil)
187
+ if err ! = nil {
188
+ log.Printf(" ERROR: %s" , err)
189
+ return
190
+ }
191
+
192
+ resp, err := client.GetImageGenerations(context.TODO (), azopenai.ImageGenerationOptions{
193
+ Prompt: to.Ptr(" A painting of a cat in the style of Dali." ),
194
+ ResponseFormat: to.Ptr(azopenai.ImageGenerationResponseFormatURL),
195
+ DeploymentName: to.Ptr(modelDeploymentID),
196
+ }, nil)
197
+
198
+ if err ! = nil {
199
+ // Implement application specific error handling logic.
200
+ log.Printf(" ERROR: %s" , err)
201
+ return
202
+ }
203
+
204
+ for _, generatedImage := range resp.Data {
205
+ // The underlying type for the generatedImage is determined by the value of
206
+ // ImageGenerationOptions.ResponseFormat.
207
+ // In this example we use ` azopenai.ImageGenerationResponseFormatURL` ,
208
+ // so the underlying type will be ImageLocation.
209
+
210
+ resp, err := http.Head(* generatedImage.URL)
211
+
212
+ if err ! = nil {
213
+ // Implement application specific error handling logic.
214
+ log.Printf(" ERROR: %s" , err)
215
+ return
216
+ }
217
+
218
+ fmt.Fprintf(os.Stderr, " Image generated, HEAD request on URL returned %d\nImage URL: %s\n" , resp.StatusCode, * generatedImage.URL)
219
+ }
220
+ }
221
+ ` ` `
222
+
223
+ 1. Run the following command to create a new Go module:
224
+
225
+ ` ` ` shell
226
+ go mod init quickstart.go
227
+ ` ` `
228
+
229
+ 1. Run ` go mod tidy` to install the required dependencies:
230
+
231
+ ` ` ` cmd
232
+ go mod tidy
233
+ ` ` `
234
+
235
+ 1. Run the following command to run the sample:
236
+
237
+ ` ` ` shell
238
+ go run quickstart.go
239
+ ` ` `
113
240
114
- ``` console
115
- go run sample.go
116
- ```
241
+ ---
117
242
118
243
# # Output
119
244
0 commit comments