Documentation
¶
Overview ¶
Package model2vec is a pure-Go inference engine for MinishLab's Model2Vec static embeddings, bundled with the potion-base-8M model.
Model2Vec is a distilled sentence-transformer represented as a lookup table: a single matrix of shape [vocab_size, dim]. Inference is just tokenize → look up each token's row → mean-pool → L2-normalize. There is no neural network at runtime, no GPU, no CGO.
The bundled model (potion-base-8M, 29 MB) is embedded via go:embed and loaded once at startup. The same Engine instance handles all queries.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func LoadEmbeddings ¶
LoadEmbeddings reads a safetensors stream and returns the embedding matrix as a flat []float32 of length vocabSize*dim, along with the inferred shape. The flat layout means row i (token id i) occupies indices [i*dim, (i+1)*dim).
Types ¶
type Engine ¶
type Engine struct {
// contains filtered or unexported fields
}
Engine is a Model2Vec semantic-similarity backend. It implements the embeddings.Engine interface so it can be swapped for TFIDF in the graph.
Construction is split from initialization: Default() loads the bundled model lazily on first use, so importing the package costs only memory for the embedded byte slices, not a 30 MB matrix allocation.
func Default ¶
Default returns an Engine initialized from the embedded potion-base-8M model. Subsequent calls return the same Engine via package-level cache, so the 30 MB matrix is decoded exactly once per process.
func New ¶
New constructs an Engine from an already-loaded vocabulary and embedding matrix. Most callers want Default() instead.
func (*Engine) Embed ¶
Embed produces a single L2-normalized embedding vector for free-form text. Empty input or all-OOV input returns a nil vector — callers should treat nil as "no signal" and fall back accordingly.
func (*Engine) Index ¶
func (e *Engine) Index(symbols []core.SymbolRecord)
Index pre-computes a normalized vector for every symbol's document text. Safe to call multiple times — each call replaces the previous index.
type Tokenizer ¶
type Tokenizer struct {
// contains filtered or unexported fields
}
Tokenizer is a minimal BERT WordPiece tokenizer matching the configuration of baai/bge-base-en-v1.5 (the base of potion-base-8M):
- lowercase = true
- strip_accents = true (default when lowercase is true)
- handle_chinese_chars = true
- continuing_subword_prefix = "##"
- max_input_chars_per_word = 100
- unk_token = "[UNK]"
Model2Vec inference does not need [CLS]/[SEP] wrapping — we tokenize the raw text into content WordPieces and average their embeddings. The Encode method therefore returns only content piece IDs.
func LoadVocab ¶
LoadVocab reads a BERT vocab.txt (one token per line, line N maps to ID N) and returns a Tokenizer ready for Encode.
func (*Tokenizer) Encode ¶
Encode tokenizes text into WordPiece content IDs. Empty input returns nil. Out-of-vocabulary words become a single UNK token.