aeat.application.corpus_search._embed_build module¶
Build-time corpus embedding precompute (the semantic half of R3).
The corpus is static and bundled, so its embeddings are precomputed ONCE
at build time with model2vec (potion-multilingual-128M — MIT code
and MIT weights) and shipped as a plain float32 numpy matrix plus a
parallel chunk-id list. Model outputs are shippable per the licence-clean
rule; the query model is needed ONLY to embed a live query, so a build
that ships the corpus vectors carries a degraded, no-download semantic
mode: the “more-like-this-document” primitive here runs over the shipped
matrix with numpy alone and never touches the model.
model2vec rides the capability-gated aeat-cli[search] extra. When it
is absent this module still imports (the degraded lexical-only mode stays
live) and embed_corpus() refuses with an install hint rather than
crashing, mirroring every other optional-integration boundary in the
tree. numpy is imported function-locally for the same reason, so the
lexical-only surface never depends on it at import time.
See also
ensure_corpus_embeddings()Runtime build-once cache that calls this precompute behind the
aeat-cli[search]extra.QueryEmbedderLive-query half that embeds operator text into the same vector space.
hybrid_search()Retrieval fusion consumer of the precomputed matrix.
- embed_corpus(chunks, *, matrix_path, chunk_ids_path, model_id='minishlab/potion-multilingual-128M', revision='73908c3438cf03b6a01bcb9611d62b23d0726f08', cache_dir=None)[source]¶
Precompute and persist corpus embeddings for
chunks.- Parameters:
chunks (
Iterable[CorpusChunk]) – The chunk sequence to embed, typicallyiter_corpus_chunks(). Order is preserved so the matrix stays row-aligned with the persisted chunk-id list.matrix_path (
Path) – Destination.npyfile for the float32 matrix.chunk_ids_path (
Path) – Destination.jsonfile for the parallel chunk-id list.model_id (
str) – The model2vec model to load.revision (
str) – The pinned model revision, recorded for provenance.cache_dir (
Path|None) – Optional app-controlled model cache directory.
- Return type:
- Returns:
A
CorpusEmbeddingBuildResultdescribing the written artifacts.- Raises:
CorpusSearchDependencyError – If the
searchextra (model2vec) is not installed.
- load_embeddings(matrix_path, chunk_ids_path)[source]¶
Load a precomputed matrix and its parallel chunk-id list.
- Parameters:
matrix_path (
Path) – A.npymatrix written byembed_corpus().chunk_ids_path (
Path) – The parallel.jsonchunk-id list.
- Return type:
- Returns:
The loaded matrix and the chunk-id tuple.
- more_like_this(matrix, chunk_ids, query_chunk_id, *, top_k=5)[source]¶
Return the cosine-nearest chunks to
query_chunk_id.Runs over the precomputed matrix with numpy alone — no model, so this is the degraded, no-download semantic primitive. The query chunk itself is excluded from its own results.
- Parameters:
- Return type:
- Returns:
Up to
top_kSimilarChunkrecords, most similar first.- Raises:
CorpusSearchInputError – If
query_chunk_idis not inchunk_ids,top_kis not positive, or the matrix and id list disagree in length.