From 2f5621a90e5a38f314ec72a9644d6564e0e30ded Mon Sep 17 00:00:00 2001 From: Anush Date: Wed, 17 Jul 2024 21:00:02 +0530 Subject: [PATCH] chore: Added init_options property (#541) * chore: Added init_options property * refactor: @property * test: init_opts * chore: regen async client again with Py 3.10 * chore: deepcopy kwargs --- qdrant_client/async_qdrant_client.py | 16 ++++++++++++++++ qdrant_client/qdrant_client.py | 19 +++++++++++++++++++ tests/test_qdrant_client.py | 7 +++++++ 3 files changed, 42 insertions(+) diff --git a/qdrant_client/async_qdrant_client.py b/qdrant_client/async_qdrant_client.py index ca6d9da5..4109cb3b 100644 --- a/qdrant_client/async_qdrant_client.py +++ b/qdrant_client/async_qdrant_client.py @@ -10,6 +10,7 @@ # ****** WARNING: THIS FILE IS AUTOGENERATED ****** import warnings +from copy import deepcopy from typing import ( Any, Awaitable, @@ -105,6 +106,12 @@ def __init__( **kwargs: Any, ): super().__init__(**kwargs) + self._init_options = { + key: value + for (key, value) in locals().items() + if key not in ("self", "__class__", "kwargs") + } + self._init_options.update(deepcopy(kwargs)) self._client: AsyncQdrantBase if sum([param is not None for param in (location, url, host, path)]) > 1: raise ValueError( @@ -189,6 +196,15 @@ def http(self) -> AsyncApis[AsyncApiClient]: return self._client.http raise NotImplementedError(f"REST client is not supported for {type(self._client)}") + @property + def init_options(self) -> Dict[str, Any]: + """`__init__` Options + + Returns: + A dictionary of options the client class was instantiated with + """ + return self._init_options + async def search_batch( self, collection_name: str, diff --git a/qdrant_client/qdrant_client.py b/qdrant_client/qdrant_client.py index 5f432d00..a07bcdb1 100644 --- a/qdrant_client/qdrant_client.py +++ b/qdrant_client/qdrant_client.py @@ -1,4 +1,5 @@ import warnings +from copy import deepcopy from typing import ( Any, Awaitable, @@ -101,6 +102,15 @@ def __init__( # we will need to pop them from **kwargs. Otherwise, they might be passed to QdrantRemote as httpx kwargs. # Httpx has specific set of params, which it accepts and will raise an error if it receives any other params. + # Saving the init options to facilitate building AsyncQdrantClient from QdrantClient and vice versa. + # Eg. AsyncQdrantClient(**sync_client.init_options) or QdrantClient(**async_client.init_options) + self._init_options = { + key: value + for key, value in locals().items() + if key not in ("self", "__class__", "kwargs") + } + self._init_options.update(deepcopy(kwargs)) + self._client: QdrantBase if sum([param is not None for param in (location, url, host, path)]) > 1: @@ -220,6 +230,15 @@ def http(self) -> SyncApis[ApiClient]: raise NotImplementedError(f"REST client is not supported for {type(self._client)}") + @property + def init_options(self) -> Dict[str, Any]: + """`__init__` Options + + Returns: + A dictionary of options the client class was instantiated with + """ + return self._init_options + def search_batch( self, collection_name: str, diff --git a/tests/test_qdrant_client.py b/tests/test_qdrant_client.py index 21d61aee..4ee24278 100644 --- a/tests/test_qdrant_client.py +++ b/tests/test_qdrant_client.py @@ -174,6 +174,13 @@ def test_client_init(): ): QdrantClient(**params) + client = QdrantClient( + url="http://localhost:6333", prefix="custom", metadata={"some-rest-meta": "some-value"} + ) + assert client.init_options["url"] == "http://localhost:6333" + assert client.init_options["prefix"] == "custom" + assert client.init_options["metadata"] == {"some-rest-meta": "some-value"} + @pytest.mark.parametrize("prefer_grpc", [False, True]) @pytest.mark.parametrize("parallel", [1, 2])