1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
"""Type stubs for the native ``pyvicinity._core`` extension module.
Hand-written because PyO3 doesn't generate stubs. Verified against the
compiled module by ``mypy.stubtest`` in CI -- keep in sync with
``src/python.rs`` or stubtest will fail.
"""
=
:
:
"""Sentinel value padded into ``batch_search`` ID rows shorter than ``k``.
Equal to ``-1``, matching faiss's convention. Mask with::
valid = ids[ids != pyvicinity.MISSING_LABEL]
"""
:
"""Sentinel value padded into ``batch_search`` distance rows shorter than ``k``.
Equal to ``math.inf``."""
"""Distance metric for vector comparison.
Not a true ``enum.Enum`` (PyO3 emits a plain class with ``int``-comparable
members), but works the same way for ``==`` and pattern matching.
"""
:
"""Euclidean (L2) distance."""
:
"""Cosine distance: ``1 - cos(a, b)``. Inputs are expected to be
L2-normalized; pass ``auto_normalize=True`` to the index to handle
raw vectors on both insert and query."""
:
"""Angular distance: ``arccos(cos(a, b)) / pi``, in ``[0, 1]``.
Computes norms internally; raw vectors are fine."""
:
"""Inner-product distance: ``-dot(a, b)`` (for MIPS).
Not normalized; query magnitude affects ranking by design."""
: None # type: ignore[assignment] # eq_int enums are unhashable
...
...
...
"""HNSW index for approximate nearest-neighbor search.
Example::
import numpy as np
from pyvicinity import HNSWIndex, DistanceMetric
vectors = np.random.randn(10_000, 128).astype(np.float32)
index = HNSWIndex(dim=128, metric=DistanceMetric.Cosine, auto_normalize=True)
index.add_items(vectors)
index.build()
ids, dists = index.search(vectors[0], k=10)
"""
...
"""Add a batch of vectors.
Args:
vectors: 2-D ``(n, dim)`` float32 array, C-contiguous.
ids: Optional 1-D int64 array of length n. Each value must be
in ``[0, 2**32)`` (vicinity stores IDs as u32 internally).
If omitted, sequential IDs are assigned starting at the
current ``len(index)``.
"""
"""Finalize the index. Must be called before any search."""
"""Set the default ``ef_search`` parameter for subsequent queries."""
"""Search for the k nearest neighbors of one query vector.
Args:
query: 1-D ``(dim,)`` float32 array.
k: Number of neighbors to return.
ef: Search width. Defaults to ``self.ef_search``.
Returns:
``(ids, distances)``: 1-D arrays of length at most k.
Shorter than k only when the index has fewer than k vectors.
"""
"""Search for the k nearest neighbors of each query.
Args:
queries: 2-D ``(nq, dim)`` float32 array, C-contiguous.
k: Number of neighbors per query.
ef: Search width. Defaults to ``self.ef_search``.
Returns:
``(ids, distances)``: 2-D arrays of shape ``(nq, k)``. Rows
with fewer than k results are padded with :data:`MISSING_LABEL`
and :data:`MISSING_DISTANCE` so the result is rectangular.
"""
"""Number of vectors currently in the index."""
"""Vector dimension."""
"""Distance metric this index was built with."""
"""Whether inserts and queries are L2-normalized internally."""
"""Max connections per node (the ``M`` HNSW parameter)."""
"""Search width during construction."""
"""Default search width for queries."""
...
...