diff --git a/semantic_router/index/base.py b/semantic_router/index/base.py index d86e5350e124b93b7415c8faf707859ff722857e..3d6a3afad1ea3a7796df9ab7c2297afc314629f1 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 368e18eb0906348d067de000dc46d5dc7bb2895b..96ce6788d087d99d7d82374a42f8656cccbc2415 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 34bfbce6350cb4ecfef37e35fac3787367aeaca9..3d5c18af1231da201bbe3e61a40878d96f25b304 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