Skip to content

Commit 510da5e

Browse files
lingyinwcopybara-github
authored andcommitted
feat: Add hybrid query example to vector search sample.
PiperOrigin-RevId: 643779254
1 parent 32e3b22 commit 510da5e

File tree

3 files changed

+63
-4
lines changed

3 files changed

+63
-4
lines changed

samples/model-builder/test_constants.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,26 @@
350350
VECTOR_SEARCH_INDEX_ENDPOINT = "456"
351351
VECTOR_SEARCH_DEPLOYED_INDEX_ID = "789"
352352
VECTOR_SERACH_INDEX_QUERIES = [[0.1]]
353+
VECTOR_SERACH_INDEX_HYBRID_QUERIES = [
354+
aiplatform.matching_engine.matching_engine_index_endpoint.HybridQuery(
355+
dense_embedding=[1, 2, 3],
356+
sparse_embedding_dimensions=[10, 20, 30],
357+
sparse_embedding_values=[1.0, 1.0, 1.0],
358+
rrf_ranking_alpha=0.5,
359+
),
360+
aiplatform.matching_engine.matching_engine_index_endpoint.HybridQuery(
361+
dense_embedding=[1, 2, 3],
362+
sparse_embedding_dimensions=[10, 20, 30],
363+
sparse_embedding_values=[0.1, 0.2, 0.3],
364+
),
365+
aiplatform.matching_engine.matching_engine_index_endpoint.HybridQuery(
366+
sparse_embedding_dimensions=[10, 20, 30],
367+
sparse_embedding_values=[0.1, 0.2, 0.3],
368+
),
369+
aiplatform.matching_engine.matching_engine_index_endpoint.HybridQuery(
370+
dense_embedding=[1, 2, 3]
371+
),
372+
]
353373
VECTOR_SEARCH_INDEX_DISPLAY_NAME = "my-vector-search-index"
354374
VECTOR_SEARCH_GCS_URI = "gs://fake-dir"
355375
VECTOR_SEARCH_INDEX_ENDPOINT_DISPLAY_NAME = "my-vector-search-index-endpoint"

samples/model-builder/vector_search/vector_search_find_neighbors_sample.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,32 @@ def vector_search_find_neighbors(
5555
)
5656
print(resp)
5757

58+
# Query hybrid datapoints, sparse-only datapoints, and dense-only datapoints.
59+
hybrid_queries = [
60+
aiplatform.matching_engine.matching_engine_index_endpoint.HybridQuery(
61+
dense_embedding=[1, 2, 3],
62+
sparse_embedding_dimensions=[10, 20, 30],
63+
sparse_embedding_values=[1.0, 1.0, 1.0],
64+
rrf_ranking_alpha=0.5,
65+
),
66+
aiplatform.matching_engine.matching_engine_index_endpoint.HybridQuery(
67+
dense_embedding=[1, 2, 3],
68+
sparse_embedding_dimensions=[10, 20, 30],
69+
sparse_embedding_values=[0.1, 0.2, 0.3],
70+
),
71+
aiplatform.matching_engine.matching_engine_index_endpoint.HybridQuery(
72+
sparse_embedding_dimensions=[10, 20, 30],
73+
sparse_embedding_values=[0.1, 0.2, 0.3],
74+
),
75+
aiplatform.matching_engine.matching_engine_index_endpoint.HybridQuery(
76+
dense_embedding=[1, 2, 3]
77+
),
78+
]
79+
80+
hybrid_resp = my_index_endpoint.find_neighbors(
81+
deployed_index_id=deployed_index_id,
82+
queries=hybrid_queries,
83+
num_neighbors=num_neighbors,)
84+
print(hybrid_resp)
5885

5986
# [END aiplatform_sdk_vector_search_find_neighbors_sample]

samples/model-builder/vector_search/vector_search_find_neighbors_sample_test.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
from unittest.mock import call
16+
1517
import test_constants as constants
1618
from vector_search import vector_search_find_neighbors_sample
1719

@@ -38,8 +40,18 @@ def test_vector_search_find_neighbors_sample(
3840
index_endpoint_name=constants.VECTOR_SEARCH_INDEX_ENDPOINT)
3941

4042
# Check index_endpoint.find_neighbors is called with right params.
41-
mock_index_endpoint_find_neighbors.assert_called_with(
42-
deployed_index_id=constants.VECTOR_SEARCH_DEPLOYED_INDEX_ID,
43-
queries=constants.VECTOR_SERACH_INDEX_QUERIES,
44-
num_neighbors=10
43+
mock_index_endpoint_find_neighbors.assert_has_calls(
44+
[
45+
call(
46+
deployed_index_id=constants.VECTOR_SEARCH_DEPLOYED_INDEX_ID,
47+
queries=constants.VECTOR_SERACH_INDEX_QUERIES,
48+
num_neighbors=10,
49+
),
50+
call(
51+
deployed_index_id=constants.VECTOR_SEARCH_DEPLOYED_INDEX_ID,
52+
queries=constants.VECTOR_SERACH_INDEX_HYBRID_QUERIES,
53+
num_neighbors=10,
54+
),
55+
],
56+
any_order=False,
4557
)

0 commit comments

Comments
 (0)