embedding

package
v0.1.3 Latest Latest
Warning

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

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

Documentation

Overview

Package embedding implements the §4.7 EmbeddingProvider SPI plus the four built-in providers (`openai`, `voyage`, `cohere`, `ollama`). Providers translate text into a fixed-dimensional float vector that the vector store indexes for hybrid retrieval.

Implementations are stateless HTTP clients over the provider's embeddings endpoint. None of them require an SDK; the wire format is JSON over `net/http`. Tests use `httptest.NewServer` to replay vendored response fixtures so the parsing path is exercised without external services.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrEmptyTexts is returned when Embed is called with no inputs.
	ErrEmptyTexts = errors.New("embedding: empty texts")
	// ErrUnreachable wraps a transient network or 5xx failure.
	// Callers retry with backoff or fall back to BM25-only.
	ErrUnreachable = errors.New("embedding: provider unreachable")
	// ErrAuth wraps a 401/403 from the provider.
	ErrAuth = errors.New("embedding: auth failed")
	// ErrQuota wraps a 429 / quota-exceeded response.
	ErrQuota = errors.New("embedding: quota exceeded")
)

Errors returned by Provider implementations.

View Source
var Default = NewRegistry()

Default is the process-global embedding-provider registry the bootstrap consults before its built-in switch. It is empty by default; the built-in providers (openai, voyage, cohere, ollama) remain the switch's concern so their per-provider env-var validation is unchanged. Deployers add custom providers via Default.Register.

Functions

This section is empty.

Types

type Cohere

type Cohere struct {
	APIKey  string
	Model_  string
	Dim     int
	BaseURL string
	Client  *http.Client
}

Cohere is the §4.7 Cohere embeddings provider. Defaults to `embed-v4` (1024 dim); set Model to override.

Wire format: POST <BaseURL>/embed with body `{"model":"<model>","texts":[...],"input_type":"search_document"}`.

func (Cohere) Dimensions

func (p Cohere) Dimensions() int

Dimensions returns the configured dimension; defaults vary by model (embed-v4 = 1024).

func (Cohere) Embed

func (p Cohere) Embed(ctx context.Context, texts []string) ([][]float32, error)

Embed sends a batch of texts and returns vectors in input order.

func (Cohere) ID

func (Cohere) ID() string

ID returns "cohere".

func (Cohere) Model

func (p Cohere) Model() string

Model returns the configured model, defaulting to embed-v4.

type Factory added in v0.1.3

type Factory func(settings map[string]string) (Provider, error)

Factory constructs a Provider from the resolved settings. Returning an error fails startup rather than silently disabling search.

type Ollama

type Ollama struct {
	BaseURL string
	Model_  string
	Dim     int
	Client  *http.Client
}

Ollama is the §4.7 self-hosted embeddings provider. Points at any Ollama endpoint; defaults to `http://localhost:11434` with model `nomic-embed-text` (768 dim). Recommended for offline / air-gapped deployments where no cloud provider is acceptable.

Wire format: POST <BaseURL>/api/embeddings with body `{"model":"<model>","prompt":"<text>"}`. Ollama processes one text per call; the provider issues N HTTP calls for a batch of N texts. This is ergonomically slower than batch APIs but keeps the wire format simple and matches what Ollama itself supports.

func (Ollama) Dimensions

func (p Ollama) Dimensions() int

Dimensions returns the configured dimension; defaults vary by model (nomic-embed-text = 768, mxbai-embed-large = 1024).

func (Ollama) Embed

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

Embed serially calls the Ollama endpoint once per text. Errors short-circuit the loop so the caller never sees a partial result.

func (Ollama) ID

func (Ollama) ID() string

ID returns "ollama".

func (Ollama) Model

func (p Ollama) Model() string

Model returns the configured model, defaulting to nomic-embed-text.

type OpenAI

type OpenAI struct {
	APIKey  string
	Model_  string
	Dim     int
	BaseURL string
	Org     string
	Client  *http.Client
}

