From 9924b9c89c8d86cf69c49a2001a6fc7089251b81 Mon Sep 17 00:00:00 2001 From: James Briggs <james.briggs@hotmail.com> Date: Mon, 11 Nov 2024 16:06:02 +0100 Subject: [PATCH] fix: remove error from empty local index --- semantic_router/index/local.py | 2 +- tests/unit/test_layer.py | 34 +++++++++++++++++----------------- tests/unit/test_sync.py | 4 ++-- 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/semantic_router/index/local.py b/semantic_router/index/local.py index 62174ed1..00210613 100644 --- a/semantic_router/index/local.py +++ b/semantic_router/index/local.py @@ -69,7 +69,7 @@ class LocalIndex(BaseIndex): List[Tuple]: A list of (route_name, utterance) objects. """ if self.routes is None or self.utterances is None: - raise ValueError("No routes have been added to the index.") + return [] return list(zip(self.routes, self.utterances)) def describe(self) -> Dict: diff --git a/tests/unit/test_layer.py b/tests/unit/test_layer.py index f3963945..7308191e 100644 --- a/tests/unit/test_layer.py +++ b/tests/unit/test_layer.py @@ -226,7 +226,7 @@ class TestRouteLayer: def test_initialization_dynamic_route( self, cohere_encoder, openai_encoder, dynamic_routes, index_cls ): - index = init_index(index_cls) + index = init_index(index_cls, sync="local") route_layer_cohere = RouteLayer( encoder=cohere_encoder, routes=dynamic_routes, index=index ) @@ -238,13 +238,13 @@ class TestRouteLayer: assert route_layer_openai.score_threshold == 0.3 def test_add_route(self, routes, openai_encoder, index_cls): - index = init_index(index_cls) + index = init_index(index_cls, sync="local") route_layer = RouteLayer(encoder=openai_encoder, index=index) - # Initially, the routes list should be empty + # Initially, the local routes list should be empty assert route_layer.routes == [] - if index_cls is PineconeIndex: - time.sleep(PINECONE_SLEEP) # allow for index to be populated + # same for the remote index + assert route_layer.index.get_utterances() == [] # Add route1 and check route_layer.add(route=routes[0]) @@ -262,7 +262,7 @@ class TestRouteLayer: assert len(route_layer.index.get_utterances()) == 5 def test_list_route_names(self, openai_encoder, routes, index_cls): - index = init_index(index_cls) + index = init_index(index_cls, sync="local") route_layer = RouteLayer(encoder=openai_encoder, routes=routes, index=index) route_names = route_layer.list_route_names() assert set(route_names) == { @@ -270,7 +270,7 @@ class TestRouteLayer: }, "The list of route names should match the names of the routes added." def test_delete_route(self, openai_encoder, routes, index_cls): - index = init_index(index_cls) + index = init_index(index_cls, sync="local") route_layer = RouteLayer(encoder=openai_encoder, routes=routes, index=index) # Delete a route by name route_to_delete = routes[0].name @@ -298,7 +298,7 @@ class TestRouteLayer: # we should see warning in logs only (ie no errors) def test_add_multiple_routes(self, openai_encoder, routes, index_cls): - index = init_index(index_cls) + index = init_index(index_cls, sync="local") route_layer = RouteLayer(encoder=openai_encoder, index=index) if index_cls is PineconeIndex: time.sleep(PINECONE_SLEEP) @@ -309,7 +309,7 @@ class TestRouteLayer: assert len(route_layer.index.get_utterances()) == 5 def test_query_and_classification(self, openai_encoder, routes, index_cls): - index = init_index(index_cls, dimensions=3) + index = init_index(index_cls, dimensions=3, sync="local") route_layer = RouteLayer(encoder=openai_encoder, routes=routes, index=index) if index_cls is PineconeIndex: time.sleep(PINECONE_SLEEP) # allow for index to be populated @@ -317,7 +317,7 @@ class TestRouteLayer: assert query_result in ["Route 1", "Route 2"] def test_query_filter(self, openai_encoder, routes, index_cls): - index = init_index(index_cls, dimensions=3) + index = init_index(index_cls, dimensions=3, sync="local") route_layer = RouteLayer(encoder=openai_encoder, routes=routes, index=index) if index_cls is PineconeIndex: @@ -336,7 +336,7 @@ class TestRouteLayer: ) def test_query_filter_pinecone(self, openai_encoder, routes, index_cls): if index_cls is PineconeIndex: - pineconeindex = init_index(index_cls, dimensions=3) + pineconeindex = init_index(index_cls, dimensions=3, sync="local") route_layer = RouteLayer( encoder=openai_encoder, routes=routes, index=pineconeindex ) @@ -448,7 +448,7 @@ class TestRouteLayer: assert route_layer(text="Anything").name is None def test_query_with_vector(self, openai_encoder, routes, index_cls): - index = init_index(index_cls) + index = init_index(index_cls, sync="local") route_layer = RouteLayer(encoder=openai_encoder, routes=routes, index=index) if index_cls is PineconeIndex: time.sleep(PINECONE_SLEEP) # allow for index to be populated @@ -463,7 +463,7 @@ class TestRouteLayer: route_layer() def test_semantic_classify(self, openai_encoder, routes, index_cls): - index = init_index(index_cls) + index = init_index(index_cls, sync="local") route_layer = RouteLayer(encoder=openai_encoder, routes=routes, index=index) classification, score = route_layer._semantic_classify( [ @@ -475,7 +475,7 @@ class TestRouteLayer: assert score == [0.9] def test_semantic_classify_multiple_routes(self, openai_encoder, routes, index_cls): - index = init_index(index_cls) + index = init_index(index_cls, sync="local") route_layer = RouteLayer(encoder=openai_encoder, routes=routes, index=index) classification, score = route_layer._semantic_classify( [ @@ -499,13 +499,13 @@ class TestRouteLayer: route_layer(vector=vector) def test_pass_threshold(self, openai_encoder, index_cls): - index = init_index(index_cls) + index = init_index(index_cls, sync="local") route_layer = RouteLayer(encoder=openai_encoder, index=index) assert not route_layer._pass_threshold([], 0.3) assert route_layer._pass_threshold([0.6, 0.7], 0.3) def test_failover_score_threshold(self, openai_encoder, index_cls): - index = init_index(index_cls) + index = init_index(index_cls, sync="local") route_layer = RouteLayer(encoder=openai_encoder, index=index) assert route_layer.score_threshold == 0.3 @@ -515,7 +515,7 @@ class TestRouteLayer: temp_path = temp.name # Save the temporary file's path temp.close() # Close the file to ensure it can be opened again on Windows os.environ["OPENAI_API_KEY"] = "test_api_key" - index = init_index(index_cls) + index = init_index(index_cls, sync="local") route_layer = RouteLayer(encoder=openai_encoder, routes=routes, index=index) route_layer.to_json(temp_path) assert os.path.exists(temp_path) diff --git a/tests/unit/test_sync.py b/tests/unit/test_sync.py index e1d9d8e8..7c89f143 100644 --- a/tests/unit/test_sync.py +++ b/tests/unit/test_sync.py @@ -211,8 +211,8 @@ class TestRouteLayer: self, openai_encoder, routes, routes_2, index_cls ): index = init_index(index_cls, sync=None) - route_layer = RouteLayer(encoder=openai_encoder, routes=routes, index=index) - _ = RouteLayer(encoder=openai_encoder, routes=routes_2, index=index) + _ = RouteLayer(encoder=openai_encoder, routes=routes, index=index) + route_layer = RouteLayer(encoder=openai_encoder, routes=routes_2, index=index) if index_cls is PineconeIndex: time.sleep(PINECONE_SLEEP) # allow for index to be populated assert route_layer.is_synced() is False -- GitLab