Files
qdrant-client/tests/congruence_tests/test_group_recommend.py
George ff7f584d33 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>
2025-11-11 21:17:08 +07:00

185 lines
6.7 KiB
Python

from qdrant_client.client_base import QdrantBase
from qdrant_client.http.models import models
from tests.congruence_tests.test_common import (
COLLECTION_NAME,
compare_client_results,
generate_fixtures,
init_client,
init_local,
init_remote,
)
from tests.fixtures.filters import one_random_filter_please
secondary_collection_name = "congruence_secondary_collection"
class TestGroupRecommendation:
__test__ = False
def __init__(self):
self.group_by = "rand_digit"
self.group_size = 1
def simple_recommend_groups_image(self, client: QdrantBase) -> models.GroupsResult:
return client.query_points_groups(
collection_name=COLLECTION_NAME,
query=models.RecommendQuery(
recommend=models.RecommendInput(positive=[10], negative=[])
),
with_payload=models.PayloadSelectorExclude(exclude=["city.geo", "rand_number"]),
limit=10,
using="image",
group_by=self.group_by,
group_size=self.group_size,
search_params=models.SearchParams(exact=True),
)
def simple_recommend_groups_best_scores(self, client: QdrantBase) -> models.GroupsResult:
return client.query_points_groups(
collection_name=COLLECTION_NAME,
query=models.RecommendQuery(
recommend=models.RecommendInput(
positive=[10],
negative=[],
strategy=models.RecommendStrategy.BEST_SCORE,
),
),
with_payload=models.PayloadSelectorExclude(exclude=["city.geo", "rand_number"]),
limit=10,
using="image",
group_by=self.group_by,
group_size=self.group_size,
search_params=models.SearchParams(exact=True),
)
def many_recommend_groups(self, client: QdrantBase) -> models.GroupsResult:
return client.query_points_groups(
collection_name=COLLECTION_NAME,
query=models.RecommendQuery(
recommend=models.RecommendInput(
positive=[10, 19], strategy=models.RecommendStrategy.SUM_SCORES
)
),
with_payload=models.PayloadSelectorExclude(exclude=["city.geo", "rand_number"]),
limit=10,
using="image",
group_by=self.group_by,
group_size=self.group_size,
search_params=models.SearchParams(exact=True),
)
def simple_recommend_groups_negative(self, client: QdrantBase) -> models.GroupsResult:
return client.query_points_groups(
collection_name=COLLECTION_NAME,
query=models.RecommendQuery(
recommend=models.RecommendInput(positive=[10], negative=[15, 7])
),
with_payload=models.PayloadSelectorExclude(exclude=["city.geo", "rand_number"]),
limit=10,
using="image",
group_by=self.group_by,
group_size=self.group_size,
search_params=models.SearchParams(exact=True),
)
def recommend_groups_from_another_collection(self, client: QdrantBase) -> models.GroupsResult:
return client.query_points_groups(
collection_name=COLLECTION_NAME,
query=models.RecommendQuery(
recommend=models.RecommendInput(positive=[10], negative=[15, 7])
),
with_payload=models.PayloadSelectorExclude(exclude=["city.geo", "rand_number"]),
limit=10,
using="image",
lookup_from=models.LookupLocation(
collection=secondary_collection_name,
vector="image",
),
group_by=self.group_by,
group_size=self.group_size,
search_params=models.SearchParams(exact=True),
)
def filter_recommend_groups_text(
self, client: QdrantBase, query_filter: models.Filter
) -> models.GroupsResult:
return client.query_points_groups(
collection_name=COLLECTION_NAME,
query=models.RecommendQuery(recommend=models.RecommendInput(positive=[10])),
query_filter=query_filter,
with_payload=models.PayloadSelectorExclude(exclude=["city.geo", "rand_number"]),
limit=10,
using="text",
group_by=self.group_by,
group_size=self.group_size,
search_params=models.SearchParams(exact=True),
)
def group_by_keys():
return ["id", "rand_digit", "two_words", "city.name", "maybe", "maybe_null"]
def test_simple_recommend_groups() -> None:
fixture_points = generate_fixtures()
secondary_collection_points = generate_fixtures(100)
recommender = TestGroupRecommendation()
local_client = init_local()
init_client(local_client, fixture_points)
init_client(local_client, secondary_collection_points, secondary_collection_name)
remote_client = init_remote()
init_client(remote_client, fixture_points)
init_client(remote_client, secondary_collection_points, secondary_collection_name)
for group_size in (3, 5):
recommender.group_size = group_size
compare_client_results(
local_client, remote_client, recommender.simple_recommend_groups_image
)
compare_client_results(
local_client, remote_client, recommender.simple_recommend_groups_best_scores
)
compare_client_results(local_client, remote_client, recommender.many_recommend_groups)
compare_client_results(
local_client, remote_client, recommender.simple_recommend_groups_negative
)
compare_client_results(
local_client,
remote_client,
recommender.recommend_groups_from_another_collection,
)
for key in group_by_keys():
recommender.group_by = key
compare_client_results(
local_client, remote_client, recommender.simple_recommend_groups_image
)
compare_client_results(local_client, remote_client, recommender.many_recommend_groups)
compare_client_results(
local_client, remote_client, recommender.simple_recommend_groups_negative
)
compare_client_results(
local_client,
remote_client,
recommender.recommend_groups_from_another_collection,
)
recommender.group_by = "rand_digit"
for i in range(10):
query_filter = one_random_filter_please()
try:
compare_client_results(
local_client,
remote_client,
recommender.filter_recommend_groups_text,
query_filter=query_filter,
)
except AssertionError as e:
print(f"\nFailed with filter {query_filter}")
raise e