OpenAI is the §4.7 OpenAI embeddings provider. Defaults to `text-embedding-3-small` (1536 dim); set Model to override.

Wire format: POST <BaseURL>/embeddings with body `{"model":"<model>","input":[<texts>]}`. Response carries one `{embedding: [...float...], index: int}` per input.

func (OpenAI) Dimensions

func (p OpenAI) Dimensions() int

Dimensions returns the configured dimension; defaults to 1536 for text-embedding-3-small.

func (OpenAI) Embed

func (p OpenAI) Embed(ctx context.Context, texts []string) ([][]float32, error)

Embed sends a batch of texts and returns the corresponding vectors in input order.

func (OpenAI) ID

func (OpenAI) ID() string

ID returns "openai".

func (OpenAI) Model

func (p OpenAI) Model() string

Model returns the configured model, defaulting to text-embedding-3-small.

type Provider

type Provider interface {
	// ID returns the provider identifier ("openai" | "voyage" |
	// "cohere" | "ollama" | ...).
	ID() string
	// Model returns the configured model name (e.g. "voyage-3").
	Model() string
	// Dimensions returns the output vector size. Providers report
	// this from configuration or by hard-coded model-default tables;
	// callers use it to validate vector-store schema compatibility.
	Dimensions() int
	// Embed converts a batch of texts into a batch of float vectors.
	// The returned slice has the same length and order as texts.
	// Errors map to the package-level sentinels via errors.Is.
	Embed(ctx context.Context, texts []string) ([][]float32, error)
}

Provider is the SPI implementations satisfy.

type Registry added in v0.1.3

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

Registry is the process-global registration seam for the §9.1 EmbeddingProvider SPI, distributed per §9.2 as an in-process Go module imported into a registry build. §9.1 states custom backends "register through this SPI as Go-module plugins (§9.2)"; this is the seam that makes the claim hold. A deployment imports a package whose init calls Default.Register, and the bootstrap selects the provider by the PODIUM_EMBEDDING_PROVIDER id, consulting this registry before the built-in switch.

Settings is a wire-serializable map of resolved configuration values so a future out-of-process provider (§9.3) receives the same inputs.

spec: §9.1 (EmbeddingProvider), §9.2 (Go-module plugins).

func NewRegistry added in v0.1.3

func NewRegistry() *Registry

NewRegistry returns an empty Registry.

func (*Registry) IDs added in v0.1.3

func (r *Registry) IDs() []string

IDs returns every registered provider id, sorted.

func (*Registry) New added in v0.1.3

func (r *Registry) New(id string, settings map[string]string) (Provider, bool, error)

New constructs the provider registered under id. Returns (nil, false, nil) when no provider is registered so the caller can fall through to the built-in switch.

func (*Registry) Register added in v0.1.3

func (r *Registry) Register(id string, f Factory) error

Register adds a Factory under id. Returns an error when id is empty or already registered.

type Voyage

type Voyage struct {
	APIKey  string
	Model_  string
	Dim     int
	BaseURL string
	Client  *http.Client
}

Voyage is the §4.7 Voyage AI embeddings provider. Defaults to `voyage-3` (1024 dim); set Model to override.

Wire format: POST <BaseURL>/embeddings with body `{"model":"<model>","input":[<texts>],"input_type":"document"}`.

func (Voyage) Dimensions

func (p Voyage) Dimensions() int

Dimensions returns the configured dimension; defaults vary by model (voyage-3 = 1024).

func (Voyage) Embed

func (p Voyage) Embed(ctx context.Context, texts []string) ([][]float32, error)

Embed sends a batch of texts and returns vectors in input order.

func (Voyage) ID

func (Voyage) ID() string

ID returns "voyage".

func (Voyage) Model

func (p Voyage) Model() string

Model returns the configured model, defaulting to voyage-3.

Jump to

Keyboard shortcuts

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