embed

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jun 29, 2026 License: MIT Imports: 6 Imported by: 0

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

View Source
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.

View Source
const Dim = 384

Dim is the embedding dimensionality of all-MiniLM-L6-v2.

View Source
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

View Source
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

func MeanPool(flat []float32, mask []int64, batch, seqLen, dim int) [][]float32

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.

func ModelDir

func ModelDir(model string) (string, error)

ModelDir returns the on-disk directory for a model under the user's mnemos home (~/.mnemos/models/<model>). It is where `models install` downloads the weights and where New loads them from. It does not create the directory.

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.

func New

func New(_ string) (Embedder, error)

New returns the no-op embedder. modelDir is ignored in the default build; the returned embedder never loads a model and never succeeds at embedding.

Jump to

Keyboard shortcuts

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