-
Notifications
You must be signed in to change notification settings - Fork 116
add selection params #1997
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
robbespo00
wants to merge
9
commits into
dev/1.37
Choose a base branch
from
rob/diversity
base: dev/1.37
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
add selection params #1997
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
007c38a
add selection params
robbespo00 659bf28
Merge remote-tracking branch 'origin/main' into rob/diversity
robbespo00 5bc5470
Add integration tests
g-despot 4b44a5d
Merge branch 'dev/1.37' into rob/diversity
g-despot 7ab97c4
Fix stubs and proto version
g-despot b68160d
Add more tests
g-despot 6017fde
Rename _DiversityMMR to MMR
g-despot d3651a3
FIx test versions
g-despot ee1e781
Fix linter issue
g-despot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| import pytest | ||
|
|
||
| from integration.conftest import CollectionFactory | ||
| from weaviate.classes.query import Diversity | ||
| from weaviate.collections.classes.config import Configure, DataType, Property | ||
| from weaviate.collections.classes.data import DataObject | ||
|
|
||
|
|
||
| def _create_clustered_collection(collection_factory: CollectionFactory): | ||
| """Create a collection with 3 tight clusters (a, b, c) of vectors in 3D.""" | ||
| collection = collection_factory( | ||
| properties=[Property(name="text", data_type=DataType.TEXT)], | ||
| vectorizer_config=Configure.Vectorizer.none(), | ||
| ) | ||
| if collection._connection._weaviate_version.is_lower_than(1, 37, 0): | ||
| pytest.skip("Diversity selection requires Weaviate >= 1.37.0") | ||
| collection.data.insert_many( | ||
| [ | ||
| DataObject(properties={"text": "a1"}, vector=[1.0, 0.0, 0.0]), | ||
| DataObject(properties={"text": "a2"}, vector=[0.95, 0.05, 0.0]), | ||
| DataObject(properties={"text": "a3"}, vector=[0.9, 0.1, 0.0]), | ||
| DataObject(properties={"text": "b1"}, vector=[0.0, 1.0, 0.0]), | ||
| DataObject(properties={"text": "b2"}, vector=[0.05, 0.95, 0.0]), | ||
| DataObject(properties={"text": "c1"}, vector=[0.0, 0.0, 1.0]), | ||
| ] | ||
| ) | ||
| return collection | ||
|
|
||
|
|
||
| def test_near_vector_diversity_pure_relevance( | ||
| collection_factory: CollectionFactory, | ||
| ) -> None: | ||
| """balance=1.0 -> MMR degenerates to pure relevance (same as no diversity).""" | ||
| collection = _create_clustered_collection(collection_factory) | ||
|
|
||
| baseline = collection.query.near_vector(near_vector=[1.0, 0.0, 0.0], limit=3).objects | ||
| diverse = collection.query.near_vector( | ||
| near_vector=[1.0, 0.0, 0.0], | ||
| selection=Diversity.MMR(limit=3, balance=1.0), | ||
| ).objects | ||
|
|
||
| assert [o.properties["text"] for o in baseline] == [o.properties["text"] for o in diverse] | ||
|
|
||
|
|
||
| def test_near_vector_diversity_pure_diversity( | ||
| collection_factory: CollectionFactory, | ||
| ) -> None: | ||
| """balance=0.0 -> MMR picks maximally diverse results (one per cluster).""" | ||
| collection = _create_clustered_collection(collection_factory) | ||
|
|
||
| result = collection.query.near_vector( | ||
| near_vector=[1.0, 0.0, 0.0], | ||
| selection=Diversity.MMR(limit=3, balance=0.0), | ||
| ) | ||
| texts = {o.properties["text"] for o in result.objects} | ||
| assert len(texts) == 3 | ||
| # Pure diversity should pick one from each cluster (a*, b*, c*) | ||
| clusters = {t[0] for t in texts} | ||
| assert clusters == {"a", "b", "c"} | ||
|
|
||
|
|
||
| def test_near_object_diversity(collection_factory: CollectionFactory) -> None: | ||
| """near_object supports diversity selection.""" | ||
| collection = _create_clustered_collection(collection_factory) | ||
| anchor = next(iter(collection.query.fetch_objects().objects)).uuid | ||
|
|
||
| result = collection.query.near_object( | ||
| near_object=anchor, | ||
| selection=Diversity.MMR(limit=3, balance=0.0), | ||
| ) | ||
| assert len(result.objects) == 3 | ||
| clusters = {o.properties["text"][0] for o in result.objects} | ||
| assert len(clusters) == 3 | ||
|
|
||
|
|
||
| def test_diversity_cannot_be_instantiated() -> None: | ||
| """Diversity is a factory — direct instantiation should fail.""" | ||
| with pytest.raises(TypeError): | ||
| Diversity() | ||
|
|
||
|
|
||
| def test_diversity_mmr_only_limit(collection_factory: CollectionFactory) -> None: | ||
| """MMR accepts just a limit (balance defaults to server-side value).""" | ||
| collection = _create_clustered_collection(collection_factory) | ||
| result = collection.query.near_vector( | ||
| near_vector=[1.0, 0.0, 0.0], | ||
| selection=Diversity.MMR(limit=2), | ||
| ) | ||
| assert len(result.objects) == 2 | ||
|
|
||
|
|
||
| def test_near_text_diversity(collection_factory: CollectionFactory) -> None: | ||
| """near_text supports diversity selection via text2vec-contextionary.""" | ||
| collection = collection_factory( | ||
| properties=[Property(name="name", data_type=DataType.TEXT)], | ||
| vectorizer_config=Configure.Vectorizer.text2vec_contextionary( | ||
| vectorize_collection_name=False | ||
| ), | ||
| ) | ||
| if collection._connection._weaviate_version.is_lower_than(1, 37, 0): | ||
| pytest.skip("Diversity selection requires Weaviate >= 1.37.0") | ||
| for name in ["banana", "apple", "orange", "car", "truck", "bike"]: | ||
| collection.data.insert({"name": name}) | ||
|
|
||
| result = collection.query.near_text( | ||
| query="fruit", | ||
| selection=Diversity.MMR(limit=3, balance=0.0), | ||
| ) | ||
| assert len(result.objects) == 3 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -268,6 +268,28 @@ class Rerank(_WeaviateInput): | |
| query: Optional[str] = Field(default=None) | ||
|
|
||
|
|
||
| @dataclass | ||
| class MMR: | ||
| """Define MMR (Maximal Marginal Relevance) diversity selection. | ||
|
|
||
| Args: | ||
| limit: Optional number of candidates to consider for diversification. | ||
| balance: Optional MMR lambda in [0.0, 1.0] — 1.0 is pure relevance, 0.0 is pure diversity. | ||
| """ | ||
|
|
||
| limit: Optional[int] = None | ||
| balance: Optional[float] = None | ||
|
|
||
|
|
||
| class Diversity: | ||
| """Use this factory class to apply diversity selection to search results via MMR.""" | ||
|
|
||
| def __init__(self) -> None: | ||
| raise TypeError("Diversity cannot be instantiated directly. Use Diversity.MMR(...).") | ||
|
|
||
| MMR = MMR | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would add a factory here, eg something like |
||
|
|
||
|
|
||
| @dataclass | ||
| class BM25OperatorOptions: | ||
| # replace with ClassVar[base_search_pb2.SearchOperatorOptions.Operator] once python 3.10 is removed | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should match the parameter name