@@ -17,9 +17,11 @@ type DallEImageOutput any
1717type DallEImageSize string
1818
1919const (
20- DallEImageSize256 DallEImageSize = openai .CreateImageSize256x256
21- DallEImageSize512 DallEImageSize = openai .CreateImageSize512x512
22- DallEImageSize1024 DallEImageSize = openai .CreateImageSize1024x1024
20+ DallEImageSize256x256 DallEImageSize = openai .CreateImageSize256x256
21+ DallEImageSize512x512 DallEImageSize = openai .CreateImageSize512x512
22+ DallEImageSize1024x1024 DallEImageSize = openai .CreateImageSize1024x1024
23+ DallEImageSize1792x104 DallEImageSize = openai .CreateImageSize1792x1024
24+ DallEImageSize1024x1792 DallEImageSize = openai .CreateImageSize1024x1792
2325)
2426
2527type DallEImageFormat string
@@ -30,19 +32,45 @@ const (
3032 DallEImageFormatImage DallEImageFormat = "image"
3133)
3234
35+ type DallEModel string
36+
37+ const (
38+ DallEModel2 DallEModel = openai .CreateImageModelDallE2
39+ DallEModel3 DallEModel = openai .CreateImageModelDallE3
40+ )
41+
42+ type DallEImageQuality string
43+
44+ const (
45+ DallEImageQualityHD DallEImageQuality = openai .CreateImageQualityHD
46+ DallEImageQualityStandard DallEImageQuality = openai .CreateImageQualityStandard
47+ )
48+
49+ type DallEImageStyle string
50+
51+ const (
52+ DallEImageStyleVivid DallEImageStyle = openai .CreateImageStyleVivid
53+ DallEImageStyleNatural DallEImageStyle = openai .CreateImageStyleNatural
54+ )
55+
3356type DallE struct {
3457 openAIClient * openai.Client
58+ model DallEModel
3559 imageSize DallEImageSize
3660 imageFormat DallEImageFormat
37- imageFile string
61+ imageStyle DallEImageStyle
62+ imageQuality DallEImageQuality
3863}
3964
4065func NewDallE () * DallE {
4166 openAIKey := os .Getenv ("OPENAI_API_KEY" )
4267 return & DallE {
4368 openAIClient : openai .NewClient (openAIKey ),
44- imageSize : DallEImageSize256 ,
69+ model : DallEModel2 ,
70+ imageSize : DallEImageSize256x256 ,
4571 imageFormat : DallEImageFormatURL ,
72+ imageStyle : DallEImageStyleNatural ,
73+ imageQuality : DallEImageQualityStandard ,
4674 }
4775}
4876
@@ -56,73 +84,83 @@ func (d *DallE) WithImageSize(imageSize DallEImageSize) *DallE {
5684 return d
5785}
5886
59- func (d * DallE ) AsURL ( ) * DallE {
60- d .imageFormat = DallEImageFormatURL
87+ func (d * DallE ) WithImageStyle ( imageStyle DallEImageStyle ) * DallE {
88+ d .imageStyle = imageStyle
6189 return d
6290}
6391
64- func (d * DallE ) AsFile (path string ) * DallE {
65- d .imageFormat = DallEImageFormatFile
66- d .imageFile = path
92+ func (d * DallE ) WithImageQuality (imageQuality DallEImageQuality ) * DallE {
93+ d .imageQuality = imageQuality
6794 return d
6895}
6996
70- func (d * DallE ) AsImage () * DallE {
71- d .imageFormat = DallEImageFormatImage
97+ func (d * DallE ) WithModel (model DallEModel ) * DallE {
98+ d .model = model
99+ return d
100+ }
101+
102+ func (d * DallE ) WithImageFormat (imageFormat DallEImageFormat ) * DallE {
103+ d .imageFormat = imageFormat
72104 return d
73105}
74106
75107func (d * DallE ) Transform (ctx context.Context , input string ) (any , error ) {
76108 switch d .imageFormat {
77109 case DallEImageFormatURL :
78- return d .transformToURL (ctx , input )
110+ return d .TransformAsURL (ctx , input )
79111 case DallEImageFormatFile :
80- return d .transformToFile (ctx , input )
112+ return d .TransformAsFile (ctx , input , nil )
81113 case DallEImageFormatImage :
82- return d .transformToImage (ctx , input )
114+ return d .TransformToImage (ctx , input )
83115 default :
84116 return "" , fmt .Errorf ("unknown image format: %s" , d .imageFormat )
85117 }
86118}
87119
88- func (d * DallE ) transformToURL (ctx context.Context , input string ) (any , error ) {
120+ func (d * DallE ) TransformAsURL (ctx context.Context , input string ) (string , error ) {
89121 reqURL := openai.ImageRequest {
90122 Prompt : input ,
123+ Model : string (d .model ),
91124 Size : string (d .imageSize ),
125+ Quality : string (d .imageQuality ),
126+ Style : string (d .imageStyle ),
92127 ResponseFormat : openai .CreateImageResponseFormatURL ,
93128 N : 1 ,
94129 }
95130
96131 respURL , err := d .openAIClient .CreateImage (ctx , reqURL )
97132 if err != nil {
98- return nil , err
133+ return "" , err
99134 }
100135
101136 return respURL .Data [0 ].URL , nil
102137}
103138
104- func (d * DallE ) transformToFile (ctx context.Context , input string ) (any , error ) {
105- imgData , err := d .transformToImage (ctx , input )
139+ func (d * DallE ) TransformAsFile (ctx context.Context , input string , file * os. File ) (string , error ) {
140+ imgData , err := d .TransformToImage (ctx , input )
106141 if err != nil {
107- return nil , err
142+ return "" , err
108143 }
109144
110- file , err := os .Create (d .imageFile )
111- if err != nil {
112- return nil , err
145+ if file == nil {
146+ // create a temporary file
147+ file , err = os .CreateTemp ("" , "dall-e-*.png" )
148+ if err != nil {
149+ return "" , err
150+ }
113151 }
152+
114153 defer file .Close ()
115154
116- err = png .Encode (file , imgData .(image. Image ) )
155+ err = png .Encode (file , imgData )
117156 if err != nil {
118- return nil , err
157+ return "" , err
119158 }
120159
121- var output interface {}
122- return output , nil
160+ return file .Name (), nil
123161}
124162
125- func (d * DallE ) transformToImage (ctx context.Context , input string ) (any , error ) {
163+ func (d * DallE ) TransformToImage (ctx context.Context , input string ) (image. Image , error ) {
126164 reqBase64 := openai.ImageRequest {
127165 Prompt : input ,
128166 Size : string (d .imageSize ),
0 commit comments