Documentation
¶
Overview ¶
Package embed turns text into dense sentence embeddings for semantic search.
The heavy ONNX inference stack (gomlx + onnx-gomlx + sugarme/tokenizer) lives behind the "embed" build tag in onnx.go, so the default build stays lean and cgo-free with only the no-op embedder (noop.go) compiled in. Callers depend on the Embedder interface and the build-tag-selected New constructor; the rest of the package (pooling, normalization, the model/dim constants) is pure Go and shared by both builds so it can be unit-tested without the tag or a model.
Index ¶
Constants ¶
const DefaultModel = "all-MiniLM-L6-v2"
DefaultModel is the canonical model name stored alongside each vector. It is the sentence-transformers all-MiniLM-L6-v2 checkpoint.
const Dim = 384
Dim is the embedding dimensionality of all-MiniLM-L6-v2.
const Supported = false
Supported reports whether this binary was built with semantic-embedding support. The default (no-tag) build is false; rebuild with -tags embed to enable the ONNX embedder. CLI and retrieval code branch on this constant so they compile identically in both builds.
Variables ¶
var ErrNotSupported = errors.New("embed: built without embedding support (rebuild with -tags embed)")
ErrNotSupported is returned by the no-op embedder (default build). Retrieval code treats it as "semantic search unavailable" and degrades to lexical-only rather than failing. Inspect it with errors.Is.
Functions ¶
func L2Normalize ¶
func L2Normalize(vec []float32)
L2Normalize scales vec in place to unit Euclidean length. A zero vector is left unchanged (no division by zero). After normalization the cosine similarity between two vectors equals their dot product, which is what the vector search relies on.
func MeanPool ¶
MeanPool applies attention-mask-weighted mean pooling over the token axis of a transformer's last hidden state. flat is the row-major [batch, seqLen, dim] output; mask is the row-major [batch, seqLen] attention mask (1 = real token, 0 = padding). It returns one unnormalized dim-length vector per batch row: the average of the hidden states of the unmasked tokens. Rows with no unmasked tokens yield a zero vector. The result is not L2-normalized; call L2Normalize.
Types ¶
type Embedder ¶
type Embedder interface {
// Embed returns one L2-normalized Dim-length vector per input text. An empty
// input yields a nil slice and no error.
Embed(ctx context.Context, texts []string) ([][]float32, error)
// Dim reports the embedding dimensionality.
Dim() int
// Model reports the model name persisted with each vector.
Model() string
}
Embedder turns texts into L2-normalized sentence embeddings. Implementations must return one vector per input text, each of length Dim. Embed is the single blocking method and takes a context so long batches can be cancelled.