From 5c4c12a499dad0e356d0df8bca34a9e481277924 Mon Sep 17 00:00:00 2001 From: ch4r1ty <920853279@qq.com> Date: Tue, 11 Mar 2025 19:56:05 -0400 Subject: [PATCH] Refactor: Abstract collection existence check into a utility function - Moved the logic for checking if a collection exists into a shared utility function `collection_exists`. - Updated `QdrantStorage` class to use the new utility function for checking collection existence. - This change reduces code duplication and improves maintainability across the codebase. --- camel/storages/vectordb_storages/qdrant.py | 8 +++----- camel/storages/vectordb_storages/utils.py | 6 ++++++ 2 files changed, 9 insertions(+), 5 deletions(-) create mode 100644 camel/storages/vectordb_storages/utils.py diff --git a/camel/storages/vectordb_storages/qdrant.py b/camel/storages/vectordb_storages/qdrant.py index 12a66b236d..a00a5d25c2 100644 --- a/camel/storages/vectordb_storages/qdrant.py +++ b/camel/storages/vectordb_storages/qdrant.py @@ -27,6 +27,7 @@ ) from camel.types import VectorDistance from camel.utils import dependencies_required +from .utils import collection_exists _qdrant_local_client_map: Dict[str, Tuple[Any, int]] = {} logger = logging.getLogger(__name__) @@ -218,11 +219,8 @@ def _delete_collection( ) def _collection_exists(self, collection_name: str) -> bool: - r"""Returns wether the collection exists in the database""" - for c in self._client.get_collections().collections: - if collection_name == c.name: - return True - return False + """Returns whether the collection exists in the database.""" + return collection_exists(self._client, collection_name) def _generate_collection_name(self) -> str: r"""Generates a collection name if user doesn't provide""" diff --git a/camel/storages/vectordb_storages/utils.py b/camel/storages/vectordb_storages/utils.py new file mode 100644 index 0000000000..61b564e765 --- /dev/null +++ b/camel/storages/vectordb_storages/utils.py @@ -0,0 +1,6 @@ +def collection_exists(client, collection_name: str) -> bool: + """Check if a collection exists in the database.""" + for c in client.get_collections().collections: + if collection_name == c.name: + return True + return False \ No newline at end of file