|
| 1 | +import pathlib |
| 2 | + |
1 | 3 | import pytest |
2 | 4 |
|
3 | 5 | from integration.conftest import CollectionFactory |
| 6 | +from integration.constants import WEAVIATE_LOGO_NEW_ENCODED |
4 | 7 | from weaviate.classes.query import Diversity |
5 | 8 | from weaviate.collections.classes.config import Configure, DataType, Property |
6 | 9 | from weaviate.collections.classes.data import DataObject |
@@ -87,3 +90,65 @@ def test_diversity_mmr_only_limit(collection_factory: CollectionFactory) -> None |
87 | 90 | selection=Diversity.MMR(limit=2), |
88 | 91 | ) |
89 | 92 | assert len(result.objects) == 2 |
| 93 | + |
| 94 | + |
| 95 | +def test_near_text_diversity(collection_factory: CollectionFactory) -> None: |
| 96 | + """near_text supports diversity selection via text2vec-contextionary.""" |
| 97 | + collection = collection_factory( |
| 98 | + properties=[Property(name="name", data_type=DataType.TEXT)], |
| 99 | + vectorizer_config=Configure.Vectorizer.text2vec_contextionary( |
| 100 | + vectorize_collection_name=False |
| 101 | + ), |
| 102 | + ) |
| 103 | + if collection._connection._weaviate_version.is_lower_than(1, 37, 1): |
| 104 | + pytest.skip("Diversity selection requires Weaviate >= 1.37.1") |
| 105 | + for name in ["banana", "apple", "orange", "car", "truck", "bike"]: |
| 106 | + collection.data.insert({"name": name}) |
| 107 | + |
| 108 | + result = collection.query.near_text( |
| 109 | + query="fruit", |
| 110 | + selection=Diversity.MMR(limit=3, balance=0.0), |
| 111 | + ) |
| 112 | + assert len(result.objects) == 3 |
| 113 | + |
| 114 | + |
| 115 | +@pytest.mark.skip("img2vec-neural not available in CI — see test_near_image in test_collection.py") |
| 116 | +def test_near_image_diversity(collection_factory: CollectionFactory) -> None: |
| 117 | + """near_image supports diversity selection.""" |
| 118 | + collection = collection_factory( |
| 119 | + properties=[Property(name="image", data_type=DataType.BLOB)], |
| 120 | + vector_config=Configure.Vectors.img2vec_neural(image_fields=["image"]), |
| 121 | + ) |
| 122 | + if collection._connection._weaviate_version.is_lower_than(1, 37, 1): |
| 123 | + pytest.skip("Diversity selection requires Weaviate >= 1.37.1") |
| 124 | + img_path = pathlib.Path("integration/weaviate-logo.png") |
| 125 | + for _ in range(3): |
| 126 | + collection.data.insert({"image": WEAVIATE_LOGO_NEW_ENCODED}) |
| 127 | + |
| 128 | + result = collection.query.near_image( |
| 129 | + near_image=img_path, |
| 130 | + selection=Diversity.MMR(limit=2, balance=0.0), |
| 131 | + ) |
| 132 | + assert len(result.objects) == 2 |
| 133 | + |
| 134 | + |
| 135 | +@pytest.mark.skip("multi2vec-* modules not available in CI") |
| 136 | +def test_near_media_diversity(collection_factory: CollectionFactory) -> None: |
| 137 | + """near_media supports diversity selection.""" |
| 138 | + from weaviate.collections.classes.grpc import NearMediaType |
| 139 | + |
| 140 | + collection = collection_factory( |
| 141 | + properties=[Property(name="image", data_type=DataType.BLOB)], |
| 142 | + vector_config=Configure.Vectors.img2vec_neural(image_fields=["image"]), |
| 143 | + ) |
| 144 | + if collection._connection._weaviate_version.is_lower_than(1, 37, 1): |
| 145 | + pytest.skip("Diversity selection requires Weaviate >= 1.37.1") |
| 146 | + for _ in range(3): |
| 147 | + collection.data.insert({"image": WEAVIATE_LOGO_NEW_ENCODED}) |
| 148 | + |
| 149 | + result = collection.query.near_media( |
| 150 | + media=WEAVIATE_LOGO_NEW_ENCODED, |
| 151 | + type_=NearMediaType.IMAGE, |
| 152 | + selection=Diversity.MMR(limit=2, balance=0.0), |
| 153 | + ) |
| 154 | + assert len(result.objects) == 2 |
0 commit comments