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
- Vectors come back in the order of the texts
- A repeated text is sent only once
- Texts already cached cause no call
- Only the new text is sent when one is added
- 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.
Output
Press Run to see what your program prints.