Documentation
¶
Index ¶
- Constants
- func CosineDistance(a, b []float32) float32
- func CosineSimilarity(a, b []float32) float32
- func DotProduct(a, b []float32) float32
- func L2Distance(a, b []float32) float32
- func L2DistanceSquared(a, b []float32) float32
- func MarshalHNSW(h *HNSW) []byte
- func Normalize(v []float32) []float32
- func QuantizedCosineDistance(qv QuantizedVector, query []float32, queryNorm float32) float32
- func QuantizedDotProduct(qv QuantizedVector, b []float32) float32
- func VectorNorm(v []float32) float32
- type BruteForce
- func (bf *BruteForce) Add(id string, vec []float32)
- func (bf *BruteForce) Dim() int
- func (bf *BruteForce) ForEach(fn func(id string, vec []float32))
- func (bf *BruteForce) Get(id string) []float32
- func (bf *BruteForce) Has(id string) bool
- func (bf *BruteForce) Len() int
- func (bf *BruteForce) Remove(id string)
- func (bf *BruteForce) Search(query []float32, k int) []SearchHit
- type Engine
- func (e *Engine) Add(kbID, id string, vec []float32) error
- func (e *Engine) DropIndex(kbID string)
- func (e *Engine) EnsureIndex(kbID string, dim int)
- func (e *Engine) HasIndex(kbID string) bool
- func (e *Engine) Len(kbID string) int
- func (e *Engine) LoadIndex(kbID string, dim int, vectors map[string][]float32)
- func (e *Engine) Remove(kbID, id string) error
- func (e *Engine) Search(kbID string, query []float32, k int) ([]SearchHit, error)
- type EngineConfig
- type HNSW
- type Index
- type QuantizedVector
- type SearchHit
Constants ¶
const ( DefaultM = 16 // max edges per node per layer DefaultEfConstruction = 200 // beam width during construction DefaultEfSearch = 50 // beam width during search (tunable at query time) )
HNSW parameters.
const (
// DefaultHNSWThreshold is the vector count at which we switch from brute-force to HNSW.
DefaultHNSWThreshold = 10_000
)
Variables ¶
This section is empty.
Functions ¶
func CosineDistance ¶
CosineDistance returns 1 - CosineSimilarity, in [0, 2]. Suitable as a distance metric where 0 = identical.
func CosineSimilarity ¶
CosineSimilarity returns the cosine similarity between two vectors. Returns a value in [-1, 1] where 1 means identical direction. Both vectors must have the same length; panics otherwise.
func DotProduct ¶
DotProduct returns the dot product of two vectors.
func L2Distance ¶
L2Distance returns the Euclidean distance between two vectors.
func L2DistanceSquared ¶
L2DistanceSquared returns the squared Euclidean distance (avoids sqrt). Useful for comparisons where only relative ordering matters.
func Normalize ¶
Normalize returns a unit-length copy of v. Returns a zero vector if v has zero magnitude.
func QuantizedCosineDistance ¶
func QuantizedCosineDistance(qv QuantizedVector, query []float32, queryNorm float32) float32
QuantizedCosineDistance computes approximate cosine distance using a quantized stored vector and a float32 query. queryNorm should be precomputed for batch efficiency. Uses the precomputed Norm field instead of recomputing per call.
func QuantizedDotProduct ¶
func QuantizedDotProduct(qv QuantizedVector, b []float32) float32
QuantizedDotProduct computes an approximate dot product between a quantized stored vector and a float32 query vector without full dequantization.
dot(dequant(q), b) = scale * sum(q_i * b_i) + offset * sum(b_i)
Types ¶
type BruteForce ¶
type BruteForce struct {
// contains filtered or unexported fields
}
BruteForce is a linear scan index. Efficient for small collections (<10K vectors). Supports optional int8 quantization for faster distance computation.
func NewBruteForce ¶
func NewBruteForce(dim int, quantize bool) *BruteForce
NewBruteForce creates a brute-force index for the given dimension. If quantize is true, vectors are also stored in int8 form for faster search.
func (*BruteForce) Add ¶
func (bf *BruteForce) Add(id string, vec []float32)
func (*BruteForce) Dim ¶
func (bf *BruteForce) Dim() int
func (*BruteForce) ForEach ¶
func (bf *BruteForce) ForEach(fn func(id string, vec []float32))
ForEach calls fn for each stored vector. Holds read lock for the duration.
func (*BruteForce) Get ¶
func (bf *BruteForce) Get(id string) []float32
Get returns the raw float32 vector for the given ID, or nil if not found.
func (*BruteForce) Has ¶
func (bf *BruteForce) Has(id string) bool
func (*BruteForce) Len ¶
func (bf *BruteForce) Len() int
func (*BruteForce) Remove ¶
func (bf *BruteForce) Remove(id string)
type Engine ¶
type Engine struct {
// contains filtered or unexported fields
}
Engine manages per-KB vector indexes with adaptive strategy switching.
func (*Engine) Add ¶
Add inserts or updates a vector in the KB's index. Returns an error on dimension mismatch instead of panicking.
func (*Engine) EnsureIndex ¶
EnsureIndex creates a per-KB index if it doesn't exist. Thread-safe.
func (*Engine) LoadIndex ¶
LoadIndex populates a KB's index from pre-existing vectors. Typically called at startup to hydrate the index from DB.
type EngineConfig ¶
EngineConfig configures the vector search engine.
type HNSW ¶
type HNSW struct {
// contains filtered or unexported fields
}
HNSW implements the Hierarchical Navigable Small World graph. Reference: Malkov & Yashunin, 2016/2018.
func UnmarshalHNSW ¶
UnmarshalHNSW deserializes an HNSW index from bytes.
func (*HNSW) Add ¶
Add inserts a vector into the index. Thread-safe.
If a node with the same ID already exists, only the stored vector is updated. Graph edges are NOT re-linked. For correct results after a vector change, callers should Remove then Add to rebuild edges.
type Index ¶
type Index interface {
Add(id string, vec []float32)
Remove(id string)
Search(query []float32, k int) []SearchHit
Len() int
Dim() int
Has(id string) bool
Get(id string) []float32
}
Index is the common interface for vector search indexes.
type QuantizedVector ¶
type QuantizedVector struct {
Data []int8
Scale float32
Offset float32
Norm float32 // precomputed L2 norm of dequantized vector
}
QuantizedVector holds an int8-quantized vector with scale/offset for reconstruction. Original value ≈ (int8_value * scale) + offset
func Quantize ¶
func Quantize(v []float32) QuantizedVector
Quantize converts a float32 vector to int8 using min-max scalar quantization. Maps [min, max] to [-127, 127]. Achieves ~4x memory reduction with <1% accuracy loss on normalized embedding vectors.
func (QuantizedVector) Dequantize ¶
func (qv QuantizedVector) Dequantize() []float32
Dequantize reconstructs a float32 vector from quantized form.