model2vec

package
v0.19.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 8, 2026 License: Apache-2.0 Imports: 15 Imported by: 0

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

func LoadEmbeddings(r io.Reader) (matrix []float32, vocabSize, dim int, err error)

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

func Default() (*Engine, error)

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

func New(tok *Tokenizer, matrix []float32, dim int) (*Engine, error)

New constructs an Engine from an already-loaded vocabulary and embedding matrix. Most callers want Default() instead.

func (*Engine) Dim

func (e *Engine) Dim() int

Dim returns the embedding dimension.

func (*Engine) Embed

func (e *Engine) Embed(text string) []float32

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.

func (*Engine) IndexWithCache added in v0.6.0

func (e *Engine) IndexWithCache(symbols []core.SymbolRecord, cache map[string][]float32)

IndexWithCache is Index with vector reuse across rebuilds. A symbol's vector is a pure function of its document text, and symbol IDs embed the file content SHA — so a cache hit by ID is always valid. After a delta reindex of a large repo this turns the first query's full-corpus re-embed into embedding only the symbols of changed files. Entries whose IDs are gone are pruned so the cache tracks the live symbol set.

func (*Engine) Query

func (e *Engine) Query(query string, limit int) []embeddings.Scored

Query returns the top-`limit` symbols ranked by cosine similarity. Returns nil if the engine has not been indexed or the query produces no signal.

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

func LoadVocab(r io.Reader) (*Tokenizer, error)

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

func (t *Tokenizer) Encode(text string) []int32

Encode tokenizes text into WordPiece content IDs. Empty input returns nil. Out-of-vocabulary words become a single UNK token.

func (*Tokenizer) Size

func (t *Tokenizer) Size() int

Size returns the number of entries in the vocabulary.

func (*Tokenizer) UnkID

func (t *Tokenizer) UnkID() int32

UnkID returns the token ID used for unknown tokens.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL