|
| 1 | +package com.cjcrafter.openai.completions |
| 2 | + |
| 3 | +import com.google.gson.annotations.SerializedName |
| 4 | + |
| 5 | +/** |
| 6 | + * `CompletionRequest` holds the configurable options that can be sent to the OpenAI |
| 7 | + * Completions API. For most use cases, you only need to set [model] and [prompt]. |
| 8 | + * For more detailed descriptions for each option, refer to the |
| 9 | + * [Completions Uncyclo](https://platform.openai.com/docs/api-reference/completions/create). |
| 10 | + * |
| 11 | + * [prompt] can be either a singular `String`, a `List<String>`, or a `String[]`. |
| 12 | + * Providing multiple prompts is called [batching](https://platform.openai.com/docs/guides/rate-limits/batching-requests), |
| 13 | + * and it can be used to reduce rate limit errors. This will cause the [CompletionResponse] |
| 14 | + * to have multiple choices. |
| 15 | + * |
| 16 | + * You should not set [stream]. The stream option is handled using [] |
| 17 | + * |
| 18 | + * @property model ID of the model to use. |
| 19 | + * @property prompt The prompt(s) to generate completions for (either a `String`, `List<String>`, or `String[]`) |
| 20 | + * @property suffix The suffix that comes after a completion of inserted text. |
| 21 | + * @property maxTokens The maximum number of tokens to generate in the completion. |
| 22 | + * @property temperature What sampling temperature to use, between 0 and 2. |
| 23 | + * @property topP An alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with top_p probability mass. |
| 24 | + * @property n How many completions to generate for each prompt. |
| 25 | + * @property stream Whether to stream back partial progress. |
| 26 | + * @property logprobs Include the log probabilities on the logprobs most likely tokens, as well the chosen tokens. |
| 27 | + * @property echo Echo back the prompt in addition to the completion. |
| 28 | + * @property stop Up to 4 sequences where the API will stop generating further tokens. |
| 29 | + * @property presencePenalty Number between -2.0 and 2.0. Positive values penalize new tokens based on whether they appear in the text so far, increasing the model's likelihood to talk about new topics. |
| 30 | + * @property frequencyPenalty Number between -2.0 and 2.0. Positive values penalize new tokens based on their existing frequency in the text so far, decreasing the model's likelihood to repeat the same line verbatim. |
| 31 | + * @property bestOf Generates best_of completions server-side and returns the "best" (the one with the highest log probability per token). |
| 32 | + * @property logitBias Modify the likelihood of specified tokens appearing in the completion. |
| 33 | + * @property user A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse. |
| 34 | + * @constructor Create a CompletionRequest instance. Recommend using [builder] instead. |
| 35 | + */ |
| 36 | +data class CompletionRequest @JvmOverloads constructor( |
| 37 | + val model: String, |
| 38 | + val prompt: Any, |
| 39 | + val suffix: String? = null, |
| 40 | + @field:SerializedName("max_tokens") val maxTokens: Int? = null, |
| 41 | + val temperature: Number? = null, |
| 42 | + @field:SerializedName("top_p") val topP: Number? = null, |
| 43 | + val n: Int? = null, |
| 44 | + @Deprecated("Use OpenAI#streamCompletion") val stream: Boolean? = null, |
| 45 | + val logprobs: Int? = null, |
| 46 | + val echo: Boolean? = null, |
| 47 | + val stop: Any? = null, |
| 48 | + @field:SerializedName("presence_penalty") val presencePenalty: Number? = null, |
| 49 | + @field:SerializedName("frequency_penalty") val frequencyPenalty: Number? = null, |
| 50 | + @field:SerializedName("best_of") val bestOf: Int? = null, |
| 51 | + @field:SerializedName("logit_bias") val logitBias: Map<String, Int>? = null, |
| 52 | + val user: String? = null |
| 53 | +) { |
| 54 | + |
| 55 | + /** |
| 56 | + * `Builder` is a helper class to build a [CompletionRequest] instance with a |
| 57 | + * stable API. It provides methods for setting the properties fo the [CompletionRequest] |
| 58 | + * object. The [build] method returns a new [CompletionRequest] instance with the |
| 59 | + * specified properties. |
| 60 | + * |
| 61 | + * Usage: |
| 62 | + * ``` |
| 63 | + * val completionRequest = CompletionRequest.builder() |
| 64 | + * .model("davinci") |
| 65 | + * .prompt("The wheels on the bus go") |
| 66 | + * .build() |
| 67 | + * ``` |
| 68 | + */ |
| 69 | + class Builder { |
| 70 | + private var model: String? = null |
| 71 | + private var prompt: Any? = null |
| 72 | + private var suffix: String? = null |
| 73 | + private var maxTokens: Int? = null |
| 74 | + private var temperature: Number? = null |
| 75 | + private var topP: Number? = null |
| 76 | + private var n: Int? = null |
| 77 | + private var stream: Boolean? = null |
| 78 | + private var logprobs: Int? = null |
| 79 | + private var echo: Boolean? = null |
| 80 | + private var stop: Any? = null |
| 81 | + private var presencePenalty: Number? = null |
| 82 | + private var frequencyPenalty: Number? = null |
| 83 | + private var bestOf: Int? = null |
| 84 | + private var logitBias: Map<String, Int>? = null |
| 85 | + private var user: String? = null |
| 86 | + |
| 87 | + /** |
| 88 | + * Sets the model for the CompletionRequest. |
| 89 | + * @param model The ID of the model to use. |
| 90 | + * @return The updated Builder instance. |
| 91 | + */ |
| 92 | + fun model(model: String) = apply { this.model = model } |
| 93 | + |
| 94 | + /** |
| 95 | + * Sets the prompt for the CompletionRequest. |
| 96 | + * @param prompt The prompt to generate completions for, encoded as a string. |
| 97 | + * @return The updated Builder instance. |
| 98 | + */ |
| 99 | + fun prompt(prompt: String?) = apply { this.prompt = prompt } |
| 100 | + |
| 101 | + /** |
| 102 | + * Sets the list of prompts for the CompletionRequest. |
| 103 | + * @param prompts The prompts to generate completions for, encoded as a list of strings. |
| 104 | + * @return The updated Builder instance. |
| 105 | + */ |
| 106 | + fun prompts(prompts: List<String>?) = apply { this.prompt = prompts } |
| 107 | + |
| 108 | + /** |
| 109 | + * Sets the array of prompts for the CompletionRequest. |
| 110 | + * @param prompts The prompts to generate completions for, encoded as an array of strings. |
| 111 | + * @return The updated Builder instance. |
| 112 | + */ |
| 113 | + fun prompts(prompts: Array<String>?) = apply { this.prompt = prompts } |
| 114 | + |
| 115 | + /** |
| 116 | + * Sets the suffix for the CompletionRequest. |
| 117 | + * @param suffix The suffix that comes after a completion of inserted text. |
| 118 | + * @return The updated Builder instance. |
| 119 | + */ |
| 120 | + fun suffix(suffix: String?) = apply { this.suffix = suffix } |
| 121 | + |
| 122 | + /** |
| 123 | + * Sets the maximum number of tokens for the CompletionRequest. |
| 124 | + * @param maxTokens The maximum number of tokens to generate in the completion. |
| 125 | + * @return The updated Builder instance. |
| 126 | + */ |
| 127 | + fun maxTokens(maxTokens: Int?) = apply { this.maxTokens = maxTokens } |
| 128 | + |
| 129 | + /** |
| 130 | + * Sets the temperature for the CompletionRequest. |
| 131 | + * @param temperature What sampling temperature to use, between 0 and 2. |
| 132 | + * @return The updated Builder instance. |
| 133 | + */ |
| 134 | + fun temperature(temperature: Number?) = apply { this.temperature = temperature } |
| 135 | + |
| 136 | + /** |
| 137 | + * Sets the top_p for the CompletionRequest. |
| 138 | + * @param topP An alternative to sampling with temperature, called nucleus sampling. |
| 139 | + * @return The updated Builder instance. |
| 140 | + */ |
| 141 | + fun topP(topP: Number?) = apply { this.topP = topP } |
| 142 | + |
| 143 | + /** |
| 144 | + * Sets the number of completions for the CompletionRequest. |
| 145 | + * @param n How many completions to generate for each prompt. |
| 146 | + * @return The updated Builder instance. |
| 147 | + */ |
| 148 | + fun n(n: Int?) = apply { this.n = n } |
| 149 | + |
| 150 | + /** |
| 151 | + * Sets the stream option for the CompletionRequest. |
| 152 | + * @param stream Whether to stream back partial progress. |
| 153 | + * @return The updated Builder instance. |
| 154 | + */ |
| 155 | + fun stream(stream: Boolean?) = apply { this.stream = stream } |
| 156 | + |
| 157 | + /** |
| 158 | + * Sets the logprobs for the CompletionRequest. |
| 159 | + * @param logprobs Include the log probabilities on the logprobs most likely tokens, as well as the chosen tokens. |
| 160 | + * @return The updated Builder instance. |
| 161 | + */ |
| 162 | + fun logprobs(logprobs: Int?) = apply { this.logprobs = logprobs } |
| 163 | + |
| 164 | + /** |
| 165 | + * Sets the echo option for the CompletionRequest. |
| 166 | + * @param echo Echo back the prompt in addition to the completion. |
| 167 | + * @return The updated Builder instance. |
| 168 | + */ |
| 169 | + fun echo(echo: Boolean?) = apply { this.echo = echo } |
| 170 | + |
| 171 | + /** |
| 172 | + * Sets the stop sequences for the CompletionRequest. |
| 173 | + * @param stop Up to 4 sequences where the API will stop generating further tokens. |
| 174 | + * @return The updated Builder instance. |
| 175 | + */ |
| 176 | + fun stop(stop: Any?) = apply { this.stop = stop } |
| 177 | + |
| 178 | + /** |
| 179 | + * Sets the presence penalty for the CompletionRequest. |
| 180 | + * @param presencePenalty Number between -2.0 and 2.0 for penalizing new tokens based on whether they appear in the text so far. |
| 181 | + * @return The updated Builder instance. |
| 182 | + */ |
| 183 | + fun presencePenalty(presencePenalty: Number?) = apply { this.presencePenalty = presencePenalty } |
| 184 | + |
| 185 | + /** |
| 186 | + * Sets the frequency penalty for the CompletionRequest. |
| 187 | + * @param frequencyPenalty Number between -2.0 and 2.0 for penalizing new tokens based on their existing frequency in the text so far. |
| 188 | + * @return The updated Builder instance. |
| 189 | + */ |
| 190 | + fun frequencyPenalty(frequencyPenalty: Number?) = apply { this.frequencyPenalty = frequencyPenalty } |
| 191 | + |
| 192 | + /** |
| 193 | + * Sets the best_of option for the CompletionRequest. |
| 194 | + * @param bestOf Generates best_of completions server-side and returns the "best" (the one with the highest log probability per token). |
| 195 | + * @return The updated Builder instance. |
| 196 | + */ |
| 197 | + fun bestOf(bestOf: Int?) = apply { this.bestOf = bestOf } |
| 198 | + |
| 199 | + /** |
| 200 | + * Sets the logit bias for the CompletionRequest. |
| 201 | + * @param logitBias Modify the likelihood of specified tokens appearing in the completion. |
| 202 | + * @return The updated Builder instance. |
| 203 | + */ |
| 204 | + fun logitBias(logitBias: Map<String, Int>?) = apply { this.logitBias = logitBias } |
| 205 | + |
| 206 | + /** |
| 207 | + * Sets the user identifier for the CompletionRequest. |
| 208 | + * @param user A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse. |
| 209 | + * @return The updated Builder instance. |
| 210 | + */ |
| 211 | + fun user(user: String?) = apply { this.user = user } |
| 212 | + |
| 213 | + /** |
| 214 | + * Builds the CompletionRequest instance with the provided parameters. |
| 215 | + * |
| 216 | + * @return The constructed CompletionRequest instance. |
| 217 | + */ |
| 218 | + fun build(): CompletionRequest { |
| 219 | + require(model != null) { "Set CompletionRequest.Builder#model(String) before building" } |
| 220 | + require(prompt != null) { "Set CompletionRequest.Builder#prompt(String) before building" } |
| 221 | + |
| 222 | + return CompletionRequest( |
| 223 | + model = model!!, |
| 224 | + prompt = prompt!!, |
| 225 | + suffix = suffix, |
| 226 | + maxTokens = maxTokens, |
| 227 | + temperature = temperature, |
| 228 | + topP = topP, |
| 229 | + n = n, |
| 230 | + stream = stream, |
| 231 | + logprobs = logprobs, |
| 232 | + echo = echo, |
| 233 | + stop = stop, |
| 234 | + presencePenalty = presencePenalty, |
| 235 | + frequencyPenalty = frequencyPenalty, |
| 236 | + bestOf = bestOf, |
| 237 | + logitBias = logitBias, |
| 238 | + user = user |
| 239 | + ) |
| 240 | + } |
| 241 | + } |
| 242 | + |
| 243 | + companion object { |
| 244 | + |
| 245 | + /** |
| 246 | + * A static method that provides a new [Builder] instance for the |
| 247 | + * [CompletionRequest] class. |
| 248 | + * |
| 249 | + * @return a new [Builder] instance for creating a [CompletionRequest] object. |
| 250 | + */ |
| 251 | + @JvmStatic |
| 252 | + fun builder(): Builder = Builder() |
| 253 | + } |
| 254 | +} |
| 255 | + |
0 commit comments