@@ -36,7 +36,7 @@ def test_hnswlib_vec_store_add_texts(tmp_path) -> None:
36
36
assert docsearch .doc_index .num_docs () == 3
37
37
38
38
39
- @pytest .mark .parametrize ('metric' , ['cosine' , 'ip' , ' l2' ])
39
+ @pytest .mark .parametrize ('metric' , ['cosine' , 'l2' ])
40
40
def test_sim_search (metric , tmp_path ) -> None :
41
41
"""Test end to end construction and simple similarity search."""
42
42
texts = ["foo" , "bar" , "baz" ]
@@ -45,12 +45,35 @@ def test_sim_search(metric, tmp_path) -> None:
45
45
FakeEmbeddings (),
46
46
work_dir = str (tmp_path ),
47
47
n_dim = 10 ,
48
+ dist_metric = metric ,
49
+ )
50
+ output = hnswlib_vec_store .similarity_search ("foo" , k = 1 )
51
+ assert output == [Document (page_content = "foo" )]
52
+
53
+
54
+ @pytest .mark .parametrize ('metric' , ['cosine' , 'l2' ])
55
+ def test_sim_search_all_configurations (metric , tmp_path ) -> None :
56
+ """Test end to end construction and simple similarity search."""
57
+ texts = ["foo" , "bar" , "baz" ]
58
+ hnswlib_vec_store = HnswLib .from_texts (
59
+ texts ,
60
+ FakeEmbeddings (),
61
+ work_dir = str (tmp_path ),
62
+ dist_metric = metric ,
63
+ n_dim = 10 ,
64
+ max_elements = 8 ,
65
+ index = False ,
66
+ ef_construction = 300 ,
67
+ ef = 20 ,
68
+ M = 8 ,
69
+ allow_replace_deleted = False ,
70
+ num_threads = 2 ,
48
71
)
49
72
output = hnswlib_vec_store .similarity_search ("foo" , k = 1 )
50
73
assert output == [Document (page_content = "foo" )]
51
74
52
75
53
- @pytest .mark .parametrize ('metric' , ['cosine' , 'ip' , ' l2' ])
76
+ @pytest .mark .parametrize ('metric' , ['cosine' , 'l2' ])
54
77
def test_sim_search_by_vector (metric , tmp_path ) -> None :
55
78
"""Test end to end construction and similarity search by vector."""
56
79
texts = ["foo" , "bar" , "baz" ]
@@ -59,14 +82,15 @@ def test_sim_search_by_vector(metric, tmp_path) -> None:
59
82
FakeEmbeddings (),
60
83
work_dir = str (tmp_path ),
61
84
n_dim = 10 ,
85
+ dist_metric = metric ,
62
86
)
63
87
embedding = [1.0 ] * 10
64
88
output = hnswlib_vec_store .similarity_search_by_vector (embedding , k = 1 )
65
89
66
90
assert output == [Document (page_content = "bar" )]
67
91
68
92
69
- @pytest .mark .parametrize ('metric' , ['cosine' , 'ip' , ' l2' ])
93
+ @pytest .mark .parametrize ('metric' , ['cosine' , 'l2' ])
70
94
def test_sim_search_with_score (metric , tmp_path ) -> None :
71
95
"""Test end to end construction and similarity search with score."""
72
96
texts = ["foo" , "bar" , "baz" ]
@@ -75,6 +99,7 @@ def test_sim_search_with_score(metric, tmp_path) -> None:
75
99
FakeEmbeddings (),
76
100
work_dir = str (tmp_path ),
77
101
n_dim = 10 ,
102
+ dist_metric = metric ,
78
103
)
79
104
output = hnswlib_vec_store .similarity_search_with_score ("foo" , k = 1 )
80
105
assert len (output ) == 1
@@ -84,6 +109,26 @@ def test_sim_search_with_score(metric, tmp_path) -> None:
84
109
assert np .isclose (out_score , 0.0 , atol = 1.e-6 )
85
110
86
111
112
+ def test_sim_search_with_score_for_ip_metric (tmp_path ) -> None :
113
+ """
114
+ Test end to end construction and similarity search with score for ip
115
+ (inner-product) metric.
116
+ """
117
+ texts = ["foo" , "bar" , "baz" ]
118
+ hnswlib_vec_store = HnswLib .from_texts (
119
+ texts ,
120
+ FakeEmbeddings (),
121
+ work_dir = str (tmp_path ),
122
+ n_dim = 10 ,
123
+ dist_metric = 'ip' ,
124
+ )
125
+ output = hnswlib_vec_store .similarity_search_with_score ("foo" , k = 3 )
126
+ assert len (output ) == 3
127
+
128
+ for result in output :
129
+ assert result [1 ] == - 8.0
130
+
131
+
87
132
@pytest .mark .parametrize ('metric' , ['cosine' , 'l2' ])
88
133
def test_max_marginal_relevance_search (metric , tmp_path ) -> None :
89
134
"""Test MRR search."""
0 commit comments