An embeddings cache keyed by content hash
Re-indexing should not recompute what has not changed. Store each vector under the hash of its text and send the model only what is new.
fakeEmbed(texts) imitates the embeddings API: it takes an array of texts and returns one vector per text. Every call is recorded in window.__embedCalls. Write three functions:
fnv1a(str): the 32-bit FNV-1a hash of the text's UTF-8 bytes (new TextEncoder().encode(str)). Start withh = 0x811c9dc5; for each byte,h ^= byteand thenh = Math.imul(h, 0x01000193) >>> 0. Returnhin hexadecimal with 8 digits.cacheKey(model, text):model + ':' + fnv1a(clean), wherecleanis the text in NFC form, with no spaces at either end and every run of whitespace reduced to one space. Do not change the case: the model tells capitals apart.makeEmbedCache(embedFn, model): return{ embedMany, stats }.
embedMany(texts) returns one vector per text, in the same order. It calls embedFn once only, with the clean texts that are not stored, each one once and in order of appearance; when none is missing, it makes no call. If embedFn fails, the error propagates and nothing is stored.
stats() returns { hits, misses }: one miss for each text that had to be sent and one hit for each text served without sending it.
A 32-bit hash is fine for learning; with millions of chunks, two different texts would end up sharing a key. Production code uses SHA-256.
Challenges 0/6
- fnv1a gives the algorithm's reference values
- The key ignores spacing, but not capitals or the model
- The first call sends only the distinct texts, in one request
- The second call sends nothing and counts the hits
- With new and known texts, only the new ones travel
- If the model fails, the error comes out and nothing is stored
function fnv1a(str) {
// h = 0x811c9dc5; for each UTF-8 byte: h ^= byte, then h = Math.imul(h, 0x01000193) >>> 0
// return h as 8 hexadecimal digits
return '00000000';
}
function cacheKey(model, text) {
// model + ':' + fnv1a of the text in NFC, trimmed, with runs of whitespace as one space
return model + ':' + text;
}
function makeEmbedCache(embedFn, model) {
const store = new Map();
let hits = 0, misses = 0;
return {
async embedMany(texts) {
// 1. clean every text and work out its key
// 2. one embedFn call with the clean texts whose key is not stored, each once
// 3. store the new vectors, count hits and misses
// 4. return one vector per input text, in input order
return embedFn(texts);
},
stats() { return { hits, misses }; },
};
}
const demo = makeEmbedCache(fakeEmbed, 'bge-m3');
demo.embedMany(CHUNKS).then((v) => console.log(v.length, 'vectors,', window.__embedCalls[0].length, 'texts sent', demo.stats()));Tab indents, Ctrl+Z undoes. To leave the editor with the keyboard: Esc, then Tab.
Why this matters
Computing embeddings is the slow part of indexing. When you change one paragraph of a manual, a naive indexer recomputes every chunk in the folder; one with a content cache recomputes only those that changed, and it does not matter that the file was moved or renamed, because the key comes from the text and not from the path. The model goes in the key for a practical reason: the day you switch embedding model, the old vectors must not mix with the new ones. The rest of the indexer, with Qdrant, is in local RAG with Qdrant and bge-m3.
Go deeper: the related guide →
This in production, with your data? Let's talk for 15 minutes →