|
| 1 | +package org.devlive.sdk.openai.entity; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; |
| 4 | +import com.fasterxml.jackson.annotation.JsonProperty; |
| 5 | +import com.google.common.collect.Maps; |
| 6 | +import lombok.AllArgsConstructor; |
| 7 | +import lombok.Builder; |
| 8 | +import lombok.Data; |
| 9 | +import lombok.NoArgsConstructor; |
| 10 | +import lombok.ToString; |
| 11 | +import okhttp3.RequestBody; |
| 12 | +import org.apache.commons.lang3.EnumUtils; |
| 13 | +import org.apache.commons.lang3.ObjectUtils; |
| 14 | +import org.apache.commons.lang3.StringUtils; |
| 15 | +import org.devlive.sdk.openai.exception.ParamException; |
| 16 | +import org.devlive.sdk.openai.model.ImageFormatModel; |
| 17 | +import org.devlive.sdk.openai.model.ImageSizeModel; |
| 18 | +import org.devlive.sdk.openai.utils.MultipartBodyUtils; |
| 19 | + |
| 20 | +import java.io.File; |
| 21 | +import java.util.Arrays; |
| 22 | +import java.util.Map; |
| 23 | + |
| 24 | +@Data |
| 25 | +@Builder |
| 26 | +@ToString |
| 27 | +@NoArgsConstructor |
| 28 | +@AllArgsConstructor |
| 29 | +@JsonIgnoreProperties(ignoreUnknown = true) |
| 30 | +public class ImageEntity |
| 31 | +{ |
| 32 | + /** |
| 33 | + * A text description of the desired image(s). The maximum length is 1000 characters. |
| 34 | + */ |
| 35 | + @JsonProperty(value = "prompt") |
| 36 | + private String prompt; |
| 37 | + |
| 38 | + /** |
| 39 | + * The number of images to generate. Must be between 1 and 10. |
| 40 | + */ |
| 41 | + @JsonProperty(value = "n") |
| 42 | + private Integer count; |
| 43 | + |
| 44 | + /** |
| 45 | + * The size of the generated images. |
| 46 | + * |
| 47 | + * @see ImageSizeModel |
| 48 | + */ |
| 49 | + @JsonProperty(value = "size") |
| 50 | + private String size; |
| 51 | + |
| 52 | + /** |
| 53 | + * The format in which the generated images are returned. |
| 54 | + * |
| 55 | + * @see org.devlive.sdk.openai.model.ImageFormatModel |
| 56 | + */ |
| 57 | + @JsonProperty(value = "response_format") |
| 58 | + private String format = "url"; |
| 59 | + |
| 60 | + /** |
| 61 | + * A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse. |
| 62 | + */ |
| 63 | + @JsonProperty(value = "user") |
| 64 | + private String user; |
| 65 | + |
| 66 | + @JsonProperty(value = "url") |
| 67 | + private String url; |
| 68 | + |
| 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 | + |
| 94 | + private ImageEntity(ImageEntityBuilder builder) |
| 95 | + { |
| 96 | + if (ObjectUtils.isEmpty(builder.prompt)) { |
| 97 | + builder.prompt(null); |
| 98 | + } |
| 99 | + this.prompt = builder.prompt; |
| 100 | + |
| 101 | + if (ObjectUtils.isEmpty(builder.count)) { |
| 102 | + builder.count(1); |
| 103 | + } |
| 104 | + this.count = builder.count; |
| 105 | + |
| 106 | + if (ObjectUtils.isEmpty(builder.size)) { |
| 107 | + builder.size(ImageSizeModel.X_1024); |
| 108 | + } |
| 109 | + this.size = builder.size; |
| 110 | + |
| 111 | + if (ObjectUtils.isEmpty(builder.format)) { |
| 112 | + builder.format(ImageFormatModel.url); |
| 113 | + } |
| 114 | + this.format = builder.format; |
| 115 | + |
| 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 | + |
| 136 | + this.user = builder.user; |
| 137 | + } |
| 138 | + |
| 139 | + public static class ImageEntityBuilder |
| 140 | + { |
| 141 | + public ImageEntityBuilder prompt(String prompt) |
| 142 | + { |
| 143 | + if ((ObjectUtils.isEmpty(this.isVariation) || !this.isVariation) && StringUtils.isEmpty(prompt)) { |
| 144 | + throw new ParamException("Invalid prompt must not be empty"); |
| 145 | + } |
| 146 | + this.prompt = prompt; |
| 147 | + return this; |
| 148 | + } |
| 149 | + |
| 150 | + public ImageEntityBuilder count(Integer count) |
| 151 | + { |
| 152 | + if (count < 1 || count > 10) { |
| 153 | + throw new ParamException(String.format("Invalid count: %s , between 1 and 10", count)); |
| 154 | + } |
| 155 | + this.count = count; |
| 156 | + return this; |
| 157 | + } |
| 158 | + |
| 159 | + public ImageEntityBuilder size(ImageSizeModel size) |
| 160 | + { |
| 161 | + Object instance = EnumUtils.getEnum(ImageSizeModel.class, size.name()); |
| 162 | + if (ObjectUtils.isEmpty(instance)) { |
| 163 | + throw new ParamException(String.format("Invalid size: %s , Must be one of %s", size, Arrays.toString(ImageSizeModel.values()))); |
| 164 | + } |
| 165 | + this.size = size.getName(); |
| 166 | + return this; |
| 167 | + } |
| 168 | + |
| 169 | + public ImageEntityBuilder format(ImageFormatModel format) |
| 170 | + { |
| 171 | + Object instance = EnumUtils.getEnum(ImageFormatModel.class, format.name()); |
| 172 | + if (ObjectUtils.isEmpty(instance)) { |
| 173 | + throw new ParamException(String.format("Invalid format: %s , Must be one of %s", format, Arrays.toString(ImageFormatModel.values()))); |
| 174 | + } |
| 175 | + this.format = format.name(); |
| 176 | + return this; |
| 177 | + } |
| 178 | + |
| 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 | + |
| 219 | + public ImageEntity build() |
| 220 | + { |
| 221 | + return new ImageEntity(this); |
| 222 | + } |
| 223 | + } |
| 224 | +} |
0 commit comments