latent

package
v1.3.2 Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2026 License: Apache-2.0 Imports: 19 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package latent projects text into a representation space (JEPA or any embedding encoder) for semantic caching and latent guardrails.

Index

Constants

View Source
const DefaultHashDims = 512

DefaultHashDims is the width of the local fallback encoder.

Variables

View Source
var ErrInvalidVector = errors.New("latent: invalid vector")

ErrInvalidVector reports an encoder output that cannot be compared.

Functions

func Cosine

func Cosine(a, b []float32) float32

Cosine is the cosine similarity of arbitrary (not necessarily unit) vectors.

func Dot

func Dot(a, b []float32) float32

Dot is the inner product of equal-length vectors; for unit vectors it is the cosine similarity. Mismatched lengths score 0 rather than panic.

func MarshalVector

func MarshalVector(v []float32) []byte

MarshalVector encodes v as little-endian float32s.

func Normalize

func Normalize(v []float32) error

Normalize scales v to unit length in place so cosine similarity is a dot product.

func Tokenize

func Tokenize(text string) []string

Tokenize folds text the way an attacker would try to evade a matcher: compatibility forms, diacritics, zero-width runes and common homoglyphs all collapse to plain lowercase Latin before splitting on non-alphanumerics.

func UnmarshalVector

func UnmarshalVector(b []byte) ([]float32, error)

UnmarshalVector decodes the output of MarshalVector.

Types

type Embedding

type Embedding struct {
	Model  string
	Vector []float32
}

Embedding is one unit-length vector and the encoder that produced it. Vectors from different models live in different spaces and are never compared.

type Encoder

type Encoder interface {
	Encode(ctx context.Context, texts []string) ([]Embedding, error)
}

Encoder maps texts to embeddings, one per input, in input order.

type Fallback

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

Fallback serves from primary and switches to secondary when it fails. The embeddings carry their model, so vectors from the two never get compared.

func NewFallback

func NewFallback(primary, secondary Encoder) *Fallback

NewFallback chains primary (typically HTTP/JEPA) to secondary (typically local).

func (*Fallback) Encode

func (f *Fallback) Encode(ctx context.Context, texts []string) ([]Embedding, error)

type HTTPEncoder

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

HTTPEncoder calls an OpenAI-compatible /v1/embeddings endpoint, which is what a node-agent hosting a JEPA encoder (or any embedding server) serves.

func NewHTTPEncoder

func NewHTTPEncoder(url, model, apiKey string, timeout time.Duration, client *http.Client) *HTTPEncoder

NewHTTPEncoder targets url with the given model name; timeout bounds one Encode call so a slow encoder cannot stall the request path.

func (*HTTPEncoder) Encode

func (e *HTTPEncoder) Encode(ctx context.Context, texts []string) ([]Embedding, error)

Encode sends all texts in one request and returns unit-length vectors.

type HashEncoder

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

HashEncoder is a deterministic, in-process encoder built on signed feature hashing of words, word bigrams and character trigrams. It is lexical, not semantic: it keeps caching and guardrails alive when no JEPA encoder is up.

func NewHashEncoder

func NewHashEncoder(dims int) *HashEncoder

NewHashEncoder returns a local encoder producing dims-wide vectors.

func (*HashEncoder) Encode

func (e *HashEncoder) Encode(ctx context.Context, texts []string) ([]Embedding, error)

Encode never fails on content; it only honours context cancellation.

func (*HashEncoder) Model

func (e *HashEncoder) Model() string

Model names the vector space; it changes whenever dims or features change.

type Memo

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

Memo is an in-memory LRU in front of an encoder. One request is encoded by both the latent guardrail and the semantic cache; the memo makes the second lookup free. Returned vectors are shared and must be treated as read-only.

func NewMemo

func NewMemo(inner Encoder, size int) *Memo

NewMemo caches up to size embeddings; size <= 0 disables caching.

func (*Memo) Encode

func (m *Memo) Encode(ctx context.Context, texts []string) ([]Embedding, error)

func (*Memo) Len

func (m *Memo) Len() int

Len reports how many embeddings are cached.

Jump to

Keyboard shortcuts

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