embed

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Aug 28, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package embed turns text into vectors via a single client speaking the OpenAI embeddings wire format — not per-provider adapters. That format is a de-facto standard: OpenAI's own API, and any "OpenAI-compatible" gateway in front of another provider (this package defaults to EdenAI's EU gateway), speak it identically, so switching provider is a config change rather than a new adapter.

Index

Constants

View Source
const (
	// DefaultBaseURL is EdenAI's EU-region gateway — keeps embedding
	// requests in-region for data residency.
	DefaultBaseURL = "https://api.eu.edenai.run/v3"
	// DefaultModel proxies gemini-embedding-001 without needing a direct
	// GCP setup.
	DefaultModel = "google/gemini-embedding-001"
	// DefaultDimension is the pgvector width this package's default
	// configuration targets.
	DefaultDimension = 1536
)

Variables

View Source
var ErrRateLimited = errors.New("embedding service rate limited")

ErrRateLimited marks a 429. Retryable, but worth a longer, deliberate backoff than a transient failure — see ErrUnavailable.

View Source
var ErrUnavailable = errors.New("embedding service unavailable")

ErrUnavailable marks a transport/deployment failure — network error or a 5xx from the embedding service itself. Retryable: the request, not the input, is at fault.

Functions

func Fingerprint

func Fingerprint(e Embedder) string

Fingerprint is the canonical identity of the space an embedder embeds into. Two embedders with the same fingerprint produce comparable vectors; different fingerprints do not, even if they happen to share a dimension.

Types

type Client

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

Client embeds text via an OpenAI-wire-compatible /embeddings endpoint: request {model, input, dimensions, encoding_format}, response {data: [{index, embedding}]}.

Not every backend honors the `dimensions` field reliably — EdenAI, for gemini-embedding-001, always returns the native 3072-wide vector regardless of what was requested. When the response is wider than the configured Dimension, Client Matryoshka-truncates and L2-renormalizes to fit — valid for MRL-trained embeddings like Gemini's. A narrower-than- requested response is a hard error: there's no safe way to pad it back to more information.

func NewOpenAICompatible

func NewOpenAICompatible(cfg OpenAICompatibleConfig) (*Client, error)

NewOpenAICompatible builds a Client. Errors if the API key is missing.

func (*Client) Dimension

func (c *Client) Dimension() int

func (*Client) Embed

func (c *Client) Embed(ctx context.Context, texts []string) ([]Vector, error)

Embed embeds a batch of texts. The wire format has no query/document task-type distinction (unlike some native provider SDKs), so this is symmetric: the same call embeds both queries and documents.

func (*Client) Model

func (c *Client) Model() string

func (*Client) Provider

func (c *Client) Provider() string

type Embedder

type Embedder interface {
	// Embed embeds a batch of texts, preserving order.
	Embed(ctx context.Context, texts []string) ([]Vector, error)
	// Provider identifies the backend, e.g. "edenai".
	Provider() string
	// Model is the active embedding model id.
	Model() string
	// Dimension is the embedding width stored in pgvector.
	Dimension() int
}

Embedder turns text into vectors.

type OpenAICompatibleConfig

type OpenAICompatibleConfig struct {
	APIKey  string
	BaseURL string
	Model   string
	// Provider is a free-form label used only in Fingerprint (e.g. "edenai",
	// "openai") — not sent on the wire.
	Provider string
	// Dimension is the target pgvector width. Defaults to DefaultDimension.
	Dimension int
	// HTTPClient overrides the default client (2 minute timeout).
	HTTPClient *http.Client
}

OpenAICompatibleConfig configures Client. BaseURL/Model default to EdenAI's EU gateway and gemini-embedding-001 when unset.

type Space added in v0.3.0

type Space struct {
	// Provider identifies the backend that produced the vectors, e.g.
	// "xberg". It is part of the identity: the same model served by two
	// providers is not reliably the same space.
	Provider string
	// Model is the embedding model id, e.g. "bge-base-en-v1.5".
	Model string
	// Dimension is the vector width, and must match the width the schema was
	// generated for.
	Dimension int
}

Space identifies an embedding space on its own, for vectors that were produced somewhere ragit cannot call — an extraction service that embeds as a side effect of extracting, say, or a batch job that ran last week.

It exists so the identity of those vectors is *declared* rather than formatted by hand. Retrieval filters on the fingerprint, so a corpus written under a string that disagrees by one character with what the query embedder reports returns nothing at all — indistinguishable from an empty corpus, and silent. A struct with three named fields cannot drift that way.

func SpaceOf added in v0.3.0

func SpaceOf(e Embedder) Space

SpaceOf is the space an Embedder embeds into.

func (Space) Fingerprint added in v0.3.0

func (s Space) Fingerprint() string

Fingerprint is the canonical identity of the space: provider|model|dimension.

func (Space) Validate added in v0.3.0

func (s Space) Validate() error

Validate rejects a space that could not identify anything.

A fingerprint with an empty field still compares equal to itself, so an unset Space would work perfectly until a second one showed up — which is exactly when it would matter.

type Vector

type Vector []float32

Vector is one embedding.

Jump to

Keyboard shortcuts

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