CacheBackedEmbeddings<T>
Models & Types · AiDotNet.RetrievalAugmentedGeneration.Embeddings
An IEmbeddingModel decorator that caches embeddings by content hash so that
repeated texts are not re-embedded, mirroring LangChain’s CacheBackedEmbeddings and
LlamaIndex’s ingestion cache.
Implements: IEmbeddingModel<T>
For Beginners
This wraps a “real” embedding model and remembers its answers.
- The first time you embed a piece of text, it calls the wrapped model and saves the result.
- The next time you embed the same text, it returns the saved result instead of calling the model again.
- This saves time and money when the same text shows up more than once (very common in RAG pipelines).
How It Works
Each text is hashed with SHA-256 (see ContentHash) and the resulting key is namespaced
by the wrapped model’s identity and embedding dimension, so different models never collide in a shared
cache. On a cache hit the stored vector is returned; on a miss the inner model is invoked and the result
is stored. Batch operations de-duplicate texts both within a single call and across calls, invoke the
inner model only for the distinct misses, and preserve the input ordering of the returned matrix rows.
Constructors
| Constructor | Summary |
|---|---|
CacheBackedEmbeddings(IEmbeddingModel<T> inner, IEmbeddingCache<T> cache, string modelNamespace) |
Initializes a new instance of the CacheBackedEmbeddings class. |
Properties
| Property | Summary |
|---|---|
IEmbeddingCache<T> Cache { get; } |
Gets the cache used to store embeddings. |
IEmbeddingModel<T> InnerModel { get; } |
Gets the underlying embedding model that this cache wraps. |
int EmbeddingDimension { get; } |
|
int MaxTokens { get; } |
|
string Namespace { get; } |
Gets the key namespace that identifies the wrapped model within the cache. |
Methods
| Method | Summary |
|---|---|
Matrix<T> EmbedBatch(IEnumerable<string> texts) |
|
Task<Matrix<T>> EmbedBatchAsync(IEnumerable<string> texts) |
|
Task<Vector<T>> EmbedAsync(string text) |
|
Vector<T> Embed(string text) |