embedding-cache-lite
v0.1.0
Published
Content-hash cache for embedding vectors — skip re-embedding text you've already embedded, keyed by a hash of the text plus model name.
Maintainers
Readme
embedding-cache-lite
Content-hash cache for embedding vectors. Wraps any embedder function so identical text (per model) only gets embedded once — cuts redundant API calls in RAG ingestion pipelines that re-run over overlapping content.
Install
npm install embedding-cache-liteQuick start
import { EmbeddingCache } from 'embedding-cache-lite';
const cache = new EmbeddingCache(
(text) => openai.embeddings.create({ input: text, model: 'text-embedding-3-small' }).then(r => r.data[0].embedding),
{ model: 'text-embedding-3-small', maxEntries: 10000 }
);
const vector = await cache.embed(chunk.text); // only calls the API on a real miss
console.log(cache.stats()); // { hits, misses }Why key on text + model
The same text embedded with two different models produces different, non-comparable vectors — caching purely by text content would silently return the wrong model's vector if you ever switch models or run two side by side. Keying on a hash of model:text keeps that safe by construction.
API
new EmbeddingCache(embedder, options?)—options: { model?, maxEntries? }.embed(text)— returns the cached vector or callsembedderand caches the result.has(text)/.size()/.stats()/.clear()
Eviction is simple LRU once maxEntries is exceeded.
License
MIT
