embed

package
v0.0.1-alpha.2 Latest Latest
Warning

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

Go to latest
Published: Jul 25, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package embed defines the Embedder port (ADR-0004) and a local adapter that speaks the OpenAI-compatible /embeddings endpoint. Embeddings stay local in the sense that the store persists them on host; the endpoint may be local (Ollama, vLLM) or remote (OpenAI) — configured per provider.

Index

Constants

View Source
const (
	DefaultBaseURL = "https://api.openai.com/v1"
	// DefaultTimeout bounds a single Embed call when no caller-supplied
	// http.Client is set.
	DefaultTimeout = 30 * time.Second
)

Variables

View Source
var ErrDimensionDrift = errors.New("embed: dimension drift")

ErrDimensionDrift is returned by Local.Embed when the endpoint returns vectors whose dimension disagrees with the dimension pinned on the first successful Embed. Callers use errors.Is to distinguish it from transport failures — the fallback path (FallbackTo) does NOT trigger on dim drift, because the fallback endpoint almost certainly uses a different model and a different vector space.

Functions

This section is empty.

Types

type Embedder

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

Embedder turns text into fixed-dimension float32 vectors. Dim() returns the pinned dimension (0 before the first successful Embed).

type Local

type Local struct {
	BaseURL string
	APIKey  string
	Model   string
	// Timeout bounds a single Embed call when HTTP is nil. Zero means
	// DefaultTimeout. Ignored when HTTP is non-nil (the caller's client
	// wins, including its timeout).
	Timeout time.Duration
	HTTP    *http.Client
	// contains filtered or unexported fields
}

Local is the OpenAI-compatible /embeddings adapter. It pins the vector dimension on the first successful Embed and rejects later mismatches.

func (*Local) Dim

func (l *Local) Dim() int

Dim returns the pinned embedding dimension (0 before the first Embed).

func (*Local) Embed

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

Embed posts texts to {BaseURL}/embeddings and returns one vector per text.

func (*Local) FallbackTo

func (l *Local) FallbackTo(fb Embedder) Embedder

FallbackTo wraps l with a fallback Embedder that is consulted only on transport-class errors (network failure, 5xx, 429). It returns an Embedder; the wrapper itself is a small struct holding primary and fallback. Dim() reports the primary's pinned dimension.

FallbackTo does NOT trigger on ErrDimensionDrift (the fallback endpoint almost certainly uses a different model with a different vector space) or on 4xx other than 429 (those are caller errors, not transport errors).

Calling FallbackTo on a nil receiver is a programming error and returns nil.

Jump to

Keyboard shortcuts

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