|
| 1 | +package org.devlive.sdk.openai.entity; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; |
| 4 | +import com.fasterxml.jackson.annotation.JsonProperty; |
| 5 | +import lombok.AllArgsConstructor; |
| 6 | +import lombok.Builder; |
| 7 | +import lombok.Data; |
| 8 | +import lombok.NoArgsConstructor; |
| 9 | +import lombok.ToString; |
| 10 | +import org.apache.commons.lang3.EnumUtils; |
| 11 | +import org.apache.commons.lang3.ObjectUtils; |
| 12 | +import org.apache.commons.lang3.StringUtils; |
| 13 | +import org.devlive.sdk.openai.exception.ParamException; |
| 14 | +import org.devlive.sdk.openai.model.ImageFormatModel; |
| 15 | +import org.devlive.sdk.openai.model.ImageSizeModel; |
| 16 | + |
| 17 | +@Data |
| 18 | +@Builder |
| 19 | +@ToString |
| 20 | +@NoArgsConstructor |
| 21 | +@AllArgsConstructor |
| 22 | +@JsonIgnoreProperties(ignoreUnknown = true) |
| 23 | +public class ImageEntity |
| 24 | +{ |
| 25 | + /** |
| 26 | + * A text description of the desired image(s). The maximum length is 1000 characters. |
| 27 | + */ |
| 28 | + @JsonProperty(value = "prompt") |
| 29 | + private String prompt; |
| 30 | + |
| 31 | + /** |
| 32 | + * The number of images to generate. Must be between 1 and 10. |
| 33 | + */ |
| 34 | + @JsonProperty(value = "n") |
| 35 | + private Integer count; |
| 36 | + |
| 37 | + /** |
| 38 | + * The size of the generated images. |
| 39 | + * |
| 40 | + * @see ImageSizeModel |
| 41 | + */ |
| 42 | + @JsonProperty(value = "size") |
| 43 | + private String size; |
| 44 | + |
| 45 | + /** |
| 46 | + * The format in which the generated images are returned. |
| 47 | + * |
| 48 | + * @see org.devlive.sdk.openai.model.ImageFormatModel |
| 49 | + */ |
| 50 | + @JsonProperty(value = "response_format") |
| 51 | + private String format = "url"; |
| 52 | + |
| 53 | + /** |
| 54 | + * A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse. |
| 55 | + */ |
| 56 | + @JsonProperty(value = "user") |
| 57 | + private String user; |
| 58 | + |
| 59 | + @JsonProperty(value = "url") |
| 60 | + private String url; |
| 61 | + |
| 62 | + private ImageEntity(ImageEntityBuilder builder) |
| 63 | + { |
| 64 | + if (ObjectUtils.isEmpty(builder.prompt)) { |
| 65 | + builder.prompt(null); |
| 66 | + } |
| 67 | + this.prompt = builder.prompt; |
| 68 | + |
| 69 | + if (ObjectUtils.isEmpty(builder.count)) { |
| 70 | + builder.count(1); |
| 71 | + } |
| 72 | + this.count = builder.count; |
| 73 | + |
| 74 | + if (ObjectUtils.isEmpty(builder.size)) { |
| 75 | + builder.size(ImageSizeModel.X_1024); |
| 76 | + } |
| 77 | + this.size = builder.size; |
| 78 | + |
| 79 | + if (ObjectUtils.isEmpty(builder.format)) { |
| 80 | + builder.format(ImageFormatModel.url); |
| 81 | + } |
| 82 | + this.format = builder.format; |
| 83 | + |
| 84 | + this.user = builder.user; |
| 85 | + } |
| 86 | + |
| 87 | + public static class ImageEntityBuilder |
| 88 | + { |
| 89 | + public ImageEntityBuilder prompt(String prompt) |
| 90 | + { |
| 91 | + if (StringUtils.isEmpty(prompt)) { |
| 92 | + throw new ParamException("Invalid prompt must not be empty"); |
| 93 | + } |
| 94 | + this.prompt = prompt; |
| 95 | + return this; |
| 96 | + } |
| 97 | + |
| 98 | + public ImageEntityBuilder count(Integer count) |
| 99 | + { |
| 100 | + if (count < 1 || count > 10) { |
| 101 | + throw new ParamException(String.format("Invalid count: %s , between 1 and 10", count)); |
| 102 | + } |
| 103 | + this.count = count; |
| 104 | + return this; |
| 105 | + } |
| 106 | + |
| 107 | + public ImageEntityBuilder size(ImageSizeModel size) |
| 108 | + { |
| 109 | + Object instance = EnumUtils.getEnum(ImageSizeModel.class, size.name()); |
| 110 | + if (ObjectUtils.isEmpty(instance)) { |
| 111 | + throw new ParamException(String.format("Invalid size: %s , Must be one of %s", size, ImageSizeModel.values())); |
| 112 | + } |
| 113 | + this.size = size.getName(); |
| 114 | + return this; |
| 115 | + } |
| 116 | + |
| 117 | + public ImageEntityBuilder format(ImageFormatModel format) |
| 118 | + { |
| 119 | + Object instance = EnumUtils.getEnum(ImageFormatModel.class, format.name()); |
| 120 | + if (ObjectUtils.isEmpty(instance)) { |
| 121 | + throw new ParamException(String.format("Invalid format: %s , Must be one of %s", format, ImageFormatModel.values())); |
| 122 | + } |
| 123 | + this.format = format.name(); |
| 124 | + return this; |
| 125 | + } |
| 126 | + |
| 127 | + public ImageEntity build() |
| 128 | + { |
| 129 | + return new ImageEntity(this); |
| 130 | + } |
| 131 | + } |
| 132 | +} |
0 commit comments