In-process embeddings (FastEmbed)
WorkedWorked Example: In-Process FastEmbed Embedding with Process Cache
This is a fully solved example. Study it carefully to understand how the production code in kg/llm.py creates an in-process FastEmbed embedding model (bge-small, 384-dim) with a process-wide cache. The same pattern is used across all memory lanes to avoid re-loading ONNX weights.
Why this pattern?
FastEmbedEmbeddingloads the ONNX model into memory when instantiated. If several lanes (ingest, query, rerank) each callmake_embed(), they'd load the same weights multiple times, wasting seconds and RAM.- The solution caches the embedding object in a module-level dictionary
_EMBED_CACHEkeyed by model name. This ensures only one instance is created per model per process. - Environment variables
EMBED_MODEL,EMBED_THREADS, andEMBED_CACHE_DIRallow tuning without changing code.EMBED_MODELdefaults to"BAAI/bge-small-en-v1.5"(384 dims). - If the cache already holds a model for the current
model_name, it returns it directly – no new embedding instantiation.
Your learning task:
Read the complete solution below. The starter code provides the skeleton with imports and function signature. The solution code fills in the logic. Verify you understand:
- How
_EMBED_CACHEis used to cache instances. - How environment variables are read (
os.environ.getwith defaults). - How
FastEmbedEmbeddingis constructed with optionalthreadsandcache_dirparameters. - Why this approach avoids repeated ONNX model loading.
After studying, you should be able to explain each step and the rationale.
Your code
Sources
- roadmap-kg/kg/llm.py:156-200
- roadmap-kg/kg/ground_content.py:264-308
- roadmap-kg/kg/memory_common.py:577-606