IEmbeddingCache<T>

Interfaces · AiDotNet.RetrievalAugmentedGeneration.Embeddings

Defines a cache that stores previously computed embedding vectors keyed by a stable string key.

Known implementations: InMemoryEmbeddingCache<T>

For Beginners

This is a lookup table from “fingerprint of some text” to “its embedding”.

  • Before asking the (possibly slow or paid) model to embed text, we check the cache.
  • If the answer is already there, we reuse it instead of computing it again.
  • Implementations must be safe to call from multiple threads at once.

How It Works

A cache lets a CacheBackedEmbeddings decorator avoid re-embedding text that has already been embedded. Keys are expected to be content-derived (see ContentHash) and namespaced by the underlying model’s identity so that different models never share entries.

Properties

Property Summary
int Count { get; } Gets the number of entries currently held in the cache.

Methods

Method Summary
bool TryGet(string key, Vector<T> embedding) Attempts to retrieve a cached embedding for the given key.
void Clear() Removes all entries from the cache.
void Set(string key, Vector<T> embedding) Stores an embedding in the cache under the given key, overwriting any existing entry.