Skip to content

v0.6.0

Choose a tag to compare

@funakoshi-takehiro funakoshi-takehiro released this 19 Aug 23:26
· 13 commits to main since this release
44f5590
Add ai.embed and ai.search for meaning-based lookup

The host's ai-embed bridge is in place, so this is the Python entry point
students write against: embed() turns text into a vector, search() ranks
documents by meaning. A string in gives one vector back, a list gives a
list in the same order, and vectors arrive L2-normalised so a dot product
is already the cosine.

Embedding models live in EMBED_MODELS, not alongside the chat ones. Put
them in MODELS and three things break at once: models() advertises a
model you cannot generate with, load("minilm") succeeds and leaves a
non-generative model in _pipe for ask() to choke on, and recommend()
becomes free to suggest it. Tests hold the separation.

The host refuses more than 256 texts, and passing that through would mean
the same notebook failing in PyHiroba and succeeding in Colab — the exact
divergence this library exists to prevent. embed() splits into batches
itself, so the limit is invisible. search() sends the query and documents
in one call rather than two, since each round trip is a bridge crossing.

Colab pools and normalises against transformers and torch directly.
sentence-transformers wants transformers>=5 and brings scikit-learn and
scipy along for fifteen lines of arithmetic, and writing the same steps
the host writes is what keeps the two paths meaning the same thing. The
masked mean is checked numerically against a hand-computed reference:
padding carries an extreme value, so a mean that forgot to mask would be
nowhere near the expected answer.

A vector that comes back unnormalised would not raise anything — it would
just quietly reorder results — so one length is checked per call.