Command Palette

Search for a command to run...

Hectal
Phase 2: Embeddings in Depth
RAG and Vector Search · Exercise 5 of 5 Previous
Intermediate

Cache embeddings by content and model

embed_batch(texts) is a function you are given that returns one vector per text and costs money each call. Write the class CachedEmbedder(embed_batch, model_name) with a method embed_many(texts) that returns vectors in the order of texts.

Keep a cache keyed by model_name plus a hash of the text (content_hash(text) is given). Only texts that are not in the cache are sent, once each, in a single call to embed_batch, and no call is made when nothing is missing. Count the calls in self.calls and the texts sent in self.sent. Changing self.model_name must make the old cache entries unusable.

What Check tests

  1. Vectors come back in the order of the texts
  2. A repeated text is sent only once
  3. Texts already cached cause no call
  4. Only the new text is sent when one is added
  5. A different model name does not reuse old vectors

Stuck? One hint at a time

The solution unlocks after your first Check. Try first: that is where the learning happens.

main.py
Sign in to run your code.

Output

Press Run to see what your program prints.