|
| 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.ObjectUtils; |
| 11 | +import org.apache.commons.lang3.StringUtils; |
| 12 | +import org.devlive.sdk.openai.exception.ParamException; |
| 13 | +import org.devlive.sdk.openai.model.EditModel; |
| 14 | + |
| 15 | +import java.util.Arrays; |
| 16 | + |
| 17 | +@Data |
| 18 | +@Builder |
| 19 | +@ToString |
| 20 | +@NoArgsConstructor |
| 21 | +@AllArgsConstructor |
| 22 | +@JsonIgnoreProperties(ignoreUnknown = true) |
| 23 | +@Deprecated |
| 24 | +public class EditEntity |
| 25 | +{ |
| 26 | + /** |
| 27 | + * ID of the model to use. You can use the text-davinci-edit-001 or code-davinci-edit-001 model with this endpoint. |
| 28 | + * 要使用的模型的 ID。您可以对此端点使用 text-davinci-edit-001 或 code-davinci-edit-001 模型。 |
| 29 | + * |
| 30 | + * @see org.devlive.sdk.openai.model.EditModel |
| 31 | + */ |
| 32 | + @JsonProperty(value = "model") |
| 33 | + private String model; |
| 34 | + |
| 35 | + /** |
| 36 | + * The input text to use as a starting point for the edit. |
| 37 | + * 用作编辑起点的输入文本。 |
| 38 | + */ |
| 39 | + @JsonProperty(value = "input") |
| 40 | + private String input; |
| 41 | + |
| 42 | + /** |
| 43 | + * The instruction that tells the model how to edit the prompt. |
| 44 | + * 告诉模型如何编辑提示的指令。 |
| 45 | + */ |
| 46 | + @JsonProperty(value = "instruction") |
| 47 | + private String instruction; |
| 48 | + |
| 49 | + /** |
| 50 | + * How many edits to generate for the input and instruction. |
| 51 | + * 为输入和指令生成多少编辑。 |
| 52 | + */ |
| 53 | + @JsonProperty(value = "n") |
| 54 | + private Integer count; |
| 55 | + |
| 56 | + /** |
| 57 | + * What sampling temperature to use, between 0 and 2. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic. |
| 58 | + * 使用什么采样温度,介于 0 和 2 之间。较高的值(如 0.8)将使输出更加随机,而较低的值(如 0.2)将使其更加集中和确定性。 |
| 59 | + * <p> |
| 60 | + * We generally recommend altering this or top_p but not both. |
| 61 | + * 我们通常建议更改此值或 top_p,但不要同时更改两者。 |
| 62 | + */ |
| 63 | + @JsonProperty(value = "temperature") |
| 64 | + private Double temperature; |
| 65 | + |
| 66 | + /** |
| 67 | + * An alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with top_p probability mass. So 0.1 means only the tokens comprising the top 10% probability mass are considered. |
| 68 | + * 温度采样的替代方法称为核采样,其中模型考虑具有 top_p 概率质量的标记的结果。因此 0.1 意味着仅考虑包含前 10% 概率质量的标记。 |
| 69 | + * <p> |
| 70 | + * We generally recommend altering this or temperature but not both. |
| 71 | + * 我们通常建议更改此值或温度,但不能同时更改两者。 |
| 72 | + */ |
| 73 | + @JsonProperty(value = "top_p") |
| 74 | + private Double topP; |
| 75 | + |
| 76 | + private EditEntity(EditEntityBuilder builder) |
| 77 | + { |
| 78 | + if (ObjectUtils.isEmpty(builder.model)) { |
| 79 | + throw new ParamException(String.format("Invalid model, Must be one of %s", Arrays.toString(EditModel.values()))); |
| 80 | + } |
| 81 | + this.model = builder.model; |
| 82 | + |
| 83 | + if (StringUtils.isEmpty(builder.input)) { |
| 84 | + builder.input(null); |
| 85 | + } |
| 86 | + this.input = builder.input; |
| 87 | + |
| 88 | + if (StringUtils.isEmpty(builder.instruction)) { |
| 89 | + builder.instruction(null); |
| 90 | + } |
| 91 | + this.instruction = builder.instruction; |
| 92 | + |
| 93 | + if (ObjectUtils.isEmpty(builder.count)) { |
| 94 | + builder.count(1); |
| 95 | + } |
| 96 | + this.count = builder.count; |
| 97 | + |
| 98 | + if (ObjectUtils.isEmpty(builder.temperature)) { |
| 99 | + builder.temperature(1D); |
| 100 | + } |
| 101 | + this.temperature = builder.temperature; |
| 102 | + |
| 103 | + if (ObjectUtils.isEmpty(builder.topP)) { |
| 104 | + builder.topP(1D); |
| 105 | + } |
| 106 | + this.topP = builder.topP; |
| 107 | + } |
| 108 | + |
| 109 | + public static class EditEntityBuilder |
| 110 | + { |
| 111 | + public EditEntityBuilder model(EditModel model) |
| 112 | + { |
| 113 | + this.model = model.getName(); |
| 114 | + return this; |
| 115 | + } |
| 116 | + |
| 117 | + public EditEntityBuilder input(String input) |
| 118 | + { |
| 119 | + if (StringUtils.isEmpty(input)) { |
| 120 | + throw new ParamException("Invalid input must be not empty"); |
| 121 | + } |
| 122 | + this.input = input; |
| 123 | + return this; |
| 124 | + } |
| 125 | + |
| 126 | + public EditEntityBuilder instruction(String instruction) |
| 127 | + { |
| 128 | + if (StringUtils.isEmpty(instruction)) { |
| 129 | + throw new ParamException("Invalid instruction must be not empty"); |
| 130 | + } |
| 131 | + this.instruction = instruction; |
| 132 | + return this; |
| 133 | + } |
| 134 | + |
| 135 | + public EditEntityBuilder temperature(Double temperature) |
| 136 | + { |
| 137 | + if (temperature < 0 || temperature > 2) { |
| 138 | + throw new ParamException(String.format("Invalid temperature: %s , between 0 and 2", temperature)); |
| 139 | + } |
| 140 | + this.temperature = temperature; |
| 141 | + return this; |
| 142 | + } |
| 143 | + |
| 144 | + public EditEntity build() |
| 145 | + { |
| 146 | + return new EditEntity(this); |
| 147 | + } |
| 148 | + } |
| 149 | +} |
0 commit comments