Package genlayer_embeddings#

class genlayer_embeddings.ChebyshevDistance[source]#

Bases: Distance

L-infinity (max-coordinate) distance. A true metric, safe for pruning.

__call__(l, r)[source]#

Compute the distance between two vectors.

Parameters:
  • l – left-hand vector

  • r – right-hand vector

Returns:

distance between l and r

__gl_allow_storage__ = True#
__init__(*args, **kwargs)#
batch(l, r)[source]#
class genlayer_embeddings.Distance[source]#

Bases: Protocol

Protocol for distance functions used by VecDB.

Implementations must be a true metric (non-negative, symmetric, zero iff equal, and satisfying the triangle inequality); otherwise the cover-tree pruning in VecDB.knn() may skip the true nearest neighbor.

__call__(l, r) Any[source]#

Compute the distance between two vectors.

Parameters:
  • l – left-hand vector

  • r – right-hand vector

Returns:

distance between l and r

Return type:

Any

__init__(*args, **kwargs)#
class genlayer_embeddings.EuclideanDistance[source]#

Bases: Distance

__call__(l, r)[source]#

Compute the distance between two vectors.

Parameters:
  • l – left-hand vector

  • r – right-hand vector

Returns:

distance between l and r

__gl_allow_storage__ = True#
__init__(*args, **kwargs)#
batch(l, r)[source]#
class genlayer_embeddings.ManhattanDistance[source]#

Bases: Distance

L1 (taxicab) distance. A true metric, safe for cover-tree pruning.

__call__(l, r)[source]#

Compute the distance between two vectors.

Parameters:
  • l – left-hand vector

  • r – right-hand vector

Returns:

distance between l and r

__gl_allow_storage__ = True#
__init__(*args, **kwargs)#
batch(l, r)[source]#
genlayer_embeddings.SentenceTransformer(model: str) Callable[[str], ndarray][source]#
Return type:

Callable[[str], ndarray]

genlayer_embeddings.SentenceTransformerFromPath(path: str) Callable[[str], ndarray][source]#
Return type:

Callable[[str], ndarray]

class genlayer_embeddings.VecDB[source]#

Bases: Generic

Data structure that supports storing and querying vector data using Cover Trees

Cover trees provide logarithmic time nearest neighbor search with theoretical guarantees.

There are two entities that can act as a key:

  1. vector (can have duplicates)

  2. id (int alias, can’t have duplicates)

Warning

import numpy before from genlayer import * if you wish to use VecDB!

Element = Element#

Shorthand to prevent global namespace pollution

Id = Id#

int alias to prevent confusion

__gl_allow_storage__ = True#
__init__()[source]#
__iter__()[source]#
__len__() int[source]#
Return type:

int

get_by_id(id: Id) VecDBElement[T, S, V, None][source]#
Return type:

VecDBElement[T, S, V, None]

get_by_id_or_none(id: Id) VecDBElement[T, S, V, None] | None[source]#
Return type:

VecDBElement[T, S, V, None] | None

insert(key: np.ndarray[tuple[S], np.dtype[T]], val: V) Id[source]#
Return type:

Id

knn(v: np.ndarray[tuple[S], np.dtype[T]], k: int) Iterator[VecDBElement[T, S, V, T]][source]#

Find k nearest neighbors using cover tree with pruning

Return type:

Iterator[VecDBElement[T, S, V, T]]

genlayer_embeddings.get_model(model: str, inputs: dict[str, DTypeLike], *, models_db=_ALL_MODELS)[source]#