Files
qdrant-client/tools/async_client_generator/remote_generator.py
George a8beff7c96 Drop python3.9 (#1110)
* new: remove vectors_count, update http and grpc models

* fix: update inspection cache

* new: add conversions and update interface

* fix: fix some conversions

* fix: fix typo

* fix: fix isinstance

* fix: regen async

* fix: fix update_filter usage, fix isinstance

* tests: collection metadata test

* fix: address backward compatibility in test

* new: update models, add max payload index count and copy vectors

* fix; update _inspection_cache

* new: add read consistency to count points

* Allow uuids in interface (#1085)

* new: direct uuid support

* tests: add uuid tests

* fix: update inspection cache

* new: add collection metadata and tests to local mode (#1089)

* new: add collection metadata and tests to local mode

* fix: regen async client

* new: implement parametrized rrf in local mode (#1087)

* new: implement parametrized rrf in local mode

* refactoring: use a variable for a magic value

* fix: adjust conversion according to AI

* Update filter (#1090)

* new: add missing update_filter, implement it in local mode

* fix: fix type hint, fix update operation, fix rest uploader, add tests

* fix: fix update filter is None case

* fix: mypy was not a good boy

* Text any filter (#1091)

* new: add match text any local mode

* tests: add match text any tests

* new: update models, remove init_from and locks (#1100)

* new: update models, remove init_from and locks

* deprecate: remove init from tests

* deprecate: remove lock tests

* new: convert ascii_folding

* fix: fix type stub

* new: convert acorn

* new: convert shard key with fallback

* new: update grpcio and grpcio tools in generator (#1106)

* new: update grpcio and grpcio tools in generator

* fix: bind grpcio and tools versions to 1.62.0 in generator

* Remove deprecated methods (#1103)

* deprecate: remove old api methods

* deprecate: remove type stub for removed methods

* deprecate: remove old api methods from test_qdrant_client

* deprecate: replace search with query points in test_in_memory

* deprecate: replace search methods in fastembed mixin with query points

* deprecate: replace old api methods in test async qdrant client

* deprecate: replace search with query points in test delete points

* deprecate: replace discover and context with query points in test_discovery

* deprecate: replace recommend_groups with query_points_groups in test_group_recommend

* deprecate: replace search_groups in test_group_search

* deprecate: replace recommend with query points in test_recommendation

* deprecate: replace search with query points in test search

* deprecate: replace context and discover with query points in test sparse discovery

* deprecate: replace search with query points in test sparse idf search

* deprecate: replace recommend with query points in test sparse recommend

* deprecate: replace search with query points in test sparse search

* deprecate: replace missing search request with query request in qdrant_fastembed

* deprecate: replace search with query points in test multivector search queries

* deprecate: replace upload records with upload points in test_updates

* deprecate: remove redundant structs (#1104)

* deprecate: remove redundant structs

* fix: do not use removed conversions in local mode

* fix: remove redundant conversions, simplify types.QueryRequest

* deprecate: replace old style grpc vector conversion to a new one (#1105)

* deprecate: replace old style grpc vector conversion to a new one

* fix: ignore union attr in conversion

* review fixes

---------

Co-authored-by: generall <andrey@vasnetsov.com>

---------

Co-authored-by: generall <andrey@vasnetsov.com>

---------

Co-authored-by: generall <andrey@vasnetsov.com>

* new: deprecate add, query, query_batch in fastembed mixin (#1102)

* new: deprecate add, query, query_batch in fastembed mixin

* 1.16 -> 1.17

---------

Co-authored-by: generall <andrey@vasnetsov.com>

---------

Co-authored-by: generall <andrey@vasnetsov.com>

* new: yet another update

* new: add initial_state to create shard key (#1109)

* new: drop python3.9, replace union and optional with | where possible

* fix: fix missing type hints, regen async

* fix: remove redundant optional

* fix: fix ai comments

* fix: update type hints from merge

* new: update pyproject and lock

* new: replace optional and union with |

* new: remove optional and union from qdrant local

* new: replace union with | in client classes

* fix: replace remaining union, optional, etc, address review comments

* new: adjust numpy versioning

---------

Co-authored-by: generall <andrey@vasnetsov.com>
2025-12-05 16:32:24 +07:00

146 lines
5.2 KiB
Python

import ast
import inspect
from qdrant_client.grpc import CollectionsStub, PointsStub, SnapshotsStub, QdrantStub
from qdrant_client.http import AsyncApiClient
from qdrant_client.http.api.distributed_api import AsyncDistributedApi
from qdrant_client.http.api.aliases_api import AsyncAliasesApi
from qdrant_client.http.api.indexes_api import AsyncIndexesApi
from qdrant_client.http.api.search_api import AsyncSearchApi
from qdrant_client.http.api.collections_api import AsyncCollectionsApi
from qdrant_client.http.api.points_api import AsyncPointsApi
from qdrant_client.http.api.service_api import AsyncServiceApi
from qdrant_client.http.api.snapshots_api import AsyncSnapshotsApi
from tools.async_client_generator.async_client_base import AsyncQdrantBase
from tools.async_client_generator.base_generator import BaseGenerator
from tools.async_client_generator.transformers import (
CallTransformer,
ClassDefTransformer,
ImportTransformer,
NameTransformer,
)
from tools.async_client_generator.transformers.remote import (
RemoteFunctionDefTransformer,
RemoteImportFromTransformer,
)
class RemoteGenerator(BaseGenerator):
def __init__(
self,
keep_sync: list[str] | None = None,
class_replace_map: dict | None = None,
import_replace_map: dict | None = None,
exclude_methods: list[str] | None = None,
rename_methods: dict[str, str] | None = None,
):
super().__init__()
self._async_methods: list[str] | None = None
self.transformers.append(
RemoteImportFromTransformer(import_replace_map=import_replace_map)
)
self.transformers.append(ClassDefTransformer(class_replace_map=class_replace_map))
self.transformers.append(
CallTransformer(class_replace_map=class_replace_map, async_methods=self.async_methods)
)
self.transformers.append(ImportTransformer(import_replace_map=import_replace_map))
self.transformers.append(
RemoteFunctionDefTransformer(
keep_sync=keep_sync,
exclude_methods=exclude_methods,
async_methods=self.async_methods,
)
)
self.transformers.append(
NameTransformer(
class_replace_map=class_replace_map,
import_replace_map=import_replace_map,
rename_methods=rename_methods,
)
)
@staticmethod
def _get_grpc_methods(grpc_stub_class: type) -> list[str]:
init_source = inspect.getsource(grpc_stub_class)
# Parse the source code using ast
parsed = ast.parse(init_source)
# Extract attribute names
field_names = []
for node in ast.walk(parsed):
if isinstance(node, ast.Assign):
for target in node.targets:
if (
isinstance(target, ast.Attribute)
and isinstance(target.value, ast.Name)
and target.value.id == "self"
):
field_name = target.attr
field_names.append(field_name)
return field_names
@property
def async_methods(self) -> list[str]:
if self._async_methods is None:
self._async_methods = []
for cls_ in (
AsyncQdrantBase,
AsyncDistributedApi,
AsyncCollectionsApi,
AsyncPointsApi,
AsyncServiceApi,
AsyncSnapshotsApi,
AsyncIndexesApi,
AsyncAliasesApi,
AsyncSearchApi,
AsyncApiClient,
):
self._async_methods.extend(self.get_async_methods(cls_))
for cls_ in (PointsStub, SnapshotsStub, CollectionsStub, QdrantStub):
self._async_methods.extend(self._get_grpc_methods(cls_))
return self._async_methods
@staticmethod
def get_async_methods(class_obj: type) -> list[str]:
async_methods = []
for name, method in inspect.getmembers(class_obj):
if inspect.iscoroutinefunction(method):
async_methods.append(name)
return async_methods
if __name__ == "__main__":
from tools.async_client_generator.config import CLIENT_DIR, CODE_DIR
with open(CLIENT_DIR / "qdrant_remote.py", "r") as source_file:
code = source_file.read()
generator = RemoteGenerator(
class_replace_map={
"QdrantBase": "AsyncQdrantBase",
"QdrantFastembedMixin": "AsyncQdrantFastembedMixin",
"QdrantClient": "AsyncQdrantClient",
"QdrantRemote": "AsyncQdrantRemote",
},
import_replace_map={
"qdrant_client.client_base": "qdrant_client.async_client_base",
"QdrantBase": "AsyncQdrantBase",
"QdrantRemote": "AsyncQdrantRemote",
"ApiClient": "AsyncApiClient",
"SyncApis": "AsyncApis",
},
exclude_methods=[
"__del__",
"migrate",
],
)
modified_code = generator.generate(code)
with open(CODE_DIR / "async_qdrant_remote.py", "w") as target_file:
target_file.write(modified_code)