agrag.embedding.cached.CachedEmbedder
CachedEmbedder(inner:Embedder, *, maxsize:int = 256) -> None
Bases: Embedder
An Embedder that caches query vectors and deduplicates in-flight calls.
Wraps another embedder with an in-memory LRU keyed by text, so repeated queries (for example the same query hitting two search tools in one turn) embed once. Texts that are cached or already being embedded by an earlier call are not sent to the inner embedder; the remaining texts go in one batch call. A failed inner call resolves its in-flight futures with the error and is retried on the next call. Cached vectors are stored and returned as copies, so callers cannot mutate them.
Functions:
- dimensions – Return the wrapped embedder's dimension, cached after the first call.
- embed – Embed a batch of texts, using the cache and in-flight sharing.
- embed_one – Embed a single text.
Attributes:
- distance (
Distance) – The wrapped embedder's distance metric. - model (
str) – The wrapped embedder's model name.
Parameters:
- inner (
Embedder) – The embedder that performs the actual embedding. - maxsize (
int) – The most vectors the LRU cache holds. The least recently used entry is evicted beyond this.
dimensions
dimensions() -> int
Return the wrapped embedder's dimension, cached after the first call.
Returns:
int– The dimension of the vectors the wrapped embedder produces.
distance
distance: Distance
The wrapped embedder's distance metric.
embed
embed(texts:Sequence[str]) -> list[list[float]]
Embed a batch of texts, using the cache and in-flight sharing.
Parameters:
- texts (
Sequence[str]) – The texts to embed, in order. Duplicates are embedded once.
Returns:
list[list[float]]– One vector per input text, in the same order. Vector values arelist[list[float]]– fresh copies; mutating a returned vector never changes thelist[list[float]]– cache.
embed_one
embed_one(text:str) -> list[float]
Embed a single text.
Parameters:
- text (
str) – The text to embed.
Returns:
list[float]– The text's embedding vector.
model
model: str
The wrapped embedder's model name.