vector

package
v2.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 18, 2026 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

View Source
const EnvPrefix = "VLSH"

Variables

View Source
var (
	ErrEmptyVector    = errors.New("empty vector")
	ErrWrongDimension = errors.New("vector dimension does not match config")
)

Functions

func EstimateCosine

func EstimateCosine(sig1, sig2 []uint64) float64

EstimateCosine estimates cosine similarity from two binary (hyperplane) signatures. P(same sign) = 1 - angle/pi → cosine = cos(pi * (1 - matchFraction)).

func ExactCosine

func ExactCosine(v1, v2 []float64) float64

ExactCosine computes the exact cosine similarity between two vectors.

Types

type Config

type Config struct {
	lsh.Config
	VectorDimensions int     `env:"_VECTOR_DIMENSIONS" envDefault:"20"`
	CosineThreshold  float64 `env:"_COS_THRESHOLD" envDefault:"0.5"`
}

func GetConfigFromEnv

func GetConfigFromEnv() (*Config, error)

func (*Config) CalculateApproximateThreshold

func (c *Config) CalculateApproximateThreshold() float64

CalculateApproximateThreshold returns the cosine similarity where the LSH collision probability transitions sharply for the given Bands/Rows config.

P(one band) = (1 - arccos(s)/pi)^R
P(any band) = 1 - (1 - P(one band))^B

We find s where P(any band) ~ 0.5 using binary search.

func (*Config) HashVersion

func (c *Config) HashVersion(group string) (string, error)

HashVersion computes a deterministic prefix from group + all config fields.

func (*Config) Validate

func (c *Config) Validate() error

Validate checks that the LSH approximate threshold is strictly below CosineThreshold. If violated, recall at the application threshold drops below 50% — the system misses more matches than it finds. This catches dangerous B/R configurations early.

type Hasher

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

func NewHasher

func NewHasher(bands, rows, dimensions int, seed int64) *Hasher

func (*Hasher) ComputeSignature

func (h *Hasher) ComputeSignature(vector []float64, sig []uint64)

ComputeSignature produces a binary signature from a feature vector. Each bit = sign of dot product with a random hyperplane.

type Record

type Record struct {
	ID        string
	Vector    []float64
	GroupID   string
	Signature []uint64
}

type Service

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

func NewService

func NewService(repo repositories.Storage, config *Config) (*Service, error)

func (*Service) GetNewID

func (s *Service) GetNewID(vector []float64) string

GetNewID returns a deterministic ID derived from the vector content (SHA256, base64url).

func (*Service) Upsert

func (s *Service) Upsert(ctx context.Context, group string, vector []float64) (string, error)

Upsert assigns a behavioural ID to the given vector. Returns the existing representative's ID if cosine >= threshold, or a new ID if novel.

func (*Service) WithMetrics

func (s *Service) WithMetrics(m *lsh.Instruments)

WithMetrics injects pre-built OpenTelemetry instruments. If not called, metrics are silently skipped.

Jump to

Keyboard shortcuts

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