Skip to content

Commit 44ebefa

Browse files
committed
example: add python examples
Signed-off-by: LHT129 <[email protected]>
1 parent fe4fa67 commit 44ebefa

File tree

7 files changed

+175
-43
lines changed

7 files changed

+175
-43
lines changed

examples/python/101_index_hnsw.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Copyright 2024-present the vsag project
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import pyvsag
16+
import numpy as np
17+
import json
18+
19+
def hnsw_test():
20+
dim = 128
21+
num_elements = 10000
22+
query_elements = 1
23+
24+
# Generating sample data
25+
ids = range(num_elements)
26+
data = np.float32(np.random.random((num_elements, dim)))
27+
query = np.float32(np.random.random((query_elements, dim)))
28+
29+
# Declaring index
30+
index_params = json.dumps({
31+
"dtype": "float32",
32+
"metric_type": "l2",
33+
"dim": dim,
34+
"hnsw": {
35+
"max_degree": 16,
36+
"ef_construction": 100
37+
}
38+
})
39+
40+
print("[Create] hnsw index")
41+
index = pyvsag.Index("hnsw", index_params)
42+
43+
print("[Build] hnsw index")
44+
index.build(vectors=data,
45+
ids=ids,
46+
num_elements=num_elements,
47+
dim=dim)
48+
49+
print("[Search] hnsw index")
50+
search_params = json.dumps({"hnsw": {"ef_search": 100}})
51+
for q in query:
52+
result_ids, result_dists = index.knn_search(vector=q, k=10, parameters=search_params)
53+
print("result_ids:", result_ids)
54+
print("result_dists:", result_dists)
55+
56+
57+
if __name__ == '__main__':
58+
hnsw_test()
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Copyright 2024-present the vsag project
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import pyvsag
16+
import numpy as np
17+
import json
18+
19+
def hgraph_test():
20+
dim = 128
21+
num_elements = 10000
22+
query_elements = 1
23+
24+
# Generating sample data
25+
ids = range(num_elements)
26+
data = np.float32(np.random.random((num_elements, dim)))
27+
query = np.float32(np.random.random((query_elements, dim)))
28+
29+
# Declaring index
30+
index_params = """
31+
{
32+
"dtype": "float32",
33+
"metric_type": "l2",
34+
"dim": 128,
35+
"index_param": {
36+
"base_quantization_type": "sq8",
37+
"max_degree": 26,
38+
"ef_construction": 100,
39+
"alpha":1.2
40+
}
41+
}
42+
"""
43+
44+
print("[Create] hgraph index")
45+
index = pyvsag.Index("hgraph", index_params)
46+
47+
print("[Build] hgraph index")
48+
index.build(vectors=data,
49+
ids=ids,
50+
num_elements=num_elements,
51+
dim=dim)
52+
53+
print("[Search] hgraph index")
54+
search_params = json.dumps({"hgraph": {"ef_search": 100}})
55+
for q in query:
56+
result_ids, result_dists = index.knn_search(vector=q, k=10, parameters=search_params)
57+
print("result_ids:", result_ids)
58+
print("result_dists:", result_dists)
59+
60+
61+
if __name__ == '__main__':
62+
hgraph_test()
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
2+
# Copyright 2024-present the vsag project
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
import pyvsag
17+
import numpy as np
18+
import json
19+
20+
def bruteforce_test():
21+
dim = 128
22+
num_elements = 10000
23+
query_elements = 1
24+
25+
# Generating sample data
26+
ids = range(num_elements)
27+
data = np.float32(np.random.random((num_elements, dim)))
28+
query = np.float32(np.random.random((query_elements, dim)))
29+
30+
# Declaring index
31+
index_params = json.dumps({
32+
"dtype": "float32",
33+
"metric_type": "l2",
34+
"dim": dim
35+
})
36+
37+
print("[Create] bruteforce index")
38+
index = pyvsag.Index("brute_force", index_params)
39+
40+
print("[Build] brute_force index")
41+
index.build(vectors=data,
42+
ids=ids,
43+
num_elements=num_elements,
44+
dim=dim)
45+
46+
print("[Search] bruteforce index")
47+
search_params = json.dumps({"bruteforce": {}})
48+
for q in query:
49+
result_ids, result_dists = index.knn_search(vector=q, k=10, parameters=search_params)
50+
print("result_ids:", result_ids)
51+
print("result_dists:", result_dists)
52+
53+
if __name__ == '__main__':
54+
bruteforce_test()

examples/python/todo_examples/101_index_hnsw.py

Lines changed: 0 additions & 14 deletions
This file was deleted.

examples/python/todo_examples/102_index_diskann.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@
1111
# distributed under the License is distributed on an "AS IS" BASIS,
1212
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
# See the License for the specific language governing permissions and
14-
# limitations under the License.
14+
# limitations under the License.

examples/python/todo_examples/103_index_hgraph.py

Lines changed: 0 additions & 14 deletions
This file was deleted.

examples/python/todo_examples/105_index_brute_force.py

Lines changed: 0 additions & 14 deletions
This file was deleted.

0 commit comments

Comments
 (0)