@@ -36,7 +36,7 @@ def test_hnswlib_vec_store_add_texts(tmp_path) -> None:
3636 assert docsearch .doc_index .num_docs () == 3
3737
3838
39- @pytest .mark .parametrize ('metric' , ['cosine' , 'ip' , ' l2' ])
39+ @pytest .mark .parametrize ('metric' , ['cosine' , 'l2' ])
4040def test_sim_search (metric , tmp_path ) -> None :
4141 """Test end to end construction and simple similarity search."""
4242 texts = ["foo" , "bar" , "baz" ]
@@ -45,12 +45,35 @@ def test_sim_search(metric, tmp_path) -> None:
4545 FakeEmbeddings (),
4646 work_dir = str (tmp_path ),
4747 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 ,
4871 )
4972 output = hnswlib_vec_store .similarity_search ("foo" , k = 1 )
5073 assert output == [Document (page_content = "foo" )]
5174
5275
53- @pytest .mark .parametrize ('metric' , ['cosine' , 'ip' , ' l2' ])
76+ @pytest .mark .parametrize ('metric' , ['cosine' , 'l2' ])
5477def test_sim_search_by_vector (metric , tmp_path ) -> None :
5578 """Test end to end construction and similarity search by vector."""
5679 texts = ["foo" , "bar" , "baz" ]
@@ -59,14 +82,15 @@ def test_sim_search_by_vector(metric, tmp_path) -> None:
5982 FakeEmbeddings (),
6083 work_dir = str (tmp_path ),
6184 n_dim = 10 ,
85+ dist_metric = metric ,
6286 )
6387 embedding = [1.0 ] * 10
6488 output = hnswlib_vec_store .similarity_search_by_vector (embedding , k = 1 )
6589
6690 assert output == [Document (page_content = "bar" )]
6791
6892
69- @pytest .mark .parametrize ('metric' , ['cosine' , 'ip' , ' l2' ])
93+ @pytest .mark .parametrize ('metric' , ['cosine' , 'l2' ])
7094def test_sim_search_with_score (metric , tmp_path ) -> None :
7195 """Test end to end construction and similarity search with score."""
7296 texts = ["foo" , "bar" , "baz" ]
@@ -75,6 +99,7 @@ def test_sim_search_with_score(metric, tmp_path) -> None:
7599 FakeEmbeddings (),
76100 work_dir = str (tmp_path ),
77101 n_dim = 10 ,
102+ dist_metric = metric ,
78103 )
79104 output = hnswlib_vec_store .similarity_search_with_score ("foo" , k = 1 )
80105 assert len (output ) == 1
@@ -84,6 +109,26 @@ def test_sim_search_with_score(metric, tmp_path) -> None:
84109 assert np .isclose (out_score , 0.0 , atol = 1.e-6 )
85110
86111
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+
87132@pytest .mark .parametrize ('metric' , ['cosine' , 'l2' ])
88133def test_max_marginal_relevance_search (metric , tmp_path ) -> None :
89134 """Test MRR search."""
0 commit comments