embed

package
v0.18.0-alpha.1 Latest Latest
Warning

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

Go to latest
Published: Sep 12, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package embed is the public embedding boundary, the twin of pkg/llm for vectors (20260902-114838-d-tac-cov): one two-method interface over pure request and result types, sharing Identity, Usage, and the call record with the chat side, plus the same three decorators. Provider adapters and the configuration that selects them stay at each host's composition site.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Embedder

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

Embedder is the single port. Embed returns exactly len(req.Texts) vectors in order and fills Result.Identity on success; a failed call may attribute itself through *llm.Error. Fingerprint identifies the vector space the embedder produces and must be stable across calls: two embedders whose document vectors are comparable share it, two whose vectors are not, do not.

func Batched

func Batched(e Embedder, size int) Embedder

Batched decorates an Embedder so one request is served as calls of at most size texts each, in order, with the vectors concatenated and the usage summed. Batch size is a composition value like a deadline or a rate, so an adapter can stay a transport that sends exactly one request per Embed: decorators inside Batched then apply per round-trip, decorators outside apply per call. Identity is taken from the first call; the first failure aborts the rest and is returned as is.

func Bounded

func Bounded(e Embedder, timeout time.Duration) Embedder

Bounded decorates an Embedder with a per-call deadline over the whole Embed, however many transport round-trips the adapter makes for it.

func Observed

func Observed(e Embedder, sink llm.StatsSink) Embedder

Observed decorates an Embedder so every call hands one llm.CallStat to sink, with Items set to the batch size and Usage taken from the result. Failure attribution and the nil-sink behaviour match llm.Observed.

func RateLimited

func RateLimited(e Embedder, rps float64) Embedder

RateLimited decorates an Embedder with a token-bucket limiter of rps calls per second. The burst is one second's worth of calls, at least one.

type EmbedderFunc

type EmbedderFunc struct {
	Space string
	Run   func(context.Context, Request) (Result, error)
}

EmbedderFunc adapts a function to Embedder for test doubles and stubs; Space is what Fingerprint returns.

func (EmbedderFunc) Embed

func (f EmbedderFunc) Embed(ctx context.Context, req Request) (Result, error)

func (EmbedderFunc) Fingerprint

func (f EmbedderFunc) Fingerprint() string

type Purpose

type Purpose string

Purpose names which side of retrieval a batch serves. Instruction-tuned encoders want different prefixes on documents and queries, so an adapter selects its template by purpose; the value doubles as the observability dimension, the same as a chat Purpose.

const (
	PurposeDocument Purpose = "embed-document"
	PurposeQuery    Purpose = "embed-query"
)

type Request

type Request struct {
	Purpose Purpose
	Texts   []string
}

Request is one batch of texts to embed for one purpose.

type Result

type Result struct {
	Vectors  [][]float32
	Identity llm.Identity
	Usage    llm.Usage
}

Result carries one vector per input text, in input order, and reports what served the call.

Jump to

Keyboard shortcuts

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