embed

package
v0.2.4 Latest Latest
Warning

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

Go to latest
Published: Jun 12, 2026 License: AGPL-3.0 Imports: 15 Imported by: 0

Documentation

Overview

Package embed turns text into dense vectors via an external, OpenAI-compatible embeddings endpoint; memini never embeds locally.

Index

Constants

This section is empty.

Variables

View Source
var ErrDisabled = errors.New("embeddings endpoint not configured (set MEMINI_EMBED_BASE_URL)")

ErrDisabled is returned when memory operations need embeddings but no embeddings endpoint is configured.

Functions

func EmbedOne

func EmbedOne(ctx context.Context, e Embedder, text string) ([]float32, error)

EmbedOne embeds a single string.

Types

type Batched

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

Batched splits an Embed call into sub-requests bounded by item count and character budget to keep payloads under endpoint limits, truncating over-long texts.

func NewBatched

func NewBatched(inner Embedder, maxItems, maxChars, maxItemChars int) *Batched

NewBatched wraps inner. maxItems caps items per request; maxChars caps total characters per request; maxItemChars truncates any single text (0 disables).

func (*Batched) Dims

func (b *Batched) Dims() int

Dims returns the wrapped embedder's dimensionality.

func (*Batched) Embed

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

Embed splits texts into bounded sub-batches, preserving order.

type Cached

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

Cached wraps an Embedder with a content-hash LRU cache so identical text is embedded only once. Safe for concurrent use.

func NewCached

func NewCached(e Embedder, size int) (*Cached, error)

NewCached wraps e with an LRU of the given size (number of distinct texts).

func (*Cached) Dims

func (c *Cached) Dims() int

Dims returns the wrapped embedder's dimensionality.

func (*Cached) Embed

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

Embed returns vectors for texts, serving cache hits and embedding only misses.

type Disabled

type Disabled struct{ D int }

Disabled is an Embedder used when no endpoint is configured: the server boots but remember/recall fail with ErrDisabled.

func (Disabled) Dims

func (d Disabled) Dims() int

Dims returns the configured dimensionality.

func (Disabled) Embed

func (d Disabled) Embed(context.Context, []string) ([][]float32, error)

Embed always fails.

type DiskCache

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

DiskCache wraps an Embedder with a persistent content-hashed cache, so identical text is embedded once across process runs. Safe for concurrent use.

func NewDiskCache

func NewDiskCache(inner Embedder, path string) (*DiskCache, error)

NewDiskCache wraps inner, loading any existing cache at path. A corrupt cache file is discarded (and logged) rather than half-loaded.

func (*DiskCache) Dims

func (d *DiskCache) Dims() int

Dims returns the wrapped embedder's dimensionality.

func (*DiskCache) Embed

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

Embed serves cache hits and embeds only misses, recording new vectors.

func (*DiskCache) Len

func (d *DiskCache) Len() int

Len reports how many vectors are cached.

func (*DiskCache) Save

func (d *DiskCache) Save() error

Save persists the cache to disk if it changed.

type Embedder

type Embedder interface {
	// Embed returns one vector per input text, in the same order.
	Embed(ctx context.Context, texts []string) ([][]float32, error)
	// Dims is the dimensionality of returned vectors.
	Dims() int
}

Embedder converts text into fixed-dimension vectors.

func Instrument

func Instrument(e Embedder, backend string, m Metrics) Embedder

Instrument returns e wrapped with metrics. backend labels the outer implementation (e.g. "cached", "diskcache", "batched", "disabled") so dashboards can split the latency of real network work from cache hits.

type Metrics

type Metrics interface {
	// Observe records one Embed call: backend labels the layer that produced
	// the result ("openai", "cached", "diskcache", "batched", "disabled"),
	// items is the number of input texts, tokens is the API-reported token
	// usage (0 for cache hits and the disabled backend), and d is the wall
	// duration.
	Observe(backend string, items int, tokens int, d time.Duration)
	// Error records a failed Embed call on the given backend.
	Error(backend string)
}

Metrics receives embedder events for observability. Methods must be safe for concurrent use; a nil Metrics is replaced by a no-op.

type OpenAIClient

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

OpenAIClient calls an OpenAI-compatible /embeddings endpoint.

func NewOpenAI

func NewOpenAI(cfg OpenAIConfig) (*OpenAIClient, error)

NewOpenAI builds an embeddings client. BaseURL and Model are required.

func (*OpenAIClient) Dims

func (c *OpenAIClient) Dims() int

Dims returns the configured embedding dimensionality.

func (*OpenAIClient) Embed

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

Embed returns one vector per text, preserving input order.

func (*OpenAIClient) SetMetrics

func (c *OpenAIClient) SetMetrics(m Metrics)

SetMetrics installs an observability sink on this client. Reports real token usage from the API's Usage block. A nil m disables instrumentation.

type OpenAIConfig

type OpenAIConfig struct {
	BaseURL string // e.g. http://localhost:8081/v1
	APIKey  string // optional bearer token
	Model   string
	Dims    int
	// HTTPClient is optional; the SDK default is used when nil.
	HTTPClient *http.Client
}

OpenAIConfig configures the OpenAI-compatible embeddings client.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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