embedding

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Jul 21, 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.

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 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