Files
qdrant-client/qdrant_client/embed/embed_inspector.py
George 9e61a69652 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-12 17:21:08 +07:00

177 lines
6.6 KiB
Python

from copy import copy
from typing import Iterable, get_args
from pydantic import BaseModel
from qdrant_client._pydantic_compat import model_fields_set
from qdrant_client.embed.common import INFERENCE_OBJECT_TYPES
from qdrant_client.embed.schema_parser import ModelSchemaParser
from qdrant_client.embed.utils import convert_paths, FieldPath
class InspectorEmbed:
"""Inspector which collects paths to objects requiring inference in the received models
Attributes:
parser: ModelSchemaParser instance
"""
def __init__(self, parser: ModelSchemaParser | None = None) -> None:
self.parser = ModelSchemaParser() if parser is None else parser
def inspect(self, points: Iterable[BaseModel] | BaseModel) -> list[FieldPath]:
"""Looks for all the paths to objects requiring inference in the received models
Args:
points: models to inspect
Returns:
list of FieldPath objects
"""
paths = []
if isinstance(points, BaseModel):
self.parser.parse_model(points.__class__)
paths.extend(self._inspect_model(points))
elif isinstance(points, dict):
for value in points.values():
paths.extend(self.inspect(value))
elif isinstance(points, Iterable):
for point in points:
if isinstance(point, BaseModel):
self.parser.parse_model(point.__class__)
paths.extend(self._inspect_model(point))
paths = sorted(set(paths))
return convert_paths(paths)
def _inspect_model(
self, mod: BaseModel, paths: list[FieldPath] | None = None, accum: str | None = None
) -> list[str]:
"""Looks for all the paths to objects requiring inference in the received model
Args:
mod: model to inspect
paths: list of paths to the fields possibly containing objects for inference
accum: accumulator for the path. Path is a dot separated string of field names which we assemble recursively
Returns:
list of paths to the model fields containing objects for inference
"""
paths = self.parser.path_cache.get(mod.__class__.__name__, []) if paths is None else paths
found_paths = []
for path in paths:
found_paths.extend(
self._inspect_inner_models(
mod, path.current, path.tail if path.tail else [], accum
)
)
return found_paths
def _inspect_inner_models(
self,
original_model: BaseModel,
current_path: str,
tail: list[FieldPath],
accum: str | None = None,
) -> list[str]:
"""Looks for all the paths to objects requiring inference in the received model
Args:
original_model: model to inspect
current_path: the field to inspect on the current iteration
tail: list of FieldPath objects to the fields possibly containing objects for inference
accum: accumulator for the path. Path is a dot separated string of field names which we assemble recursively
Returns:
list of paths to the model fields containing objects for inference
"""
found_paths = []
if accum is None:
accum = current_path
else:
accum += f".{current_path}"
def inspect_recursive(member: BaseModel, accumulator: str) -> list[str]:
"""Iterates over the set model fields, expand recursive ones and find paths to objects requiring inference
Args:
member: currently inspected model, which may or may not contain recursive fields
accumulator: accumulator for the path, which is a dot separated string assembled recursively
"""
recursive_paths = []
for field in model_fields_set(member):
if field in self.parser.name_recursive_ref_mapping:
mapped_field = self.parser.name_recursive_ref_mapping[field]
recursive_paths.extend(self.parser.path_cache[mapped_field])
return self._inspect_model(member, copy(recursive_paths), accumulator)
model = getattr(original_model, current_path, None)
if model is None:
return []
if isinstance(model, get_args(INFERENCE_OBJECT_TYPES)):
return [accum]
if isinstance(model, BaseModel):
found_paths.extend(inspect_recursive(model, accum))
for next_path in tail:
found_paths.extend(
self._inspect_inner_models(
model, next_path.current, next_path.tail if next_path.tail else [], accum
)
)
return found_paths
elif isinstance(model, list):
for current_model in model:
if not isinstance(current_model, BaseModel):
continue
if isinstance(current_model, get_args(INFERENCE_OBJECT_TYPES)):
found_paths.append(accum)
found_paths.extend(inspect_recursive(current_model, accum))
for next_path in tail:
for current_model in model:
found_paths.extend(
self._inspect_inner_models(
current_model,
next_path.current,
next_path.tail if next_path.tail else [],
accum,
)
)
return found_paths
elif isinstance(model, dict):
found_paths = []
for key, values in model.items():
values = [values] if not isinstance(values, list) else values
for current_model in values:
if not isinstance(current_model, BaseModel):
continue
if isinstance(current_model, get_args(INFERENCE_OBJECT_TYPES)):
found_paths.append(accum)
found_paths.extend(inspect_recursive(current_model, accum))
for next_path in tail:
for current_model in values:
found_paths.extend(
self._inspect_inner_models(
current_model,
next_path.current,
next_path.tail if next_path.tail else [],
accum,
)
)
return found_paths