cache.wrapped_embeddings

chunktuner.cache.wrapped_embeddings

Wrap an EmbeddingFunction with SQLite caching.

CachedEmbeddingFunction

CachedEmbeddingFunction(inner, cache)

Delegates to inner while reading/writing vectors through EmbeddingCache.

Source code in src/chunktuner/cache/wrapped_embeddings.py
def __init__(self, inner: EmbeddingFunction, cache: EmbeddingCache):
    self._inner = inner
    self._cache = cache
    self.profile_name = inner.profile_name

maybe_cache_embeddings

maybe_cache_embeddings(fn, cache_dir, *, enabled=True)

Wrap a real embedding function with the SQLite cache under cache_dir.

Returns fn unchanged when enabled is False or fn is a DummyEmbeddingFunction (deterministic and free — caching would only add I/O). The cache is keyed by fn.profile_name (the embedding model id), so results are identical with and without the cache.

Source code in src/chunktuner/cache/wrapped_embeddings.py
def maybe_cache_embeddings(
    fn: EmbeddingFunction,
    cache_dir: Path,
    *,
    enabled: bool = True,
) -> EmbeddingFunction:
    """Wrap a real embedding function with the SQLite cache under ``cache_dir``.

    Returns ``fn`` unchanged when ``enabled`` is False or ``fn`` is a
    ``DummyEmbeddingFunction`` (deterministic and free — caching would only add I/O).
    The cache is keyed by ``fn.profile_name`` (the embedding model id), so results are
    identical with and without the cache.
    """
    from chunktuner.eval.embeddings import DummyEmbeddingFunction

    if not enabled or isinstance(fn, DummyEmbeddingFunction | CachedEmbeddingFunction):
        return fn
    cache = EmbeddingCache(default_embedding_db_path(cache_dir), fn.profile_name)
    return CachedEmbeddingFunction(fn, cache)