From 7cd7e967d5832769834d05c4110db407a2acbfd0 Mon Sep 17 00:00:00 2001 From: James Briggs <35938317+jamescalam@users.noreply.github.com> Date: Thu, 13 Feb 2025 17:29:42 +0400 Subject: [PATCH] chore: lint --- semantic_router/index/base.py | 12 ++++++++++++ semantic_router/index/pinecone.py | 26 +++++++++++++++++++++++--- semantic_router/routers/base.py | 2 +- 3 files changed, 36 insertions(+), 4 deletions(-) diff --git a/semantic_router/index/base.py b/semantic_router/index/base.py index d86e5350..3d6a3afa 100644 --- a/semantic_router/index/base.py +++ b/semantic_router/index/base.py @@ -191,6 +191,18 @@ class BaseIndex(BaseModel): """ raise NotImplementedError("This method should be implemented by subclasses.") + async def adelete(self, route_name: str) -> list[str]: + """Asynchronously delete specified route from index if it exists. Returns the IDs + of the vectors deleted. + This method should be implemented by subclasses. + + :param route_name: Name of the route to delete. + :type route_name: str + :return: List of IDs of the vectors deleted. + :rtype: list[str] + """ + raise NotImplementedError("This method should be implemented by subclasses.") + def describe(self) -> IndexConfig: """Returns an IndexConfig object with index details such as type, dimensions, and total vector count. diff --git a/semantic_router/index/pinecone.py b/semantic_router/index/pinecone.py index 368e18eb..96ce6788 100644 --- a/semantic_router/index/pinecone.py +++ b/semantic_router/index/pinecone.py @@ -659,11 +659,14 @@ class PineconeIndex(BaseIndex): return all_vector_ids, metadata - def delete(self, route_name: str): - """Delete specified route from index if it exists. + def delete(self, route_name: str) -> list[str]: + """Delete specified route from index if it exists. Returns the IDs of the vectors + deleted. :param route_name: Name of the route to delete. :type route_name: str + :return: List of IDs of the vectors deleted. + :rtype: list[str] """ route_vec_ids = self._get_route_ids(route_name=route_name) if self.index is not None: @@ -690,6 +693,23 @@ class PineconeIndex(BaseIndex): raise Exception( f"Failed to delete vectors: {response.status_code} : {error_message}" ) + return route_vec_ids + else: + raise ValueError("Index is None, could not delete.") + + async def adelete(self, route_name: str) -> list[str]: + """Asynchronously delete specified route from index if it exists. Returns the IDs + of the vectors deleted. + + :param route_name: Name of the route to delete. + :type route_name: str + :return: List of IDs of the vectors deleted. + :rtype: list[str] + """ + route_vec_ids = await self._async_get_route_ids(route_name=route_name) + if self.index is not None: + await self._async_delete(ids=route_vec_ids, namespace=self.namespace or "") + return route_vec_ids else: raise ValueError("Index is None, could not delete.") @@ -1041,7 +1061,7 @@ class PineconeIndex(BaseIndex): except JSONDecodeError as e: logger.error(f"JSON decode error: {e}") return {} - + async def _is_async_ready(self, client_only: bool = False) -> bool: """Checks if class attributes exist to be used for async operations. diff --git a/semantic_router/routers/base.py b/semantic_router/routers/base.py index 34bfbce6..3d5c18af 100644 --- a/semantic_router/routers/base.py +++ b/semantic_router/routers/base.py @@ -1121,7 +1121,7 @@ class BaseRouter(BaseModel): # ensure index is not locked if await self.index._ais_locked(): raise ValueError("Index is locked. Cannot delete route.") - current_local_hash = await self._async_get_hash() + current_local_hash = self._get_hash() current_remote_hash = await self.index._async_read_hash() if current_remote_hash.value == "": # if remote hash is empty, the index is to be initialized -- GitLab