Vectors#

Vector, SparseVector, Distance and VectorIndex are importable from agensgraph; the rest from agensgraph.vector. See Embedding vectors.

Values#

class agensgraph.Vector(values: Iterable[float] | None = None, *, half: bool = False)#

A dense vector, single precision, kept as the bytes it arrived in until a number is read. Sent as a vector. half=True reads text at half precision.

property values: array[float]#

The numbers, an array('f').

tolist() → list[float]#

The numbers, a list.

to_bytes() → bytes#

pgvector’s binary form.

index(value: float) → int#
count(value: float) → int#
classmethod from_wire(raw: bytes, *, half: bool = False) → Vector#

From pgvector’s binary form.

classmethod from_wire_text(text: bytes, *, half: bool = False) → Vector#

From pgvector’s text form.

class agensgraph.SparseVector(
entries: Mapping[int, float] | Iterable[tuple[int, float]],
dimensions: int,
)#

A sparse vector of dimensions, from a mapping or pairs of index and value. Indices count from zero. Sent as sparsevec text.

property dimensions: int#
property indices: tuple[int, ...]#
property values: tuple[float, ...]#
to_dict() → dict[int, float]#
to_dense() → list[float]#
to_text() → str#

pgvector’s text form, counting from one.

classmethod from_dense(values: Sequence[float]) → SparseVector#
classmethod from_text(text: str | bytes) → SparseVector#
class agensgraph.Distance#

A distance, whose value is its operator.

L2#

<->

INNER_PRODUCT#

<#>, negated

COSINE#

<=>

L1#

<+>

HAMMING#

<~>, for bits

JACCARD#

<%>, for bits

property operator_class: str#

The vector operator class that serves it.

property is_for_bits: bool#
class agensgraph.VectorIndex(
label: str,
name: str,
property: str,
method: str,
operator_class: str | None,
type: str | None,
dimensions: int | None,
options: dict[str, str],
)#

A vector index, in parts.

label: str#
name: str#
property: str#
method: str#
operator_class: str | None#

None for the method’s default

type: str | None#
dimensions: int | None#
options: dict[str, str]#

the settings it was built with, as strings

Statements#

agensgraph.vector.generated_column(name: str, dimensions: int, *, type: str = 'vector') → str#

A column definition, name vector(n) generated, for CREATE VLABEL.

agensgraph.vector.vector_index(
label: str,
property: str,
*,
dimensions: int | None = None,
type: str = 'vector',
method: str = 'hnsw',
operator_class: str = 'vector_cosine_ops',
name: str | None = None,
options: Mapping[str, object] | None = None,
) → str#

A property index for a vector property. Pass dimensions for a property in the map, and leave it out for a column of its own.

agensgraph.vector.nearest(
label: str,
property: str,
*,
dimensions: int | None = None,
type: str = 'vector',
limit: int = 10,
operator: str = '<=>',
variable: str = 'n',
) → str#

The nearest-neighbour search matching vector_index(), with one %s for the query vector.

agensgraph.vector.search_option_statements(
options: Mapping[str, object],
*,
local: bool = True,
) → list[str]#

The SET statements vector_search_options() sends.

agensgraph.vector.SEARCH_OPTIONS = {'hnsw.ef_search': <class 'int'>, 'hnsw.iterative_scan': <class 'str'>, 'hnsw.max_scan_tuples': <class 'int'>, 'hnsw.scan_mem_multiplier': <class 'float'>, 'ivfflat.probes': <class 'int'>, 'ivfflat.iterative_scan': <class 'str'>, 'ivfflat.max_probes': <class 'int'>}#

The search settings, with the type each takes.

agensgraph.vector.TYPES = ('vector', 'halfvec', 'sparsevec')#

pgvector’s types the driver reads.