Back to Practice

Persistent embedding cache

Completion

Task: Implement the core caching logic with float32 round-trip

You are given a partially written function get_cached_embedding(text, model, cache) that should mimic the per-text caching logic used in cached_text_embeddings from the repository source. Your job is to complete the two missing parts:

  1. Compute the cache key using embed_cache_key(text, model=model) — this generates a SHA‑256 hash of "{model}\x1f{text}" (as seen in the source).
  2. Round‑trip the embedding vector through float32 before storing it in the cache. This ensures that a freshly embedded (miss) vector returns exactly the same float values as a later cache hit that reads from the packed float32 BLOB — avoiding the miss/hit precision asymmetry described in the comment.

The scaffold provides a mock embed_model that returns a dummy vector, a simple dictionary cache (like the SQLite store), and the embed_cache_key definition from the repository. Fill in the # TODO: lines.

Important: Use the exact same functions and variable names as in the real source: hashlib.sha256, array.array('f'), .tobytes(), .frombytes(), and the key construction with \x1f (chr(31)).

Your code
Sources
  • roadmap-kg/kg/memory_common.py:643-685
  • roadmap-kg/kg/memory_common.py:757-804
  • roadmap-kg/kg/ground_content.py:264-308