embedding

package
v0.12.0 Latest Latest
Warning

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

Go to latest
Published: Aug 2, 2026 License: MIT Imports: 8 Imported by: 0

README

embedding

model := embedding.ModelRef{Provider: "local", Name: "demo", Dim: 3}
key := embedding.CacheKey(model, "hello")
err := embedding.ValidateDimensions([]embedding.Vector{{1, 2, 3}}, model.Dim)

request := embedding.Request{Model: model, Inputs: []string{"hello"}}
result := embedding.Result{Model: model, Vectors: []embedding.Vector{{1, 2, 3}}}
err = embedding.ValidateResult(request, result)

embedding is the provider-neutral contract for embedding requests and dense vectors.

The package does not call models, choose providers, or own vector-space policy. Callers pass an Embedder implementation and keep model identity, dimensions, fallback behavior, and cache invalidation explicit.

A successful Embed call returns exactly one positive-dimension, finite vector per input in the same order. ValidateResult checks that cardinality and shape, including agreement with declared request and result dimensions; zero-magnitude vectors remain valid. Implementations should run the reusable embedding/embeddingtest contract suite.

ModelRef.Identity uses versioned byte-length framing. The modelref:v1 format prevents delimiter collisions, including with Unicode fields. Upgrading to this version intentionally invalidates cache keys produced by the legacy colon-joined identity; callers should expect cold misses rather than dual-read old keys.

Cache decorator

embedding/cache wraps any Embedder and caches one vector per exact input. You inject the Store and a nonblank identity:

store := embeddingcacheinmem.New()
cached, err := embeddingcache.New(base, store, embeddingcache.Config{
	Identity: "openai:text-embedding-3-small:1536:input-v1",
})

The decorator performs one lookup per unique input, embeds unique misses in one batch, restores original order, and clones vectors across Store, embedder, and caller ownership boundaries. It validates hits, misses, and the assembled result before writing. Store and embedder errors fail closed.

The identity is caller-owned invalidation policy. Change it whenever provider, model, revision, dimensions, preprocessing, or output semantics change. The decorator derives opaque keys from that identity, the requested ModelRef, and the exact input bytes. The decorator itself provides no persistence, TTL, eviction, singleflight, or background work.

For process-local caching, embedding/cache/inmem provides a concurrency-safe Store with an immediately usable zero value. It clones vectors on reads and writes and intentionally provides no persistence, capacity, TTL, eviction, statistics, or lifecycle methods.

Documentation

Overview

Package embedding defines provider-neutral embedding model, request, result, vector, cache-key, and dimension-validation contracts. Provider clients and model policy stay in adapters or applications.

Package embedding defines provider-neutral embedding contracts.

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrInvalidResult = errors.New("embedding: invalid result")

ErrInvalidResult indicates that an embedder returned a malformed result.

Functions

func CacheKey

func CacheKey(model ModelRef, input string) string

CacheKey returns a stable cache identity for one model/input pair.

func ValidateDimensions

func ValidateDimensions(vectors []Vector, dims int) error

ValidateDimensions checks that every vector has dims values.

Example
package main

import (
	"fmt"

	"github.com/dotcommander/reliquary/embedding"
)

func main() {
	model := embedding.ModelRef{Provider: "local", Name: "demo", Dim: 3}
	err := embedding.ValidateDimensions([]embedding.Vector{{1, 2, 3}}, model.Dim)
	fmt.Println(err == nil)
}
Output:
true

func ValidateResult added in v0.9.0

func ValidateResult(request Request, result Result) error

ValidateResult verifies the shape and finite values required of a successful embedding result. A zero ModelRef.Dim means unspecified; when present, request and result dimensions must agree with each other and every vector.

Types

type Embedder

type Embedder interface {
	Embed(ctx context.Context, request Request) (Result, error)
}

Embedder embeds text into vectors. Successful results contain exactly one vector per input, in the same order as the request inputs.

type ModelRef

type ModelRef struct {
	Provider string
	Name     string
	Version  string
	Revision string
	Dim      int
}

ModelRef identifies an embedding model and vector space.

func (ModelRef) Identity

func (m ModelRef) Identity() string

Identity returns the versioned, byte-length-framed model identity used for cache keys. Format changes intentionally invalidate prior cache entries.

type Request

type Request struct {
	Model  ModelRef
	Inputs []string
}

Request is a batch embedding request.

type Result

type Result struct {
	Model   ModelRef
	Vectors []Vector
}

Result is a batch embedding result. A successful Embed call returns exactly one vector per input, in the same order as the request inputs.

type Vector

type Vector []float32

Vector is a dense embedding vector.

Directories

Path Synopsis
Package cache decorates an embedding.Embedder with an explicit, caller-owned Store.
Package cache decorates an embedding.Embedder with an explicit, caller-owned Store.
inmem
Package inmem provides a concurrency-safe, process-local implementation of cache.Store.
Package inmem provides a concurrency-safe, process-local implementation of cache.Store.
Package embeddingtest provides a reusable contract suite for Embedder implementations.
Package embeddingtest provides a reusable contract suite for Embedder implementations.

Jump to

Keyboard shortcuts

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