@@ -170,6 +170,86 @@ func main() {
170170```
171171</details >
172172
173+ <details >
174+ <summary >DALL-E 2 image generation</summary >
175+
176+ ``` go
177+ package main
178+
179+ import (
180+ " bytes"
181+ " context"
182+ " encoding/base64"
183+ " fmt"
184+ openai " github.com/sashabaranov/go-openai"
185+ " image/png"
186+ " os"
187+ )
188+
189+ func main () {
190+ c := openai.NewClient (" your token" )
191+ ctx := context.Background ()
192+
193+ // Sample image by link
194+ reqUrl := openai.ImageRequest {
195+ Prompt: " Parrot on a skateboard performs a trick, cartoon style, natural light, high detail" ,
196+ Size: openai.CreateImageSize256x256 ,
197+ ResponseFormat: openai.CreateImageResponseFormatURL ,
198+ N: 1 ,
199+ }
200+
201+ respUrl , err := c.CreateImage (ctx, reqUrl)
202+ if err != nil {
203+ fmt.Printf (" Image creation error: %v \n " , err)
204+ return
205+ }
206+ fmt.Println (respUrl.Data [0 ].URL )
207+
208+ // Example image as base64
209+ reqBase64 := openai.ImageRequest {
210+ Prompt: " Portrait of a humanoid parrot in a classic costume, high detail, realistic light, unreal engine" ,
211+ Size: openai.CreateImageSize256x256 ,
212+ ResponseFormat: openai.CreateImageResponseFormatB64JSON ,
213+ N: 1 ,
214+ }
215+
216+ respBase64 , err := c.CreateImage (ctx, reqBase64)
217+ if err != nil {
218+ fmt.Printf (" Image creation error: %v \n " , err)
219+ return
220+ }
221+
222+ imgBytes , err := base64.StdEncoding .DecodeString (respBase64.Data [0 ].B64JSON )
223+ if err != nil {
224+ fmt.Printf (" Base64 decode error: %v \n " , err)
225+ return
226+ }
227+
228+ r := bytes.NewReader (imgBytes)
229+ imgData , err := png.Decode (r)
230+ if err != nil {
231+ fmt.Printf (" PNG decode error: %v \n " , err)
232+ return
233+ }
234+
235+ file , err := os.Create (" image.png" )
236+ if err != nil {
237+ fmt.Printf (" File creation error: %v \n " , err)
238+ return
239+ }
240+ defer file.Close ()
241+
242+ if err := png.Encode (file, imgData); err != nil {
243+ fmt.Printf (" PNG encode error: %v \n " , err)
244+ return
245+ }
246+
247+ fmt.Println (" The image was saved as example.png" )
248+ }
249+
250+ ```
251+ </details >
252+
173253<details >
174254<summary >Configuring proxy</summary >
175255
0 commit comments