|
| 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.StringUtils; |
| 11 | +import org.devlive.sdk.openai.exception.ParamException; |
| 12 | + |
| 13 | +import java.util.List; |
| 14 | + |
| 15 | +@Data |
| 16 | +@Builder |
| 17 | +@ToString |
| 18 | +@NoArgsConstructor |
| 19 | +@AllArgsConstructor |
| 20 | +@JsonIgnoreProperties(ignoreUnknown = true) |
| 21 | +public class EmbeddingEntity |
| 22 | +{ |
| 23 | + /** |
| 24 | + * ID of the model to use. You can use the List models API to see all of your available models, or see our Model overview for descriptions of them. |
| 25 | + */ |
| 26 | + @JsonProperty(value = "model") |
| 27 | + private String model; |
| 28 | + |
| 29 | + /** |
| 30 | + * Input text to embed, encoded as a string or array of tokens. To embed multiple inputs in a single request, pass an array of strings or array of token arrays. Each input must not exceed the max input tokens for the model (8191 tokens for text-embedding-ada-002). |
| 31 | + */ |
| 32 | + @JsonProperty(value = "input") |
| 33 | + private String input; |
| 34 | + |
| 35 | + /** |
| 36 | + * A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse. |
| 37 | + */ |
| 38 | + @JsonProperty(value = "user") |
| 39 | + private String user; |
| 40 | + |
| 41 | + /* ====================== Response ====================== */ |
| 42 | + @JsonProperty(value = "object") |
| 43 | + private String object; |
| 44 | + |
| 45 | + @JsonProperty(value = "embedding") |
| 46 | + private List<Object> embeddings; |
| 47 | + |
| 48 | + @JsonProperty(value = "index") |
| 49 | + private long index; |
| 50 | + |
| 51 | + private EmbeddingEntity(EmbeddingEntityBuilder builder) |
| 52 | + { |
| 53 | + if (StringUtils.isEmpty(builder.model)) { |
| 54 | + builder.model(null); |
| 55 | + } |
| 56 | + this.model = builder.model; |
| 57 | + |
| 58 | + if (StringUtils.isEmpty(builder.input)) { |
| 59 | + builder.input(null); |
| 60 | + } |
| 61 | + this.input = builder.input; |
| 62 | + |
| 63 | + this.user = builder.user; |
| 64 | + } |
| 65 | + |
| 66 | + public static class EmbeddingEntityBuilder |
| 67 | + { |
| 68 | + public EmbeddingEntityBuilder model(String model) |
| 69 | + { |
| 70 | + if (!model.equals("text-embedding-ada-002") |
| 71 | + && !(model.startsWith("text-similarity-") && model.endsWith("-001")) |
| 72 | + && !(model.startsWith("text-search-") && model.endsWith("-001")) |
| 73 | + && !(model.startsWith("code-search-") && model.endsWith("-001"))) { |
| 74 | + throw new ParamException(String.format("Invalid model %s must be specified, Support text-embedding-ada-002, text-similarity-*-001, text-search-*-*-001, code-search-*-*-001", model)); |
| 75 | + } |
| 76 | + this.model = model; |
| 77 | + return this; |
| 78 | + } |
| 79 | + |
| 80 | + public EmbeddingEntityBuilder input(String input) |
| 81 | + { |
| 82 | + if (StringUtils.isEmpty(input)) { |
| 83 | + throw new ParamException("Invalid input must be not empty"); |
| 84 | + } |
| 85 | + this.input = input; |
| 86 | + return this; |
| 87 | + } |
| 88 | + |
| 89 | + public EmbeddingEntity build() |
| 90 | + { |
| 91 | + return new EmbeddingEntity(this); |
| 92 | + } |
| 93 | + } |
| 94 | +} |
0 commit comments