Embedder

fun interface Embedder

Turns text into a vector. kmemo ships no embedding implementation on purpose: you bring your own (OpenAI, Cohere, Voyage, a local ONNX model, whatever your stack already pays for), and kmemo stays free of provider SDKs.

Implementations must be deterministic and stable: the same text must map to the same vector for the lifetime of a cache, and every vector must have the same dimension. Switching embedding model invalidates every entry that came before it — use a fresh store, or a different scope, when you do.

Implementations are called from coroutines and must be safe to call concurrently.

val embedder = Embedder { text -> openAi.embeddings(model = "text-embedding-3-small", input = text).vector() }

Inheritors

Functions

Link copied to clipboard
abstract suspend fun embed(text: String): FloatArray

Embeds a single text. Vectors need not be normalized; kmemo normalizes on the way in.

Link copied to clipboard
open suspend fun embedAll(texts: List<String>): List<FloatArray>

Embeds texts in one go. The default implementation is a sequential loop; override it whenever your provider exposes a batch endpoint, which is usually both cheaper and faster. The result must preserve input order and have the same size as texts.

Link copied to clipboard
fun Embedder.retrying(maxAttempts: Int = RetryingEmbedder.DEFAULT_MAX_ATTEMPTS, initialDelay: Duration = RetryingEmbedder.DEFAULT_INITIAL_DELAY, maxDelay: Duration = RetryingEmbedder.DEFAULT_MAX_DELAY, factor: Double = RetryingEmbedder.DEFAULT_FACTOR, jitter: Double = RetryingEmbedder.DEFAULT_JITTER, retryOn: (Throwable) -> Boolean = { true }): RetryingEmbedder

Wraps this embedder in a RetryingEmbedder that retries transient failures with jittered backoff.