ir.embed¶
Embedder resolution for ir — a decent local default, a light fallback.
ir favors a decent local embedding so retrieval works offline and tests
need no API keys, while keeping a light (numpy-only) option for when semantic
power is not what’s under test.
"default"/"local"/"minilm"→all-MiniLM-L6-v2(384-dim) viaef.embedder_adapters.sentence_transformers_embedder()(normalize=True), wrapped inef.CachedEmbedderover adolcache under~/.cache/ir/embeddings/<model>. Ifsentence-transformersis unavailable, it degrades to the hashing embedder with a warning."light"/"hashing"→ef.HashingEmbedder(numpy only).any other string → treated as a sentence-transformers model name.
a callable / existing
Embedder→ passed throughef.as_embedder.
make_embedder() returns (embedder, embedder_id); the id pins the
model in the corpus ledger so a model change triggers a re-embed (the SSOT
discipline that keeps the index from silently drifting).
Importing this module sets USE_TF=0 so sentence-transformers (via
transformers) does not import TensorFlow, which crashes on this stack’s
numpy ABI. Import ir before anything that imports transformers.
Functions
|
Resolve spec to |
|
Re-create the embedder a corpus was built with, from its stored id. |
Exceptions
A corpus cannot be queried with the embedder at hand. |
|
The query embedding's dimension differs from the index's. |
|
The embedder a corpus was built with cannot be loaded here. |
- exception ir.embed.EmbedderError[source]¶
Bases:
ValueErrorA corpus cannot be queried with the embedder at hand.
Subclasses
ValueError(the type earlier versions raised) so existingexcept ValueErrorcallers keep working. The message is always actionable: it says what is wrong and the command that fixes it.
- exception ir.embed.EmbedderMismatch[source]¶
Bases:
EmbedderErrorThe query embedding’s dimension differs from the index’s.
Bases:
EmbedderErrorThe embedder a corpus was built with cannot be loaded here.
- ir.embed.make_embedder(spec='default', *, cache=True, strict=False)[source]¶
Resolve spec to
(embedder, embedder_id).embedderis a batch callableIterable[str] -> ndarray(n, dim)that also acceptsinput_type=("query"/"document").With
strict=Truea local model that cannot be loaded raisesEmbedderUnavailableinstead of degrading to the hashing embedder (used when reopening a corpus, whose vectors only match its own model).
- ir.embed.make_embedder_for_id(embedder_id, *, cache=True)[source]¶
Re-create the embedder a corpus was built with, from its stored id.
The ids
make_embedder()emits are invertible:hashing__dim<N>andst__<model>. ReturnsNonefor an id that is not one of those forms (e.g."custom", or empty), so the caller falls back to the stored spec. Never degrades silently: an unavailable model raisesEmbedderUnavailable, and an id of one of those forms that does not parse raisesEmbedderError(falling back to the spec there is the drift this exists to prevent). This is what pins a corpus to its own embedder, so the machine querying it (with or withoutsentence-transformers) cannot change the vector space.