This document describes the Python bindings exposed by the spheni module.
Metric.CosineMetric.L2
IndexKind.FlatIndexKind.IVF
StorageType.F32StorageType.INT8
Constructor overloads:
IndexSpec(dim, metric, kind, normalize=True)IndexSpec(dim, metric, kind, storage, normalize=True)IndexSpec(dim, metric, kind, nlist, normalize=True)IndexSpec(dim, metric, kind, nlist, storage, normalize=True)
Fields:
dim(int): vector dimensionmetric(Metric)normalize(bool): if true and metric is Cosine, vectors are L2-normalized on add/querykind(IndexKind)storage(StorageType)nlist(int): IVF cluster count (only used for IVF)
Constructors:
SearchParams(k)SearchParams(k, nprobe)
Fields:
k(int)nprobe(int): IVF probe count (defaults to 1 if not provided)
Constructor:
SearchHit(id, score)
Fields:
id(int)score(float): higher is better (Cosine or negative L2)
Constructor:
Engine(spec)
Methods:
add(vectors)vectors:numpy.ndarrayfloat32, shape(n, dim), C-contiguous- auto-assigns integer ids starting from 0
add(ids, vectors)ids:numpy.ndarrayint64, shape(n,)vectors:numpy.ndarrayfloat32, shape(n, dim), C-contiguous
train()- Required for IVF before search
search(query, k)query:numpy.ndarrayfloat32, shape(dim,), C-contiguous- returns
List[SearchHit](sorted by descending score)
search(query, k, nprobe)- IVF search with explicit
nprobe
- IVF search with explicit
search_batch(queries, k)queries:numpy.ndarrayfloat32, shape(n, dim), C-contiguous- returns
List[List[SearchHit]]
search_batch(queries, k, nprobe)- IVF batch search with explicit
nprobe
- IVF batch search with explicit
save(path)path: string
load(path)(static)- returns
Engine
- returns
import numpy as np
import spheni
spec = spheni.IndexSpec(
4, spheni.Metric.L2, spheni.IndexKind.Flat, spheni.StorageType.INT8, False
)
engine = spheni.Engine(spec)
base = np.random.rand(10, 4).astype(np.float32)
engine.add(base)
query = np.random.rand(4).astype(np.float32)
hits = engine.search(query, 3)
print([(h.id, h.score) for h in hits])