embed

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Jul 26, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package embed provides the local-embedding arm of the hybrid funnel: an Ollama-backed Embedder plus the f32 vector codec and similarity helper. Hot-path package: stdlib only. The Embedder interface is structurally identical to gopheragent's tools.Embedder so a future upstream Ollama embedder can replace ollama.go without touching consumers (plan §upstream).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DecodeVec

func DecodeVec(b []byte) []float32

DecodeVec deserializes an EncodeVec blob. Returns nil for malformed input (length not a multiple of 4) — a stale/corrupt row degrades to "no vector", never an error on the hot path.

func Dot

func Dot(a, b []float32) float64

Dot returns the dot product — cosine similarity for Normalized vectors. Mismatched or empty vectors score 0 (treated as "no signal").

func EncodeVec

func EncodeVec(v []float32) []byte

EncodeVec serializes a vector as little-endian float32 bytes — the `embeddings.vec` BLOB format.

func Normalize

func Normalize(v []float32) []float32

Normalize scales v to unit length in place and returns it, so Dot below is cosine similarity. A zero vector is returned unchanged.

Types

type Embedder

type Embedder interface {
	Embed(ctx context.Context, texts []string) ([][]float32, error)
}

Embedder produces fixed-size vector representations of text.

type Ollama

type Ollama struct {
	Endpoint string // e.g. http://localhost:11434
	Model    string // e.g. nomic-embed-text
	// KeepAlive is passed through to Ollama as the model's residency window,
	// refreshed on every call. Ollama unloads an idle model after ~5min by
	// default, and the cold reload costs seconds — far past the hot path's
	// 100ms embed budget, so the retrieve breaker trips and the whole cosine
	// arm drops out for its cooldown. Keeping the model resident is what makes
	// hybrid retrieval actually available between prompts. "" omits the field
	// (server default).
	KeepAlive string
	Client    *http.Client
}

Ollama calls the local /api/embed endpoint. Vectors come back Normalized so Dot is cosine. The zero timeout on the shared client is deliberate: callers bound each call with ctx (100ms on the hot path, seconds for indexing).

func NewOllama

func NewOllama(endpoint, model, keepAlive string) *Ollama

NewOllama builds an embedder for endpoint/model. keepAlive is any duration string Ollama accepts ("30m", "-1" for never unload); "" leaves it to the server.

func (*Ollama) Embed

func (o *Ollama) Embed(ctx context.Context, texts []string) ([][]float32, error)

Embed embeds texts in one batched request. Every returned vector is unit length. Order matches input.

Jump to

Keyboard shortcuts

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