Documentation
¶
Overview ¶
Package vectors provides small vector math helpers for embedding and retrieval systems.
Package vectors provides zero-dependency primitive vector math for embedding and retrieval systems.
Similarity ¶
Cosine similarity and distance are available in float32 and float64 variants (Cosine32, Cosine64, CosineDistance32, CosineDistance64). Use float32 for embedding pipelines — it halves memory and is faster on modern hardware. Use float64 only when numerical precision is the governing constraint. Dot product helpers (Dot32, Dot64, DotFromBlob, TopKFromBlob) are provided for callers that normalize up front and want raw dot as a proxy for cosine. Euclidean64 and Jaccard (JaccardWords) cover additional similarity regimes.
Normalization ¶
Normalize32/Normalize64 L2-normalize a vector in place and return the original magnitude. NormalizeTo32/NormalizeTo64 return a new normalized slice. These normalization helpers use scaled accumulation so extreme finite magnitudes remain normalizable. NormSquared32/NormSquared64 and IsUnit32/IsUnit64 are low-level helpers for callers that need to pre-check or validate unit vectors.
Binary quantization ¶
Quantize encodes a float32 embedding as a BinaryVector ([]uint64 packed bits) using per-dimension thresholds; QuantizeInto is the allocation-free variant. HammingDistance counts differing bits between two BinaryVectors in O(dims/64) time. ComputeMedians/ComputeMediansChecked derive thresholds from a corpus of embeddings. Together these support compact storage and fast approximate pre-filtering: Hamming distance on quantized vectors is ~10x faster than float32 cosine, making it a practical first-pass filter before exact re-ranking.
Pooling and aggregation ¶
MeanPool32 averages a slice of vectors into one. WeightedMeanPool32 weights the average by a parallel float64 slice. AverageSimilarity summarizes pairwise similarity across a set of embeddings.
In-memory indexes ¶
ExactIndex is a flat brute-force cosine index suitable for corpora up to ~100 K chunks. SearchKeys reranks selected (group, chunk) candidates without importing caller storage policy. BinaryIndex is a Hamming-distance pre-filter index backed by packed bit blobs, intended for larger corpora or memory-constrained environments. Checked constructors return IndexBuildReport values so callers can log skipped rows without changing compatibility paths. IndexManifest records dimensions, embedding identity, median thresholds, and chunking identity so persisted indexes can be rejected when stale.
Auxiliary utilities ¶
K-means clustering (KMeans, KMeans64, FindOptimalK, FindOptimalK64, SilhouetteScore, AverageSilhouetteScore64), sliding-window and semantic boundary detection (SlidingWindowSimilarity, FindSemanticBoundaries, FindElbowCurvature), Reciprocal Rank Fusion (RRF), score normalization (MinMaxNormalize, CosineToUnit, Clamp01), statistical helpers (MeanStddev, GaussianSmooth, Gradient), and binary blob encoding/decoding (EncodeFloat32Vec, DecodeFloat32Vec, EncodeFloat64Vec, DecodeFloat64Vec) are also provided.
Dependencies ¶
This package's production code uses the standard library only. The enclosing reliquary module has dependencies for sibling packages such as chunking.
Index ¶
- Constants
- func AdaptiveThreshold(similarities []float32) float32
- func AverageSilhouetteScore64(points [][]float64, assignments []int) float64
- func AverageSimilarity(embeddings [][]float32) float32
- func BinaryWords(dim int) int
- func Clamp01(x float64) float64
- func ClusterSilhouetteScores64(points [][]float64, assignments []int) map[int]float64
- func ComputeCentroid64(points [][]float64) []float64
- func ComputeClusterCentroids64(points [][]float64, assignments []int, k int) [][]float64
- func ComputeMedians(vecs [][]float32) []float32
- func ComputeMediansChecked(vecs [][]float32) ([]float32, error)
- func Cosine32(a, b []float32) float32
- func Cosine64(a, b []float64) float64
- func CosineDistance32(a, b []float32) float32
- func CosineDistance64(a, b []float64) float64
- func CosineToUnit(score float64) float64
- func DecodeFloat32Batch(blobs [][]byte) ([][]float32, error)
- func DecodeFloat32Vec(blob []byte) []float32
- func DecodeFloat64Vec(data []byte) []float64
- func Dot32(a, b []float32) float64
- func Dot64(a, b []float64) float64
- func DotFromBlob(query []float32, blob []byte) float32
- func EncodeFloat32Vec(v []float32) []byte
- func EncodeFloat64Vec(v []float64) []byte
- func Euclidean64(a, b []float64) float64
- func FindElbowCurvature(scores []float32, minKeep int) int
- func FindOptimalK(points [][]float32, minK, maxK int, rng *rand.Rand) (int, float64)
- func FindOptimalK64(points [][]float64, minK, maxK int) (bestK int, bestScore float64, assignments []int, centroids [][]float64, ...)
- func FindSemanticBoundaries(similarities []float32, threshold float32) []int
- func GaussianSmooth(scores []float32, sigma float64) []float32
- func Gradient(scores []float32) []float32
- func HammingDistance(a, b BinaryVector) int
- func HashANNProfile(profile ANNProfile) hash.Digest
- func HashFloat32Slice(values []float32) string
- func HashIndexProfile(fields map[string]string) string
- func HashIndexProfileIdentity(fields map[string]string) supporthash.Digest
- func IsUnit32(v []float32, tolerance float64) bool
- func IsUnit64(v []float64, tolerance float64) bool
- func JaccardWords(text1, text2 string) float64
- func MeanPool32(vecs [][]float32) []float32
- func MeanStddev(vals []float64) (mean, stddev float64)
- func MinMaxNormalize(vals []float64) []float64
- func NearDuplicateGroups(vecs [][]float32, cosineThreshold float32) [][]int
- func NearDuplicatePairs(vecs [][]float32, cosineThreshold float32) [][2]int
- func NewBinaryIndexChecked(blobs [][]byte, groups []string, chunkIndices []int, dims int) (*BinaryIndex, IndexBuildReport)
- func NewExactIndexChecked(dims int, chunks []IndexChunk, arena []byte) (*ExactIndex, IndexBuildReport)
- func NormSquared32(v []float32) float64
- func NormSquared64(v []float64) float64
- func Normalize32(v []float32) float32
- func Normalize64(v []float64) float64
- func NormalizeTo32(v []float32) []float32
- func NormalizeTo64(v []float64) []float64
- func QuantizeInto(dst BinaryVector, vec, thresholds []float32) error
- func SilhouetteCoefficient64(pointIdx int, points [][]float64, assignments []int) float64
- func SilhouetteScore(points [][]float32, assignments []int, k int) float64
- func SlidingWindowSimilarity(embeddings [][]float32, windowSize int) []float32
- func SmoothSimilarities(similarities []float32, windowSize int) []float32
- func TopKMaxIndices(scores []float32, k int) []int
- func TopKMinIndices(scores []float32, k int) []int
- func WeightedMeanPool32(vecs [][]float32, weights []float64) ([]float32, error)
- type ANNProfile
- type ANNProfileResult
- type BinaryIndex
- func (idx *BinaryIndex) Clear()
- func (idx *BinaryIndex) Len() int
- func (idx *BinaryIndex) Manifest(identity IndexManifestIdentity) IndexManifest
- func (idx *BinaryIndex) SearchCandidates(queryVec []float32) ([]HammingCandidate, bool)
- func (idx *BinaryIndex) SearchCandidatesLimit(queryVec []float32, limit int) ([]HammingCandidate, bool)
- type BinaryIndexEntry
- type BinaryVector
- type ExactIndex
- func (idx *ExactIndex) Clear()
- func (idx *ExactIndex) Len() int
- func (idx *ExactIndex) Manifest(identity IndexManifestIdentity) IndexManifest
- func (idx *ExactIndex) Search(queryVec []float32, limit int, minSimilarity float64) ([]SearchResult, bool)
- func (idx *ExactIndex) SearchFiltered(queryVec []float32, limit int, minSimilarity float64, groups []string) ([]SearchResult, bool)
- func (idx *ExactIndex) SearchGroupsByMaxPool(queryVec []float32, limit int) ([]SearchResult, bool)
- func (idx *ExactIndex) SearchKeys(queryVec []float32, limit int, minSimilarity float64, keys []IndexKey) ([]SearchResult, bool)
- type HammingCandidate
- type IndexBuildReport
- type IndexChunk
- type IndexKey
- type IndexKind
- type IndexManifest
- type IndexManifestIdentity
- type KMeans64Config
- type KMeans64Result
- type KMeansResult
- type LatencySummary
- type MemoryEstimate
- type Scored
- type ScoredIndex
- type SearchResult
Examples ¶
Constants ¶
const IndexManifestVersion = 1
IndexManifestVersion is the current IndexManifest schema version.
Variables ¶
This section is empty.
Functions ¶
func AdaptiveThreshold ¶
AdaptiveThreshold calculates an adaptive threshold based on similarity distribution.
func AverageSilhouetteScore64 ¶
AverageSilhouetteScore64 computes the average silhouette score.
func AverageSimilarity ¶
AverageSimilarity calculates the average cosine similarity between multiple vector pairs. Returns 1 when fewer than two embeddings are provided or no pairs exist.
func BinaryWords ¶
BinaryWords returns the number of uint64 words required to represent dim bits.
func Clamp01 ¶
Clamp01 clamps x into [0,1]. NaN and negative values collapse to 0; values above 1 collapse to 1. The NaN/negative check is FIRST and deliberate: a naive `x < 0` / `x > 1` pair lets NaN fall through (both comparisons are false), returning NaN and poisoning any weighted sum downstream.
func ClusterSilhouetteScores64 ¶
ClusterSilhouetteScores64 computes the mean silhouette score per cluster.
func ComputeCentroid64 ¶
ComputeCentroid64 returns the mean vector of the given points. Returns nil if points is empty.
func ComputeClusterCentroids64 ¶
ComputeClusterCentroids64 computes k centroids from assignments. Ragged points are skipped because a fixed-width centroid has no valid slot for them.
func ComputeMedians ¶
ComputeMedians returns the per-dimension median across a set of float32 vectors. The result is suitable for use as the thresholds argument to Quantize. Returns nil if vectors is empty. For a single vector, returns a copy of it.
func ComputeMediansChecked ¶
ComputeMediansChecked computes the per-dimension median across a set of float32 vectors with validation. Returns an error on dimension mismatch.
func Cosine32 ¶
Cosine32 computes cosine similarity between two float32 vectors. Returns 0 if either vector has zero magnitude.
Example ¶
a := []float32{1, 2, 3}
b := []float32{1, 2, 4}
vectors.Normalize32(a)
vectors.Normalize32(b)
fmt.Printf("%.3f\n", vectors.Cosine32(a, b))
Output: 0.991
func Cosine64 ¶
Cosine64 computes cosine similarity between two float64 vectors using scaled accumulation to avoid overflow and underflow for finite inputs. It returns 0 if either vector has zero magnitude.
func CosineDistance32 ¶
CosineDistance32 returns L2 distance on cosine space: 1 - cosine(a, b). Mismatch, empty, and zero-magnitude comparisons return 1.
func CosineDistance64 ¶
CosineDistance64 returns L2 distance on cosine space: 1 - cosine(a, b). Mismatch, empty, and zero-magnitude comparisons return 1.
func CosineToUnit ¶
CosineToUnit remaps a cosine similarity in [-1,1] onto [0,1] via (x+1)/2. A NaN input (e.g. a zero-vector cosine) maps to 0 BEFORE the arithmetic, so it cannot propagate through the remap. The result is Clamp01-guarded against out-of-range cosine inputs.
func DecodeFloat32Batch ¶
DecodeFloat32Batch decodes a slice of little-endian float32 blobs, preserving input order: out[i] corresponds to blobs[i]. It fails fast with an error naming the first blob whose length is not a multiple of 4.
func DecodeFloat32Vec ¶
DecodeFloat32Vec decodes a raw little-endian byte slice into []float32. Returns nil if len(blob) is not a multiple of 4.
func DecodeFloat64Vec ¶
DecodeFloat64Vec decodes a raw little-endian byte slice into []float64. Returns nil when len(data) is not a multiple of 8.
func Dot32 ¶
Dot32 computes the dot product of two float32 vectors, accumulating in float64 to reduce rounding error on high-dimensional inputs. For L2-normalized vectors the result equals cosine similarity.
func DotFromBlob ¶
DotFromBlob computes the dot product between a pre-normalized query vector and a raw little-endian float32 BLOB without intermediate allocation. Both vectors MUST be L2-normalized (as guaranteed by the embedder); under that invariant, dot product == cosine similarity. Callers must also ensure both operands contain only finite values. Returns 0 if dimensions do not match.
func EncodeFloat32Vec ¶
EncodeFloat32Vec encodes a []float32 as a raw little-endian byte slice. Each float32 occupies 4 bytes; 512 dimensions × 4 bytes = 2048 bytes per vector.
func EncodeFloat64Vec ¶
EncodeFloat64Vec encodes a []float64 as a raw little-endian byte slice. Each float64 occupies 8 bytes.
func Euclidean64 ¶
Euclidean64 computes the L2 distance between two float64 vectors. Returns math.Inf(1) if vectors are empty or have different lengths.
func FindElbowCurvature ¶
FindElbowCurvature returns the index at which the smoothed curvature peaks. The curvature is |d²/di²| of the Gaussian-smoothed score vector. minKeep guarantees a minimum index regardless of curvature shape — the search for the peak starts at minKeep so we never cut before that point. Returns len(scores)-1 when no meaningful peak is found (flat or trivially short).
func FindOptimalK ¶
FindOptimalK runs K-means for each candidate k in [minK, maxK] and returns the k with the highest silhouette score.
func FindOptimalK64 ¶
func FindOptimalK64(points [][]float64, minK, maxK int) (bestK int, bestScore float64, assignments []int, centroids [][]float64, scores []float64, kValues []int)
FindOptimalK64 runs K-means for each candidate k and returns the best result.
func FindSemanticBoundaries ¶
FindSemanticBoundaries identifies positions where semantic similarity drops below threshold.
func GaussianSmooth ¶
GaussianSmooth applies a 1D Gaussian kernel to scores. sigma controls smoothing width; typical value 1.0. Short vectors and invalid sigma values return a copy unchanged.
func Gradient ¶
Gradient computes the discrete gradient of a score slice using central differences. Forward difference at index 0, backward at index n-1.
func HammingDistance ¶
func HammingDistance(a, b BinaryVector) int
HammingDistance returns the number of differing bits between two binary vectors. Returns 0 if the vectors have different lengths.
func HashANNProfile ¶
func HashANNProfile(profile ANNProfile) hash.Digest
HashANNProfile returns a stable profile digest for ANN comparison inputs.
func HashFloat32Slice ¶
HashFloat32Slice returns a stable SHA-256 hash for float32 identity data such as binary-index median thresholds.
func HashIndexProfile ¶
HashIndexProfile returns a stable hash for caller-owned index profile fields such as candidate limits, quantization settings, or external ANN parameters.
func HashIndexProfileIdentity ¶
func HashIndexProfileIdentity(fields map[string]string) supporthash.Digest
HashIndexProfileIdentity returns a transform identity digest for caller-owned index profile fields. HashIndexProfile is retained for compatibility with existing persisted profile hashes.
func IsUnit32 ¶
IsUnit32 validates whether v is a unit vector within tolerance. Returns false for invalid tolerances, empty vectors, zero vectors, and NaN/Inf inputs.
func IsUnit64 ¶
IsUnit64 validates whether v is a unit vector within tolerance. Returns false for invalid tolerances, empty vectors, zero vectors, and NaN/Inf inputs.
func JaccardWords ¶
JaccardWords computes Jaccard similarity between meaningful words in two texts. Returns 0 for empty inputs.
func MeanPool32 ¶
MeanPool32 averages a set of float32 vectors componentwise and L2-normalizes the result, so the output is a unit vector suitable for cosine-via-dot scoring. Members whose length differs from the first vector's length are skipped. Special cases: an empty input returns nil; a single-vector input is returned unchanged (not normalized).
func MeanStddev ¶
MeanStddev returns the mean and POPULATION standard deviation (divisor N, not N-1) of vals. An empty slice returns (0, 0) rather than NaN. The population convention is deliberate so derived thresholds are reproducible across runs.
func MinMaxNormalize ¶
MinMaxNormalize rescales vals into [0,1] via (v-min)/(max-min), returning a new slice. Degenerate input — fewer than 2 elements, or a spread (max-min) below 1e-10 — returns all 0.5: a (max-min) of ~0 would divide to NaN, and 0.5 keeps such a signal weight-neutral rather than poisoning a downstream weighted sum. An empty input returns an empty (non-nil) slice.
func NearDuplicateGroups ¶
NearDuplicateGroups groups input vectors into clusters of mutual near-duplicates by cosine similarity. Two vectors are linked when their cosine >= threshold; groups are the connected components of that graph (singletons omitted). Returns indices into vecs. Uses binary quantization as an O(n) Hamming prefilter, then verifies candidate pairs with exact Cosine32.
Guards: nil/empty input or fewer than 2 usable vectors returns an empty result; nil or zero-length member vectors are skipped (never linked).
func NearDuplicatePairs ¶
NearDuplicatePairs returns the linked index pairs (i<j, cosine>=threshold) rather than connected-component groups. Pairs are returned in ascending (i, j) order. Same guards as NearDuplicateGroups.
func NewBinaryIndexChecked ¶
func NewBinaryIndexChecked(blobs [][]byte, groups []string, chunkIndices []int, dims int) (*BinaryIndex, IndexBuildReport)
NewBinaryIndexChecked builds a BinaryIndex and reports skipped rows and build errors. Rows are validated before their keys are reserved, so the first valid row for each (Group, ChunkIndex) key is retained.
func NewExactIndexChecked ¶
func NewExactIndexChecked(dims int, chunks []IndexChunk, arena []byte) (*ExactIndex, IndexBuildReport)
NewExactIndexChecked constructs an ExactIndex and reports skipped or suspicious rows. Rows containing non-finite vector values are skipped and counted in SkippedBadBlob. It snapshots arena, so callers may mutate or release their slice after this function returns.
func NormSquared32 ¶
NormSquared32 returns the squared L2 norm of v. Accumulates in float64 to avoid intermediate rounding error.
func NormSquared64 ¶
NormSquared64 returns the squared L2 norm of v.
func Normalize32 ¶
Normalize32 L2-normalizes a float32 vector in place using scaled accumulation to avoid overflow and underflow for finite inputs. It returns the original magnitude, which may be +Inf when the mathematical magnitude exceeds the float32 range.
func Normalize64 ¶
Normalize64 L2-normalizes a float64 vector in place using scaled accumulation to avoid overflow and underflow for finite inputs. It returns the original magnitude, which may be +Inf when the mathematical magnitude exceeds the float64 range.
func NormalizeTo32 ¶
NormalizeTo32 returns a new L2-normalized copy of v without mutating the input. If v has zero magnitude, the input slice is returned unchanged (identity preserved) rather than a fresh zero slice. Scaled accumulation avoids overflow and underflow for finite inputs.
func NormalizeTo64 ¶
NormalizeTo64 is the float64 twin of NormalizeTo32. Zero-magnitude input is returned unchanged.
func QuantizeInto ¶
func QuantizeInto(dst BinaryVector, vec, thresholds []float32) error
QuantizeInto encodes a float32 embedding into an existing BinaryVector buffer. Bits are cleared before writing to avoid stale bits from prior calls.
func SilhouetteCoefficient64 ¶
SilhouetteCoefficient64 computes the silhouette coefficient for one point.
func SilhouetteScore ¶
SilhouetteScore computes the average silhouette coefficient for the given clustering. For large datasets (n > silhouetteSampleN), a deterministic subsample is used to keep computation tractable.
func SlidingWindowSimilarity ¶
SlidingWindowSimilarity calculates similarity scores using a sliding window. Returns similarity scores between consecutive windows.
func SmoothSimilarities ¶
SmoothSimilarities applies smoothing to reduce noise in similarity scores.
func TopKMaxIndices ¶
TopKMaxIndices returns the indices of the k largest values in scores, ordered largest-first. k is clamped to len(scores); k<=0 returns an empty slice. Equal scores are ordered by ascending index (stable). Runs in O(n log k) time, O(k) space.
func TopKMinIndices ¶
TopKMinIndices returns the indices of the k smallest values in scores, ordered smallest-first. Same clamping and stability rules as TopKMaxIndices.
Types ¶
type ANNProfile ¶
type ANNProfile struct {
ID string
Kind IndexKind
CandidateLimit int
Oversampling float64
ExactRescore bool
Quantization string
MemoryEstimate MemoryEstimate
TransformDigest hash.Digest
ProfileDigest hash.Digest
}
ANNProfile describes one approximate-nearest-neighbor benchmark profile. It is data-only and does not imply a specific ANN implementation.
type ANNProfileResult ¶
type ANNProfileResult struct {
Profile ANNProfile
RecallAtK float64
Latency LatencySummary
IndexedRows int
EvaluatedQuery int
}
ANNProfileResult records quality and latency observations for a profile.
Example ¶
transform := hash.HashIdentity(
hash.IdentityPart{Kind: "embedding_model", ID: "demo-hash", Version: "1"},
hash.IdentityPart{Kind: "chunker", Version: "semantic-v1", ConfigHash: "chunk-cfg"},
)
exact := vectors.ANNProfile{
ID: "exact-baseline",
Kind: vectors.IndexKindExact,
CandidateLimit: 100,
TransformDigest: transform,
}
binary := vectors.ANNProfile{
ID: "binary-screen",
Kind: vectors.IndexKindBinary,
CandidateLimit: 300,
Oversampling: 3,
ExactRescore: true,
Quantization: "median_binary",
TransformDigest: transform,
}
binary.ProfileDigest = vectors.HashANNProfile(binary)
results := []vectors.ANNProfileResult{
{Profile: exact, RecallAtK: 1, Latency: vectors.LatencySummary{Samples: 20, P95MS: 4.2}},
{Profile: binary, RecallAtK: 0.92, Latency: vectors.LatencySummary{Samples: 20, P95MS: 1.1}},
}
fmt.Println(results[0].Profile.ID, results[1].Profile.Quantization, results[1].Profile.ProfileDigest.String() != "")
Output: exact-baseline median_binary true
type BinaryIndex ¶
type BinaryIndex struct {
// contains filtered or unexported fields
}
BinaryIndex is a thread-safe, in-memory pre-filter for semantic search. It stores packed bit vectors for all chunks and provides fast Hamming-distance pre-filtering.
func NewBinaryIndex ¶
func NewBinaryIndex(blobs [][]byte, groups []string, chunkIndices []int, dims int) *BinaryIndex
NewBinaryIndex builds a BinaryIndex from raw little-endian float32 blobs. Rows with invalid or non-finite blobs, missing group/chunk metadata, or duplicate keys are skipped. For duplicate keys, the first valid row wins.
func (*BinaryIndex) Len ¶
func (idx *BinaryIndex) Len() int
Len returns the number of entries in the index.
func (*BinaryIndex) Manifest ¶
func (idx *BinaryIndex) Manifest(identity IndexManifestIdentity) IndexManifest
Manifest returns a compatibility manifest for the binary index, merging caller-owned embedding/chunking identity with index-owned dimensions, row count, and median thresholds hash.
func (*BinaryIndex) SearchCandidates ¶
func (idx *BinaryIndex) SearchCandidates(queryVec []float32) ([]HammingCandidate, bool)
SearchCandidates returns the best Hamming-distance candidates for exact re-ranking.
func (*BinaryIndex) SearchCandidatesLimit ¶
func (idx *BinaryIndex) SearchCandidatesLimit(queryVec []float32, limit int) ([]HammingCandidate, bool)
SearchCandidatesLimit returns up to limit best Hamming-distance candidates for exact re-ranking. A limit larger than the index size is clamped; a non-positive limit returns an empty candidate slice for a valid query. Non-finite queries are rejected.
type BinaryIndexEntry ¶
BinaryIndexEntry maps a binary vector back to its source ID and chunk index.
type BinaryVector ¶
type BinaryVector []uint64
BinaryVector is a packed bit representation of a float32 vector. For a 768-dimension embedding, this is 12 uint64s (768/64 = 12). For a 1024-dimension embedding, this is 16 uint64s.
func Quantize ¶
func Quantize(vec []float32, thresholds []float32) (BinaryVector, error)
Quantize converts a float32 embedding to a BinaryVector using per-dimension thresholds. For dimension i: if vec[i] > thresholds[i], the bit is 1; else 0. Bits are packed into uint64s with dimension 0 at bit 0 of uint64[0]. Returns an error if len(vec) != len(thresholds) — typically a sign that the embedder dimension changed between runs and the binary index is stale.
type ExactIndex ¶
type ExactIndex struct {
// contains filtered or unexported fields
}
ExactIndex is a thread-safe, in-memory flat vector index. It stores L2-normalized float32 vectors packed in a single contiguous byte arena to minimize GC allocation overhead.
func NewExactIndex ¶
func NewExactIndex(dims int, chunks []IndexChunk, arena []byte) *ExactIndex
NewExactIndex constructs an ExactIndex. It snapshots arena, so callers may mutate or release their slice after this function returns.
func (*ExactIndex) Clear ¶
func (idx *ExactIndex) Clear()
Clear clears all internal slices and maps.
func (*ExactIndex) Manifest ¶
func (idx *ExactIndex) Manifest(identity IndexManifestIdentity) IndexManifest
Manifest returns a compatibility manifest for the exact index, merging caller-owned embedding/chunking identity with index-owned dimensions and row count.
func (*ExactIndex) Search ¶
func (idx *ExactIndex) Search(queryVec []float32, limit int, minSimilarity float64) ([]SearchResult, bool)
Search scores all vectors against the query and returns the top-K matches. Returns false if the index is empty or the query is invalid.
func (*ExactIndex) SearchFiltered ¶
func (idx *ExactIndex) SearchFiltered( queryVec []float32, limit int, minSimilarity float64, groups []string, ) ([]SearchResult, bool)
SearchFiltered scores only vectors belonging to the specified groups. Returns false if the index is empty or the query is invalid.
func (*ExactIndex) SearchGroupsByMaxPool ¶
func (idx *ExactIndex) SearchGroupsByMaxPool(queryVec []float32, limit int) ([]SearchResult, bool)
SearchGroupsByMaxPool pools the best similarity score per group across all chunks and returns the top-K best matching groups. Returns false if the index is empty or the query is invalid.
func (*ExactIndex) SearchKeys ¶
func (idx *ExactIndex) SearchKeys( queryVec []float32, limit int, minSimilarity float64, keys []IndexKey, ) ([]SearchResult, bool)
SearchKeys scores only the chunks identified by keys. Duplicate and missing keys are ignored. Returns false if the index is empty or the query is invalid.
type HammingCandidate ¶
HammingCandidate holds a candidate from the Hamming pre-filter stage.
type IndexBuildReport ¶
type IndexBuildReport struct {
InputRows int
IndexedRows int
SkippedBadSpan int
SkippedBadBlob int
SkippedMissingMetadata int
SkippedDuplicateKey int
DimensionMismatch int
MedianError string
QuantizeError string
}
IndexBuildReport describes rows accepted or skipped while building an index.
type IndexChunk ¶
type IndexChunk struct {
Group string // e.g. EntryID
ChunkIndex int // 0-based index of this chunk within the Group
Offset int // Byte offset into the contiguous arena slice
Length int // Byte length of this vector in the arena
}
IndexChunk defines the location of a chunk's vector in the index arena.
type IndexKind ¶
type IndexKind string
IndexKind identifies the index implementation described by a manifest.
type IndexManifest ¶
type IndexManifest struct {
Version int
CreatedAt time.Time
Kind IndexKind
Dims int
IndexedRows int
EmbeddingModelID string
EmbeddingModelHash string
MediansHash string
IndexProfileHash string
ChunkStrategy string
ChunkSize int
ChunkOverlap int
}
IndexManifest records the compatibility identity and provenance for a built vector index. It is intentionally data-only so callers can persist it in any format they already use.
func NewIndexManifest ¶
func NewIndexManifest(identity IndexManifestIdentity, indexedRows int) IndexManifest
NewIndexManifest builds a manifest from caller-owned identity fields.
func (IndexManifest) Validate ¶
func (m IndexManifest) Validate(expected IndexManifestIdentity) error
Validate returns an actionable error when a manifest is incompatible with the expected identity. A fully zero expected identity is valid for backward-compatible callers that have not opted into manifest enforcement.
type IndexManifestIdentity ¶
type IndexManifestIdentity struct {
Kind IndexKind
Dims int
EmbeddingModelID string
EmbeddingModelHash string
MediansHash string
IndexProfileHash string
ChunkStrategy string
ChunkSize int
ChunkOverlap int
}
IndexManifestIdentity describes compatibility inputs that can make a vector index stale when they change. Zero-valued fields are treated as unspecified by Validate so callers can enforce only the identity dimensions they own.
type KMeans64Config ¶
KMeans64Config holds configuration for float64 K-means clustering.
type KMeans64Result ¶
type KMeans64Result struct {
Assignments []int
Centroids [][]float64
K int
Iterations int
Converged bool
}
KMeans64Result holds the result of a float64 K-means clustering run.
func KMeans64 ¶
func KMeans64(points [][]float64, cfg KMeans64Config) *KMeans64Result
KMeans64 performs K-means clustering with K-means++ initialization using cosine distance. It preserves the legacy float64 clustering semantics used by the higher-level clustering service.
type KMeansResult ¶
type KMeansResult struct {
K int
Assignments []int // Assignments[i] = cluster ID for points[i]
Centroids [][]float32 // Centroids[j] = centroid vector for cluster j
Iterations int
}
KMeansResult holds the output of a K-means clustering run.
type LatencySummary ¶
LatencySummary records deterministic benchmark sample summaries. Tests should compare explicit values, not wall-clock thresholds.
type MemoryEstimate ¶
MemoryEstimate records caller-measured or estimated index memory.
type Scored ¶
Scored pairs an index with its fused score. Sorted by descending score, then ascending index for deterministic tie-breaks.
func RRF ¶
RRF fuses ranked index lists via Reciprocal Rank Fusion. Each list is a ranking where position i has rank i+1.
Each index contribution is:
score += 1/(k+rank)
where rank is 1-based.
Raw metric scores are not used; only rank positions contribute.
Returns the fused ranking sorted by descending score and ascending index for ties, and the maximum score (or 0 for empty input).
type ScoredIndex ¶
ScoredIndex identifies one scored input row.
func TopKFromBlob ¶
func TopKFromBlob(query []float32, blobs [][]byte, limit int, minScore float32) []ScoredIndex
TopKFromBlob scores raw little-endian float32 blobs against query and returns the top limit input indexes. Invalid or dimension-mismatched blobs are skipped, as are blobs whose score is non-finite. A non-finite query returns no results. Equal scores are ordered by ascending input index.
type SearchResult ¶
SearchResult holds a query match.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package clustering performs online and offline clustering over caller-supplied vector embeddings.
|
Package clustering performs online and offline clustering over caller-supplied vector embeddings. |
|
Package pq implements Product Quantization for efficient vector compression.
|
Package pq implements Product Quantization for efficient vector compression. |