embed

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 28, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package embed turns text into vectors for retrieval and memory. The Embedder interface keeps the rest of the system independent of the embedding model. Two implementations ship: a deterministic, offline HashEmbedder (default — zero deps, always works, used for tests and air-gapped first-run) and an Ollama embedder (higher quality when a local embedding model is available).

Whichever produces a pack's vectors is pinned by ID in the pack manifest, so a pack can never be queried with a mismatched embedder.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Tokenize

func Tokenize(s string) []string

Tokenize is exported for the keyword arm of hybrid retrieval to share tokenization.

Types

type Embedder

type Embedder interface {
	// ID is the stable identifier recorded in manifests, e.g. "hash-v1:256" or
	// "ollama:nomic-embed-text".
	ID() string
	// Dim is the output dimensionality.
	Dim() int
	// Embed returns the vector for a single text.
	Embed(ctx context.Context, text string) (vector.Vector, error)
	// EmbedBatch embeds many texts (default impl loops; adapters may optimize).
	EmbedBatch(ctx context.Context, texts []string) ([]vector.Vector, error)
}

Embedder converts text to vectors. ID must uniquely identify the model+config so packs/memories can be pinned to it.

type HashEmbedder

type HashEmbedder struct {
	// contains filtered or unexported fields
}

HashEmbedder is a deterministic, dependency-free embedder using the feature- hashing ("hashing trick") technique over word unigrams and bigrams. It needs no model file and works fully offline — ideal as the always-available default and for reproducible tests.

Honest limitation: hashed bag-of-words captures lexical overlap, not deep semantics. It pairs well with the keyword arm of hybrid retrieval (which carries the literal CLI tokens). For stronger semantic recall, use the Ollama embedder (nomic-embed-text) or a future bge-small ONNX embedder behind this same iface.

func NewHashEmbedder

func NewHashEmbedder(dim int) *HashEmbedder

NewHashEmbedder builds a HashEmbedder with the given dimensionality (e.g. 256).

func (*HashEmbedder) Dim

func (h *HashEmbedder) Dim() int

Dim implements Embedder.

func (*HashEmbedder) Embed

func (h *HashEmbedder) Embed(_ context.Context, text string) (vector.Vector, error)

Embed implements Embedder.

func (*HashEmbedder) EmbedBatch

func (h *HashEmbedder) EmbedBatch(ctx context.Context, texts []string) ([]vector.Vector, error)

EmbedBatch implements Embedder.

func (*HashEmbedder) ID

func (h *HashEmbedder) ID() string

ID implements Embedder.

type OllamaEmbedder

type OllamaEmbedder struct {
	Endpoint string
	Model    string
	// contains filtered or unexported fields
}

OllamaEmbedder produces embeddings via a local Ollama server's /api/embeddings endpoint (e.g. the nomic-embed-text model). Higher semantic quality than the hash embedder, still fully local. Dim is discovered from the first response.

func NewOllamaEmbedder

func NewOllamaEmbedder(endpoint, model string) *OllamaEmbedder

NewOllamaEmbedder builds an Ollama-backed embedder. Empty endpoint defaults to loopback; empty model defaults to nomic-embed-text.

func (*OllamaEmbedder) Dim

func (o *OllamaEmbedder) Dim() int

Dim implements Embedder (0 until the first successful Embed).

func (*OllamaEmbedder) Embed

func (o *OllamaEmbedder) Embed(ctx context.Context, text string) (vector.Vector, error)

Embed implements Embedder.

func (*OllamaEmbedder) EmbedBatch

func (o *OllamaEmbedder) EmbedBatch(ctx context.Context, texts []string) ([]vector.Vector, error)

EmbedBatch implements Embedder.

func (*OllamaEmbedder) ID

func (o *OllamaEmbedder) ID() string

ID implements Embedder.

Jump to

Keyboard shortcuts

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