embed

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2026 License: MIT Imports: 7 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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 model used when [embedding].model is unset: the sentence-transformers all-MiniLM-L6-v2 checkpoint. It is the fallback name, not the name every vector carries: an embedder reports the model it actually loaded (see ModelName).

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.

func ModelName added in v0.3.0

func ModelName(modelDir string) string

ModelName is the inverse of ModelDir: it recovers the model name from a model directory, and is what an embedder reports as the identity of the weights it loaded. A trailing separator is tolerated. An empty or filesystem-root dir yields DefaultModel, so a caller that never set a name still records the conventional one rather than an empty or nonsense label.

func ResolveModel added in v0.3.0

func ResolveModel(configured string) string

ResolveModel picks the embedding model name: the configured value when set, otherwise DefaultModel. Callers pass Config.Embedding.Model straight in, so the empty-means-default rule lives in one place.

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(modelDir string) (Embedder, error)

New returns the no-op embedder. No model is loaded and Embed never succeeds, but modelDir still sets the reported Model name so both builds agree on the identity for a given directory.

Jump to

Keyboard shortcuts

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