Source code for aeat.application.corpus_search._model_loader
"""Shared model2vec loader for the semantic-search stack.Both the build-time precompute(:mod:`~application.corpus_search._embed_build`) and the runtime query embedder(:mod:`~application.corpus_search._query_embed`) load the same``potion-multilingual-128M`` static model behind the capability-gated``aeat-cli[search]`` extra. This module is the one place that lazily imports``model2vec`` and refuses with an install hint when it is absent, so neitherconsumer duplicates the gate and the degraded lexical-only mode never dependson the semantic stack at import time."""from__future__importannotationsimportinspectfrompathlibimportPathfromtypingimportAnyfrom._errorsimportCorpusSearchDependencyError#: Install hint surfaced on every semantic-stack refusal.SEARCH_EXTRA_HINT="pip install aeat-cli[search]"_DEFAULT_DIMENSIONS=256
[docs]defload_static_model(model_id:str,*,revision:str,cache_dir:Path|None=None)->Any:"""Load a model2vec ``StaticModel``, refusing if the ``search`` extra is absent. Args: model_id: The model2vec model to load. revision: The pinned model revision, passed through when the installed ``from_pretrained`` accepts it. cache_dir: Optional app-controlled cache directory for the download. Returns: The loaded ``StaticModel`` (typed ``Any`` — model2vec ships no stubs). Raises: CorpusSearchDependencyError: If ``model2vec`` is not installed. """try:# IMPORT-RATIONALE-OPTIONAL-SEARCH-EXTRA: model2vec rides the optional# aeat-cli[search] extra; a bare-core install lacks it, so the import is# lazy and its typing is unresolved until the extra is present.frommodel2vecimport(StaticModel,# type: ignore[import-not-found, unused-ignore] # TYPE-IGNORE-RATIONALE-optextra: model2vec is an optional extra without shipped type stubs)exceptImportErrorasexc:raiseCorpusSearchDependencyError("the corpus-search semantic stack (model2vec) is not installed",context={"model_id":model_id,"dependency":"model2vec"},suggestion=SEARCH_EXTRA_HINT,)fromexckwargs:dict[str,object]={}accepted=inspect.signature(StaticModel.from_pretrained).parametersif"revision"inaccepted:kwargs["revision"]=revisionifcache_dirisnotNoneand"cache_dir"inaccepted:kwargs["cache_dir"]=str(cache_dir)returnStaticModel.from_pretrained(model_id,**kwargs)
# KWARGS-ANY-RATIONALE-cli: model is an untyped model2vec StaticModel optional-extra object
[docs]defmodel_dimensions(model:Any,)->int:# KWARGS-ANY-RATIONALE-optextra: model is an untyped model2vec StaticModel optional-extra object"""Return the embedding dimensionality of a loaded ``StaticModel``."""dim=getattr(model,"dim",None)ifisinstance(dim,int)anddim>0:returndimembedding=getattr(model,"embedding",None)shape=getattr(embedding,"shape",None)ifshapeisnotNoneandlen(shape)==2:returnint(shape[1])return_DEFAULT_DIMENSIONS