-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbindings.cpp
More file actions
66 lines (56 loc) · 2.36 KB
/
bindings.cpp
File metadata and controls
66 lines (56 loc) · 2.36 KB
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
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include "simple_hnsw.h"
namespace py = pybind11;
PYBIND11_MODULE(SimpleHNSW, m) {
m.doc() = "SimpleHNSW - A simple HNSW implementation for approximate nearest neighbor search";
py::class_<SimpleHNSWIndex>(m, "SimpleHNSWIndex")
.def(py::init<int, double, int, int, unsigned int>(),
py::arg("L") = 5,
py::arg("mL") = 0.62,
py::arg("efc") = 10,
py::arg("maxConnections") = 16,
py::arg("seed") = 0u,
R"doc(
Initialize a SimpleHNSW index.
Args:
L (int): Number of layers (default: 5)
mL (float): Normalization factor for layer assignment (default: 0.62)
efc (int): Size of the dynamic candidate list during construction (default: 10)
maxConnections (int): Maximum number of connections per node (default: 16)
seed (int): RNG seed (0 => non-deterministic)
)doc")
.def("insert", &SimpleHNSWIndex::insert,
py::arg("vector"),
R"doc(
Insert a vector into the index.
Args:
vector (list[float]): The vector to insert
)doc")
.def("search", &SimpleHNSWIndex::search,
py::arg("query"),
py::arg("ef") = 1,
R"doc(
Search for the nearest neighbors of a query vector.
Args:
query (list[float]): The query vector
ef (int): Size of the dynamic candidate list during search (default: 1)
Returns:
list[tuple[float, int]]: List of (distance, index) pairs for nearest neighbors
)doc")
.def("toJSON", &SimpleHNSWIndex::toJSON,
R"doc(
Serialize the index to a JSON string.
Returns:
str: JSON representation of the index
)doc")
.def_static("fromJSON", &SimpleHNSWIndex::fromJSON,
py::arg("json"),
R"doc(
Deserialize an index from a JSON string.
Args:
json (str): JSON representation of the index
Returns:
SimpleHNSWIndex: Deserialized index
)doc");
}