"""One process-wide sentence-transformer shared by every semantic lookup.""" from __future__ import annotations import os import threading MODEL_NAME = "BAAI/bge-small-en-v1.5" _model = None _lock = threading.Lock() def _offline() -> bool: """Use only cached model files when any supported offline flag is set.""" truthy = {"1", "true", "yes", "on"} return any( os.environ.get(name, "").strip().lower() in truthy for name in ("CHRISTIANMCP_OFFLINE", "HF_HUB_OFFLINE", "TRANSFORMERS_OFFLINE") ) def get_embedder(): """Load the CPU model once and safely reuse it across tool modules.""" global _model if _model is None: with _lock: if _model is None: from sentence_transformers import SentenceTransformer _model = SentenceTransformer( MODEL_NAME, device="cpu", local_files_only=_offline(), ) return _model def preload_embedder(): """Eagerly initialize the shared model; useful before MCP event loops.""" return get_embedder()