Skip to content

Commit

Permalink
new: update generator launch script, update async files
Browse files Browse the repository at this point in the history
  • Loading branch information
joein committed Oct 12, 2023
1 parent ddbc915 commit f228567
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 76 deletions.
12 changes: 7 additions & 5 deletions qdrant_client/async_client_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ async def recommend_batch(
async def recommend(
self,
collection_name: str,
positive: Sequence[types.PointId],
negative: Optional[Sequence[types.PointId]] = None,
positive: Optional[Sequence[types.RecommendExample]] = None,
negative: Optional[Sequence[types.RecommendExample]] = None,
query_filter: Optional[types.Filter] = None,
search_params: Optional[types.SearchParams] = None,
limit: int = 10,
Expand All @@ -67,7 +67,8 @@ async def recommend(
with_vectors: Union[bool, List[str]] = False,
score_threshold: Optional[float] = None,
using: Optional[str] = None,
lookup_from: Optional[models.LookupLocation] = None,
lookup_from: Optional[types.LookupLocation] = None,
strategy: Optional[types.RecommendStrategy] = None,
**kwargs: Any,
) -> List[types.ScoredPoint]:
raise NotImplementedError()
Expand All @@ -76,8 +77,8 @@ async def recommend_groups(
self,
collection_name: str,
group_by: str,
positive: Sequence[types.PointId],
negative: Optional[Sequence[types.PointId]] = None,
positive: Optional[Sequence[types.RecommendExample]] = None,
negative: Optional[Sequence[types.RecommendExample]] = None,
query_filter: Optional[models.Filter] = None,
search_params: Optional[models.SearchParams] = None,
limit: int = 10,
Expand All @@ -88,6 +89,7 @@ async def recommend_groups(
using: Optional[str] = None,
lookup_from: Optional[models.LookupLocation] = None,
with_lookup: Optional[types.WithLookupInterface] = None,
strategy: Optional[types.RecommendStrategy] = None,
**kwargs: Any,
) -> types.GroupsResult:
raise NotImplementedError()
Expand Down
53 changes: 37 additions & 16 deletions qdrant_client/async_qdrant_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,8 +388,8 @@ async def recommend_batch(
async def recommend(
self,
collection_name: str,
positive: Sequence[types.PointId],
negative: Optional[Sequence[types.PointId]] = None,
positive: Optional[Sequence[types.RecommendExample]] = None,
negative: Optional[Sequence[types.RecommendExample]] = None,
query_filter: Optional[types.Filter] = None,
search_params: Optional[types.SearchParams] = None,
limit: int = 10,
Expand All @@ -399,6 +399,7 @@ async def recommend(
score_threshold: Optional[float] = None,
using: Optional[str] = None,
lookup_from: Optional[types.LookupLocation] = None,
strategy: Optional[types.RecommendStrategy] = None,
consistency: Optional[types.ReadConsistency] = None,
**kwargs: Any,
) -> List[types.ScoredPoint]:
Expand All @@ -410,14 +411,16 @@ async def recommend(
Args:
collection_name: Collection to search in
positive:
List of stored point IDs, which should be used as reference for similarity search.
If there is only one ID provided - this request is equivalent to the regular search with vector of that
List of stored point IDs or vectors, which should be used as reference for similarity search.
If there is only one example - this request is equivalent to the regular search with vector of that
point.
If there are more than one IDs, Qdrant will attempt to search for similar to all of them.
Recommendation for multiple vectors is experimental. Its behaviour may change in the future.
If there are more than one example, Qdrant will attempt to search for similar to all of them.
Recommendation for multiple vectors is experimental.
Its behaviour may change depending on selected strategy.
negative:
List of stored point IDs, which should be dissimilar to the search result.
Negative examples is an experimental functionality. Its behaviour may change in the future.
List of stored point IDs or vectors, which should be dissimilar to the search result.
Negative examples is an experimental functionality.
Its behaviour may change depending on selected strategy.
query_filter:
- Exclude vectors which doesn't fit given conditions.
- If `None` - search among all vectors
Expand Down Expand Up @@ -457,6 +460,12 @@ async def recommend(
- 'majority' - query all replicas, but return values present in the majority of replicas
- 'quorum' - query the majority of replicas, return values present in all of them
- 'all' - query all replicas, and return values present in all replicas
strategy:
Strategy to use for recommendation.
Strategy defines how to combine multiple examples into a recommendation query.
Possible values:
- 'average_vector' - calculates average vector of all examples and uses it for search
- 'best_score' - finds the result which is closer to positive examples and further from negative
Returns:
List of recommended points with similarity scores.
Expand All @@ -476,15 +485,16 @@ async def recommend(
using=using,
lookup_from=lookup_from,
consistency=consistency,
strategy=strategy,
**kwargs,
)

async def recommend_groups(
self,
collection_name: str,
group_by: str,
positive: Sequence[types.PointId],
negative: Optional[Sequence[types.PointId]] = None,
positive: Optional[Sequence[types.RecommendExample]] = None,
negative: Optional[Sequence[types.RecommendExample]] = None,
query_filter: Optional[types.Filter] = None,
search_params: Optional[types.SearchParams] = None,
limit: int = 10,
Expand All @@ -495,6 +505,7 @@ async def recommend_groups(
using: Optional[str] = None,
lookup_from: Optional[types.LookupLocation] = None,
with_lookup: Optional[types.WithLookupInterface] = None,
strategy: Optional[types.RecommendStrategy] = None,
consistency: Optional[types.ReadConsistency] = None,
**kwargs: Any,
) -> types.GroupsResult:
Expand All @@ -508,14 +519,16 @@ async def recommend_groups(
Args:
collection_name: Collection to search in
positive:
List of stored point IDs, which should be used as reference for similarity search.
If there is only one ID provided - this request is equivalent to the regular search with vector of that
List of stored point IDs or vectors, which should be used as reference for similarity search.
If there is only one example - this request is equivalent to the regular search with vector of that
point.
If there are more than one IDs, Qdrant will attempt to search for similar to all of them.
Recommendation for multiple vectors is experimental. Its behaviour may change in the future.
If there are more than one example, Qdrant will attempt to search for similar to all of them.
Recommendation for multiple vectors is experimental.
Its behaviour may change depending on selected strategy.
negative:
List of stored point IDs, which should be dissimilar to the search result.
Negative examples is an experimental functionality. Its behaviour may change in the future.
List of stored point IDs or vectors, which should be dissimilar to the search result.
Negative examples is an experimental functionality.
Its behaviour may change depending on selected strategy.
group_by: Name of the payload field to group by.
Field must be of type "keyword" or "integer".
Nested fields are specified using dot notation, e.g. "nested_field.subfield".
Expand Down Expand Up @@ -560,6 +573,12 @@ async def recommend_groups(
- 'majority' - query all replicas, but return values present in the majority of replicas
- 'quorum' - query the majority of replicas, return values present in all of them
- 'all' - query all replicas, and return values present in all replicas
strategy:
Strategy to use for recommendation.
Strategy defines how to combine multiple examples into a recommendation query.
Possible values:
- 'average_vector' - calculates average vector of all examples and uses it for search
- 'best_score' - finds the result which is closer to positive examples and further from negative
Returns:
List of groups with not more than `group_size` hits in each group.
Expand All @@ -583,6 +602,7 @@ async def recommend_groups(
lookup_from=lookup_from,
consistency=consistency,
with_lookup=with_lookup,
strategy=strategy,
**kwargs,
)

Expand Down Expand Up @@ -1409,6 +1429,7 @@ def upload_records(
parallel=parallel,
method=method,
max_retries=max_retries,
wait=wait,
**kwargs,
)

Expand Down
86 changes: 43 additions & 43 deletions qdrant_client/async_qdrant_remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -491,8 +491,8 @@ async def recommend_batch(
async def recommend(
self,
collection_name: str,
positive: Sequence[types.PointId],
negative: Optional[Sequence[types.PointId]] = None,
positive: Optional[Sequence[types.RecommendExample]] = None,
negative: Optional[Sequence[types.RecommendExample]] = None,
query_filter: Optional[types.Filter] = None,
search_params: Optional[types.SearchParams] = None,
limit: int = 10,
Expand All @@ -502,24 +502,19 @@ async def recommend(
score_threshold: Optional[float] = None,
using: Optional[str] = None,
lookup_from: Optional[types.LookupLocation] = None,
strategy: Optional[types.RecommendStrategy] = None,
consistency: Optional[types.ReadConsistency] = None,
**kwargs: Any,
) -> List[types.ScoredPoint]:
if positive is None:
positive = []
if negative is None:
negative = []
if self._prefer_grpc:
positive = [
RestToGrpc.convert_extended_point_id(point_id)
if isinstance(point_id, get_args_subscribed(models.ExtendedPointId))
else point_id
for point_id in positive
]
negative = [
RestToGrpc.convert_extended_point_id(point_id)
if isinstance(point_id, get_args_subscribed(models.ExtendedPointId))
else point_id
for point_id in negative
]
positive_ids = RestToGrpc.convert_recommend_examples_to_ids(positive)
positive_vectors = RestToGrpc.convert_recommend_examples_to_vectors(positive)
negative_ids = RestToGrpc.convert_recommend_examples_to_ids(negative)
negative_vectors = RestToGrpc.convert_recommend_examples_to_vectors(negative)
if isinstance(query_filter, models.Filter):
query_filter = RestToGrpc.convert_filter(model=query_filter)
if isinstance(search_params, models.SearchParams):
Expand All @@ -532,11 +527,13 @@ async def recommend(
lookup_from = RestToGrpc.convert_lookup_location(lookup_from)
if isinstance(consistency, get_args_subscribed(models.ReadConsistency)):
consistency = RestToGrpc.convert_read_consistency(consistency)
if isinstance(strategy, models.RecommendStrategy):
strategy = RestToGrpc.convert_recommend_strategy(strategy)
res: grpc.SearchResponse = await self.grpc_points.Recommend(
grpc.RecommendPoints(
collection_name=collection_name,
positive=positive,
negative=negative,
positive=positive_ids,
negative=negative_ids,
filter=query_filter,
limit=limit,
offset=offset,
Expand All @@ -547,22 +544,25 @@ async def recommend(
using=using,
lookup_from=lookup_from,
read_consistency=consistency,
strategy=strategy,
positive_vectors=positive_vectors,
negative_vectors=negative_vectors,
),
timeout=self._timeout,
)
return [GrpcToRest.convert_scored_point(hit) for hit in res.result]
else:
positive = [
GrpcToRest.convert_point_id(point_id)
if isinstance(point_id, grpc.PointId)
else point_id
for point_id in positive
GrpcToRest.convert_point_id(example)
if isinstance(example, grpc.PointId)
else example
for example in positive
]
negative = [
GrpcToRest.convert_point_id(point_id)
if isinstance(point_id, grpc.PointId)
else point_id
for point_id in negative
GrpcToRest.convert_point_id(example)
if isinstance(example, grpc.PointId)
else example
for example in negative
]
if isinstance(query_filter, grpc.Filter):
query_filter = GrpcToRest.convert_filter(model=query_filter)
Expand All @@ -588,6 +588,7 @@ async def recommend(
score_threshold=score_threshold,
lookup_from=lookup_from,
using=using,
strategy=strategy,
),
)
).result
Expand All @@ -598,8 +599,8 @@ async def recommend_groups(
self,
collection_name: str,
group_by: str,
positive: Sequence[types.PointId],
negative: Optional[Sequence[types.PointId]] = None,
positive: Optional[Sequence[Union[types.PointId, List[float]]]] = None,
negative: Optional[Sequence[Union[types.PointId, List[float]]]] = None,
query_filter: Optional[models.Filter] = None,
search_params: Optional[models.SearchParams] = None,
limit: int = 10,
Expand All @@ -610,28 +611,21 @@ async def recommend_groups(
using: Optional[str] = None,
lookup_from: Optional[models.LookupLocation] = None,
with_lookup: Optional[types.WithLookupInterface] = None,
consistency: Optional[models.ReadConsistencyType] = None,
strategy: Optional[types.RecommendStrategy] = None,
consistency: Optional[types.ReadConsistency] = None,
**kwargs: Any,
) -> types.GroupsResult:
if negative is None:
negative = []
positive = positive if positive is not None else []
negative = negative if negative is not None else []
if self._prefer_grpc:
if isinstance(with_lookup, models.WithLookup):
with_lookup = RestToGrpc.convert_with_lookup(with_lookup)
if isinstance(with_lookup, str):
with_lookup = grpc.WithLookup(lookup_index=with_lookup)
positive = [
RestToGrpc.convert_extended_point_id(point_id)
if isinstance(point_id, get_args_subscribed(models.ExtendedPointId))
else point_id
for point_id in positive
]
negative = [
RestToGrpc.convert_extended_point_id(point_id)
if isinstance(point_id, get_args_subscribed(models.ExtendedPointId))
else point_id
for point_id in negative
]
positive_ids = RestToGrpc.convert_recommend_examples_to_ids(positive)
positive_vectors = RestToGrpc.convert_recommend_examples_to_vectors(positive)
negative_ids = RestToGrpc.convert_recommend_examples_to_ids(negative)
negative_vectors = RestToGrpc.convert_recommend_examples_to_vectors(negative)
if isinstance(query_filter, models.Filter):
query_filter = RestToGrpc.convert_filter(model=query_filter)
if isinstance(search_params, models.SearchParams):
Expand All @@ -644,12 +638,14 @@ async def recommend_groups(
lookup_from = RestToGrpc.convert_lookup_location(lookup_from)
if isinstance(consistency, get_args_subscribed(models.ReadConsistency)):
consistency = RestToGrpc.convert_read_consistency(consistency)
if isinstance(strategy, models.RecommendStrategy):
strategy = RestToGrpc.convert_recommend_strategy(strategy)
res: grpc.GroupsResult = (
await self.grpc_points.RecommendGroups(
grpc.RecommendPointGroups(
collection_name=collection_name,
positive=positive,
negative=negative,
positive=positive_ids,
negative=negative_ids,
filter=query_filter,
group_by=group_by,
limit=limit,
Expand All @@ -662,6 +658,9 @@ async def recommend_groups(
lookup_from=lookup_from,
read_consistency=consistency,
with_lookup=with_lookup,
strategy=strategy,
positive_vectors=positive_vectors,
negative_vectors=negative_vectors,
),
timeout=self._timeout,
)
Expand Down Expand Up @@ -710,6 +709,7 @@ async def recommend_groups(
lookup_from=lookup_from,
using=using,
with_lookup=with_lookup,
strategy=strategy,
),
)
).result
Expand Down
2 changes: 1 addition & 1 deletion tools/async_client_generator/client_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import inspect
from typing import Optional

from qdrant_client.async_client_base import AsyncQdrantBase
from .async_client_base import AsyncQdrantBase


class AsyncAwaitTransformer(ast.NodeTransformer):
Expand Down
2 changes: 1 addition & 1 deletion tools/async_client_generator/fastembed_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import inspect
from typing import Optional

from qdrant_client.async_client_base import AsyncQdrantBase
from .async_client_base import AsyncQdrantBase


# Define a custom AST transformer to add 'await' before method calls
Expand Down
3 changes: 2 additions & 1 deletion tools/async_client_generator/qdrant_remote_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import inspect
from typing import Optional

from qdrant_client.async_client_base import AsyncQdrantBase
from qdrant_client.grpc import CollectionsStub, PointsStub, SnapshotsStub
from qdrant_client.http import AsyncApiClient
from qdrant_client.http.api.cluster_api import AsyncClusterApi
Expand All @@ -13,6 +12,8 @@
from qdrant_client.http.api.service_api import AsyncServiceApi
from qdrant_client.http.api.snapshots_api import AsyncSnapshotsApi

from .async_client_base import AsyncQdrantBase


class AsyncAwaitTransformer(ast.NodeTransformer):
def __init__(
Expand Down
Loading

0 comments on commit f228567

Please sign in to comment.