22
33import com .fasterxml .jackson .annotation .JsonIgnoreProperties ;
44import com .fasterxml .jackson .annotation .JsonProperty ;
5+ import com .google .common .collect .Maps ;
56import lombok .AllArgsConstructor ;
67import lombok .Builder ;
78import lombok .Data ;
89import lombok .NoArgsConstructor ;
910import lombok .ToString ;
11+ import okhttp3 .RequestBody ;
1012import org .apache .commons .lang3 .EnumUtils ;
1113import org .apache .commons .lang3 .ObjectUtils ;
1214import org .apache .commons .lang3 .StringUtils ;
1315import org .devlive .sdk .openai .exception .ParamException ;
1416import org .devlive .sdk .openai .model .ImageFormatModel ;
1517import org .devlive .sdk .openai .model .ImageSizeModel ;
18+ import org .devlive .sdk .openai .utils .MultipartBodyUtils ;
1619
20+ import java .io .File ;
1721import java .util .Arrays ;
22+ import java .util .Map ;
1823
1924@ Data
2025@ Builder
@@ -61,6 +66,31 @@ public class ImageEntity
6166 @ JsonProperty (value = "url" )
6267 private String url ;
6368
69+ @ JsonProperty (value = "image" )
70+ private File image ;
71+
72+ @ JsonProperty (value = "mask" )
73+ private File mask ;
74+
75+ private Boolean isEdit ;
76+ private Boolean isVariation ;
77+
78+ public Map <String , RequestBody > convertMap ()
79+ {
80+ Map <String , RequestBody > map = Maps .newConcurrentMap ();
81+ if (this .isEdit ) {
82+ map .put ("prompt" , RequestBody .create (MultipartBodyUtils .TYPE , this .getPrompt ()));
83+ }
84+ map .put ("n" , RequestBody .create (MultipartBodyUtils .TYPE , this .getCount ().toString ()));
85+ map .put ("size" , RequestBody .create (MultipartBodyUtils .TYPE , this .getSize ()));
86+ map .put ("response_format" , RequestBody .create (MultipartBodyUtils .TYPE , this .getFormat ()));
87+
88+ if (StringUtils .isNotEmpty (this .getUser ())) {
89+ map .put ("user" , RequestBody .create (MultipartBodyUtils .TYPE , this .getUser ()));
90+ }
91+ return map ;
92+ }
93+
6494 private ImageEntity (ImageEntityBuilder builder )
6595 {
6696 if (ObjectUtils .isEmpty (builder .prompt )) {
@@ -83,14 +113,34 @@ private ImageEntity(ImageEntityBuilder builder)
83113 }
84114 this .format = builder .format ;
85115
116+ if (ObjectUtils .isEmpty (builder .image )) {
117+ builder .image (null );
118+ }
119+ this .image = builder .image ;
120+
121+ if (ObjectUtils .isEmpty (builder .mask )) {
122+ builder .mask (null );
123+ }
124+ this .mask = builder .mask ;
125+
126+ if (ObjectUtils .isEmpty (builder .isEdit )) {
127+ builder .isEdit (Boolean .FALSE );
128+ }
129+ this .isEdit = builder .isEdit ;
130+
131+ if (ObjectUtils .isEmpty (builder .isVariation )) {
132+ builder .isVariation (Boolean .FALSE );
133+ }
134+ this .isVariation = builder .isVariation ;
135+
86136 this .user = builder .user ;
87137 }
88138
89139 public static class ImageEntityBuilder
90140 {
91141 public ImageEntityBuilder prompt (String prompt )
92142 {
93- if (StringUtils .isEmpty (prompt )) {
143+ if (( ObjectUtils . isEmpty ( this . isVariation ) || ! this . isVariation ) && StringUtils .isEmpty (prompt )) {
94144 throw new ParamException ("Invalid prompt must not be empty" );
95145 }
96146 this .prompt = prompt ;
@@ -126,6 +176,46 @@ public ImageEntityBuilder format(ImageFormatModel format)
126176 return this ;
127177 }
128178
179+ public ImageEntityBuilder image (File image )
180+ {
181+ if (ObjectUtils .isNotEmpty (image ) && image .length () > 4 * 1024 * 102 ) {
182+ throw new ParamException ("Must be less than 4MB" );
183+ }
184+ this .image = image ;
185+ return this ;
186+ }
187+
188+ public ImageEntityBuilder mask (File mask )
189+ {
190+ if (ObjectUtils .isNotEmpty (mask ) && mask .length () > 4 * 1024 * 102 ) {
191+ throw new ParamException ("Must be less than 4MB" );
192+ }
193+ this .mask = mask ;
194+ return this ;
195+ }
196+
197+ public ImageEntityBuilder isEdit (Boolean isEdit )
198+ {
199+ if (isEdit && ObjectUtils .isEmpty (this .image )) {
200+ throw new ParamException ("Image must not be empty." );
201+ }
202+ this .isEdit = isEdit ;
203+ return this ;
204+ }
205+
206+ public ImageEntityBuilder isVariation (Boolean isVariation )
207+ {
208+ if (isVariation && ObjectUtils .isNotEmpty (this .prompt )) {
209+ throw new ParamException ("Please remove prompt" );
210+ }
211+
212+ if (isVariation && ObjectUtils .isEmpty (this .image )) {
213+ throw new ParamException ("Image must not be empty." );
214+ }
215+ this .isVariation = isVariation ;
216+ return this ;
217+ }
218+
129219 public ImageEntity build ()
130220 {
131221 return new ImageEntity (this );
0 commit comments