|
| 1 | +package com.cjcrafter.openai.embeddings |
| 2 | + |
| 3 | +import com.cjcrafter.openai.util.OpenAIDslMarker |
| 4 | +import com.fasterxml.jackson.annotation.JsonProperty |
| 5 | + |
| 6 | +/** |
| 7 | + * Holds the options sent to the [embeddings](https://beta.openai.com/docs/api-reference/embeddings) endpoint. |
| 8 | + * The generated embeddings can be used in Machine Learning models. |
| 9 | + * |
| 10 | + * [input] can be either a string or a list of strings. |
| 11 | + * |
| 12 | + * @property input The input(s) to convert to embeddings. |
| 13 | + * @property model Which [model](https://platform.openai.com/docs/models/embeddings) to use to generate the embeddings. |
| 14 | + * @property encodingFormat Determines how the embeddings are encoded. Defaults to [EncodingFormat.FLOAT]. |
| 15 | + * @property user The user ID to associate with this request. |
| 16 | + * @constructor Create empty Embeddings request |
| 17 | + */ |
| 18 | +data class EmbeddingsRequest internal constructor( |
| 19 | + var input: Any, |
| 20 | + var model: String, |
| 21 | + @JsonProperty("encoding_format") var encodingFormat: EncodingFormat? = null, |
| 22 | + var user: String? = null, |
| 23 | +) { |
| 24 | + |
| 25 | + /** |
| 26 | + * A builder design pattern for constructing an [EmbeddingsRequest] instance. |
| 27 | + */ |
| 28 | + @OpenAIDslMarker |
| 29 | + class Builder internal constructor() { |
| 30 | + private var input: Any? = null |
| 31 | + private var model: String? = null |
| 32 | + private var encodingFormat: EncodingFormat? = null |
| 33 | + private var user: String? = null |
| 34 | + |
| 35 | + fun input(input: String) = apply { this.input = input } |
| 36 | + fun input(input: List<String>) = apply { this.input = input } |
| 37 | + fun model(model: String) = apply { this.model = model } |
| 38 | + fun encodingFormat(encodingFormat: EncodingFormat) = apply { this.encodingFormat = encodingFormat } |
| 39 | + fun user(user: String) = apply { this.user = user } |
| 40 | + |
| 41 | + fun build(): EmbeddingsRequest { |
| 42 | + return EmbeddingsRequest( |
| 43 | + input = input ?: throw IllegalStateException("input must be defined to use EmbeddingsRequest"), |
| 44 | + model = model ?: throw IllegalStateException("model must be defined to use EmbeddingsRequest"), |
| 45 | + encodingFormat = encodingFormat, |
| 46 | + user = user |
| 47 | + ) |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + companion object { |
| 52 | + |
| 53 | + /** |
| 54 | + * Returns a builder to construct an [EmbeddingsRequest] instance. |
| 55 | + */ |
| 56 | + @JvmStatic |
| 57 | + fun builder() = Builder() |
| 58 | + } |
| 59 | +} |
0 commit comments