GeezerVison is showing github.com — feat: Add support for self-signed JWT for queries on private endpoints · smbale/python-aiplatform@5025d03
File treeExpand file treeCollapse file tree Open diff view settings google/cloud/aiplatform/matching_engine
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: google/cloud/aiplatform/matching_engine/matching_engine_index_endpoint.py +23-2Lines changed: 23 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1387,6 +1387,7 @@ def find_neighbors( | 1387 | 1387 | return_full_datapoint: bool = False, | 1388 | 1388 | numeric_filter: Optional[List[NumericNamespace]] = None, | 1389 | 1389 | embedding_ids: Optional[List[str]] = None, | | 1390 | + signed_jwt: Optional[str] = None, | 1390 | 1391 | ) -> List[List[MatchNeighbor]]: | 1391 | 1392 | """Retrieves nearest neighbors for the given embedding queries on the | 1392 | 1393 | specified deployed index which is deployed to either public or private | @@ -1456,6 +1457,9 @@ def find_neighbors( | 1456 | 1457 | `embedding_ids` to lookup embedding values from dataset, if embedding | 1457 | 1458 | with `embedding_ids` exists in the dataset, do nearest neighbor search. | 1458 | 1459 |
| | 1460 | + signed_jwt (str): | | 1461 | + Optional. A signed JWT for accessing the private endpoint. | | 1462 | +
| 1459 | 1463 | Returns: | 1460 | 1464 | List[List[MatchNeighbor]] - A list of nearest neighbors for each query. | 1461 | 1465 | """ | @@ -1471,6 +1475,7 @@ def find_neighbors( | 1471 | 1475 | approx_num_neighbors=approx_num_neighbors, | 1472 | 1476 | fraction_leaf_nodes_to_search_override=fraction_leaf_nodes_to_search_override, | 1473 | 1477 | numeric_filter=numeric_filter, | | 1478 | + signed_jwt=signed_jwt, | 1474 | 1479 | ) | 1475 | 1480 |
| 1476 | 1481 | # Create the FindNeighbors request | @@ -1570,6 +1575,7 @@ def read_index_datapoints( | 1570 | 1575 | *, | 1571 | 1576 | deployed_index_id: str, | 1572 | 1577 | ids: List[str] = [], | | 1578 | + signed_jwt: Optional[str] = None, | 1573 | 1579 | ) -> List[gca_index_v1beta1.IndexDatapoint]: | 1574 | 1580 | """Reads the datapoints/vectors of the given IDs on the specified | 1575 | 1581 | deployed index which is deployed to public or private endpoint. | @@ -1587,6 +1593,8 @@ def read_index_datapoints( | 1587 | 1593 | Required. The ID of the DeployedIndex to match the queries against. | 1588 | 1594 | ids (List[str]): | 1589 | 1595 | Required. IDs of the datapoints to be searched for. | | 1596 | + signed_jwt (str): | | 1597 | + Optional. A signed JWT for accessing the private endpoint. | 1590 | 1598 | Returns: | 1591 | 1599 | List[gca_index_v1beta1.IndexDatapoint] - A list of datapoints/vectors of the given IDs. | 1592 | 1600 | """ | @@ -1595,6 +1603,7 @@ def read_index_datapoints( | 1595 | 1603 | embeddings = self._batch_get_embeddings( | 1596 | 1604 | deployed_index_id=deployed_index_id, | 1597 | 1605 | ids=ids, | | 1606 | + signed_jwt=signed_jwt, | 1598 | 1607 | ) | 1599 | 1608 |
| 1600 | 1609 | response = [] | @@ -1641,6 +1650,7 @@ def _batch_get_embeddings( | 1641 | 1650 | *, | 1642 | 1651 | deployed_index_id: str, | 1643 | 1652 | ids: List[str] = [], | | 1653 | + signed_jwt: Optional[str] = None, | 1644 | 1654 | ) -> List[match_service_pb2.Embedding]: | 1645 | 1655 | """ | 1646 | 1656 | Reads the datapoints/vectors of the given IDs on the specified index | @@ -1651,6 +1661,8 @@ def _batch_get_embeddings( | 1651 | 1661 | Required. The ID of the DeployedIndex to match the queries against. | 1652 | 1662 | ids (List[str]): | 1653 | 1663 | Required. IDs of the datapoints to be searched for. | | 1664 | + signed_jwt: | | 1665 | + Optional. A signed JWT for accessing the private endpoint. | 1654 | 1666 | Returns: | 1655 | 1667 | List[match_service_pb2.Embedding] - A list of datapoints/vectors of the given IDs. | 1656 | 1668 | """ | @@ -1665,7 +1677,10 @@ def _batch_get_embeddings( | 1665 | 1677 |
| 1666 | 1678 | for id in ids: | 1667 | 1679 | batch_request.id.append(id) | 1668 | | - response = stub.BatchGetEmbeddings(batch_request) | | 1680 | + metadata = None | | 1681 | + if signed_jwt: | | 1682 | + metadata = (("authorization", f"Bearer: {signed_jwt}"),) | | 1683 | + response = stub.BatchGetEmbeddings(batch_request, metadata=metadata) | 1669 | 1684 |
| 1670 | 1685 | return response.embeddings | 1671 | 1686 |
| @@ -1680,6 +1695,7 @@ def match( | 1680 | 1695 | fraction_leaf_nodes_to_search_override: Optional[float] = None, | 1681 | 1696 | low_level_batch_size: int = 0, | 1682 | 1697 | numeric_filter: Optional[List[NumericNamespace]] = None, | | 1698 | + signed_jwt: Optional[str] = None, | 1683 | 1699 | ) -> List[List[MatchNeighbor]]: | 1684 | 1700 | """Retrieves nearest neighbors for the given embedding queries on the | 1685 | 1701 | specified deployed index for private endpoint only. | @@ -1729,6 +1745,8 @@ def match( | 1729 | 1745 | results. For example: | 1730 | 1746 | [NumericNamespace(name="cost", value_int=5, op="GREATER")] | 1731 | 1747 | will match datapoints that its cost is greater than 5. | | 1748 | + signed_jwt (str): | | 1749 | + Optional. A signed JWT for accessing the private endpoint. | 1732 | 1750 |
| 1733 | 1751 | Returns: | 1734 | 1752 | List[List[MatchNeighbor]] - A list of nearest neighbors for each query. | @@ -1809,7 +1827,10 @@ def match( | 1809 | 1827 | batch_request.requests.append(batch_request_for_index) | 1810 | 1828 |
| 1811 | 1829 | # Perform the request | 1812 | | - response = stub.BatchMatch(batch_request) | | 1830 | + metadata = None | | 1831 | + if signed_jwt: | | 1832 | + metadata = (("authorization", f"Bearer: {signed_jwt}"),) | | 1833 | + response = stub.BatchMatch(batch_request, metadata=metadata) | 1813 | 1834 |
| 1814 | 1835 | # Wrap the results in MatchNeighbor objects and return | 1815 | 1836 | match_neighbors_response = [] | |
Collapse file Copy file name to clipboardExpand all lines: samples/model-builder/conftest.py +14Lines changed: 14 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1382,6 +1382,20 @@ def mock_index_endpoint_find_neighbors(mock_index_endpoint): | 1382 | 1382 | yield mock_find_neighbors | 1383 | 1383 |
| 1384 | 1384 |
| | 1385 | +@pytest.fixture | | 1386 | +def mock_index_endpoint_match(mock_index_endpoint): | | 1387 | + with patch.object(mock_index_endpoint, "match") as mock: | | 1388 | + mock.return_value = None | | 1389 | + yield mock | | 1390 | + | | 1391 | + | | 1392 | +@pytest.fixture | | 1393 | +def mock_index_endpoint_read_index_datapoints(mock_index_endpoint): | | 1394 | + with patch.object(mock_index_endpoint, "read_index_datapoints") as mock: | | 1395 | + mock.return_value = None | | 1396 | + yield mock | | 1397 | + | | 1398 | + | 1385 | 1399 | @pytest.fixture | 1386 | 1400 | def mock_index_create_tree_ah_index(mock_index): | 1387 | 1401 | with patch.object( | |
Collapse file Copy file name to clipboardExpand all lines: samples/model-builder/test_constants.py +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 407 | 407 | VECTOR_SEARCH_INDEX_LABELS = {"my_key": "my_value"} | 408 | 408 | VECTOR_SEARCH_GCS_URI = "gs://fake-dir" | 409 | 409 | VECTOR_SEARCH_INDEX_ENDPOINT_DISPLAY_NAME = "my-vector-search-index-endpoint" | | 410 | +VECTOR_SEARCH_PRIVATE_ENDPOINT_SIGNED_JWT = "fake-signed-jwt" |
Collapse file Copy file name to clipboardExpand all lines: samples/model-builder/vector_search/vector_search_find_neighbors_sample.py +50Lines changed: 50 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -84,3 +84,53 @@ def vector_search_find_neighbors( | 84 | 84 | print(hybrid_resp) | 85 | 85 |
| 86 | 86 | # [END aiplatform_sdk_vector_search_find_neighbors_sample] | | 87 | + | | 88 | + | | 89 | +# [START aiplatform_sdk_vector_search_find_neighbors_jwt_sample] | | 90 | +def vector_search_find_neighbors_jwt( | | 91 | + project: str, | | 92 | + location: str, | | 93 | + index_endpoint_name: str, | | 94 | + deployed_index_id: str, | | 95 | + queries: List[List[float]], | | 96 | + num_neighbors: int, | | 97 | + signed_jwt: str, | | 98 | +) -> List[List[aiplatform.matching_engine.matching_engine_index_endpoint.MatchNeighbor]]: | | 99 | + """Query the vector search index. | | 100 | +
| | 101 | + Args: | | 102 | + project (str): Required. Project ID | | 103 | + location (str): Required. The region name | | 104 | + index_endpoint_name (str): Required. Index endpoint to run the query | | 105 | + against. | | 106 | + deployed_index_id (str): Required. The ID of the DeployedIndex to run | | 107 | + the queries against. | | 108 | + queries (List[List[float]]): Required. A list of queries. Each query is | | 109 | + a list of floats, representing a single embedding. | | 110 | + num_neighbors (int): Required. The number of neighbors to return. | | 111 | + signed_jwt (str): Required. The signed JWT token for the private | | 112 | + endpoint. The endpoint must be configured to accept tokens from JWT's | | 113 | + issuer and encoded audience. | | 114 | +
| | 115 | + Returns: | | 116 | + List[List[aiplatform.matching_engine.matching_engine_index_endpoint.MatchNeighbor]] - A list of nearest neighbors for each query. | | 117 | + """ | | 118 | + # Initialize the Vertex AI client | | 119 | + aiplatform.init(project=project, location=location) | | 120 | + | | 121 | + # Create the index endpoint instance from an existing endpoint. | | 122 | + my_index_endpoint = aiplatform.MatchingEngineIndexEndpoint( | | 123 | + index_endpoint_name=index_endpoint_name | | 124 | + ) | | 125 | + | | 126 | + # Query the index endpoint for the nearest neighbors. | | 127 | + resp = my_index_endpoint.find_neighbors( | | 128 | + deployed_index_id=deployed_index_id, | | 129 | + queries=queries, | | 130 | + num_neighbors=num_neighbors, | | 131 | + signed_jwt=signed_jwt, | | 132 | + ) | | 133 | + return resp | | 134 | + | | 135 | +# [END aiplatform_sdk_vector_search_find_neighbors_jwt_sample] | | 136 | + |
Collapse file Copy file name to clipboardExpand all lines: samples/model-builder/vector_search/vector_search_find_neighbors_sample_test.py +31Lines changed: 31 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -55,3 +55,34 @@ def test_vector_search_find_neighbors_sample( | 55 | 55 | ], | 56 | 56 | any_order=False, | 57 | 57 | ) | | 58 | + | | 59 | + | | 60 | +def test_vector_search_find_neighbors_jwt_sample( | | 61 | + mock_sdk_init, mock_index_endpoint_init, mock_index_endpoint_find_neighbors | | 62 | +): | | 63 | + vector_search_find_neighbors_sample.vector_search_find_neighbors_jwt( | | 64 | + project=constants.PROJECT, | | 65 | + location=constants.LOCATION, | | 66 | + index_endpoint_name=constants.VECTOR_SEARCH_INDEX_ENDPOINT, | | 67 | + deployed_index_id=constants.VECTOR_SEARCH_DEPLOYED_INDEX_ID, | | 68 | + queries=constants.VECTOR_SERACH_INDEX_QUERIES, | | 69 | + num_neighbors=10, | | 70 | + signed_jwt=constants.VECTOR_SEARCH_PRIVATE_ENDPOINT_SIGNED_JWT, | | 71 | + ) | | 72 | + | | 73 | + # Check client initialization | | 74 | + mock_sdk_init.assert_called_with( | | 75 | + project=constants.PROJECT, location=constants.LOCATION | | 76 | + ) | | 77 | + | | 78 | + # Check index endpoint initialization with right index endpoint name | | 79 | + mock_index_endpoint_init.assert_called_with( | | 80 | + index_endpoint_name=constants.VECTOR_SEARCH_INDEX_ENDPOINT) | | 81 | + | | 82 | + # Check index_endpoint.find_neighbors is called with right params. | | 83 | + mock_index_endpoint_find_neighbors.assert_called_with( | | 84 | + deployed_index_id=constants.VECTOR_SEARCH_DEPLOYED_INDEX_ID, | | 85 | + queries=constants.VECTOR_SERACH_INDEX_QUERIES, | | 86 | + num_neighbors=10, | | 87 | + signed_jwt=constants.VECTOR_SEARCH_PRIVATE_ENDPOINT_SIGNED_JWT, | | 88 | + ) |
Collapse file Copy file name to clipboard +67Lines changed: 67 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +# Copyright 2023 Google LLC | | 2 | +# | | 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); | | 4 | +# you may not use this file except in compliance with the License. | | 5 | +# You may obtain a copy of the License at | | 6 | +# | | 7 | +# https://www.apache.org/licenses/LICENSE-2.0 | | 8 | +# | | 9 | +# Unless required by applicable law or agreed to in writing, software | | 10 | +# distributed under the License is distributed on an "AS IS" BASIS, | | 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | | 12 | +# See the License for the specific language governing permissions and | | 13 | +# limitations under the License. | | 14 | + | | 15 | +from typing import List | | 16 | + | | 17 | +from google.cloud import aiplatform | | 18 | + | | 19 | + | | 20 | +# [START aiplatform_sdk_vector_search_match_jwt_sample] | | 21 | +def vector_search_match_jwt( | | 22 | + project: str, | | 23 | + location: str, | | 24 | + index_endpoint_name: str, | | 25 | + deployed_index_id: str, | | 26 | + queries: List[List[float]], | | 27 | + num_neighbors: int, | | 28 | + signed_jwt: str, | | 29 | +) -> List[List[aiplatform.matching_engine.matching_engine_index_endpoint.MatchNeighbor]]: | | 30 | + """Query the vector search index. | | 31 | +
| | 32 | + Args: | | 33 | + project (str): Required. Project ID | | 34 | + location (str): Required. The region name | | 35 | + index_endpoint_name (str): Required. Index endpoint to run the query | | 36 | + against. The endpoint must be a private endpoint. | | 37 | + deployed_index_id (str): Required. The ID of the DeployedIndex to run | | 38 | + the queries against. | | 39 | + queries (List[List[float]]): Required. A list of queries. Each query is | | 40 | + a list of floats, representing a single embedding. | | 41 | + num_neighbors (int): Required. The number of neighbors to return. | | 42 | + signed_jwt (str): Required. The signed JWT token for the private | | 43 | + endpoint. The endpoint must be configured to accept tokens from JWT's | | 44 | + issuer and encoded audience. | | 45 | +
| | 46 | + Returns: | | 47 | + List[List[aiplatform.matching_engine.matching_engine_index_endpoint.MatchNeighbor]] - A list of nearest neighbors for each query. | | 48 | + """ | | 49 | + # Initialize the Vertex AI client | | 50 | + aiplatform.init(project=project, location=location) | | 51 | + | | 52 | + # Create the index endpoint instance from an existing endpoint. | | 53 | + my_index_endpoint = aiplatform.MatchingEngineIndexEndpoint( | | 54 | + index_endpoint_name=index_endpoint_name | | 55 | + ) | | 56 | + | | 57 | + # Query the index endpoint for matches. | | 58 | + resp = my_index_endpoint.match( | | 59 | + deployed_index_id=deployed_index_id, | | 60 | + queries=queries, | | 61 | + num_neighbors=num_neighbors, | | 62 | + signed_jwt=signed_jwt, | | 63 | + ) | | 64 | + return resp | | 65 | + | | 66 | +# [END aiplatform_sdk_vector_search_match_jwt_sample] | | 67 | + |
Collapse file Copy file name to clipboard +47Lines changed: 47 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +# Copyright 2023 Google LLC | | 2 | +# | | 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); | | 4 | +# you may not use this file except in compliance with the License. | | 5 | +# You may obtain a copy of the License at | | 6 | +# | | 7 | +# https://www.apache.org/licenses/LICENSE-2.0 | | 8 | +# | | 9 | +# Unless required by applicable law or agreed to in writing, software | | 10 | +# distributed under the License is distributed on an "AS IS" BASIS, | | 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | | 12 | +# See the License for the specific language governing permissions and | | 13 | +# limitations under the License. | | 14 | + | | 15 | +import test_constants as constants | | 16 | +from vector_search import vector_search_match_sample | | 17 | + | | 18 | + | | 19 | +def test_vector_search_match_jwt_sample( | | 20 | + mock_sdk_init, mock_index_endpoint_init, mock_index_endpoint_match | | 21 | +): | | 22 | + vector_search_match_sample.vector_search_match_jwt( | | 23 | + project=constants.PROJECT, | | 24 | + location=constants.LOCATION, | | 25 | + index_endpoint_name=constants.VECTOR_SEARCH_INDEX_ENDPOINT, | | 26 | + deployed_index_id=constants.VECTOR_SEARCH_DEPLOYED_INDEX_ID, | | 27 | + queries=constants.VECTOR_SERACH_INDEX_QUERIES, | | 28 | + num_neighbors=10, | | 29 | + signed_jwt=constants.VECTOR_SEARCH_PRIVATE_ENDPOINT_SIGNED_JWT, | | 30 | + ) | | 31 | + | | 32 | + # Check client initialization | | 33 | + mock_sdk_init.assert_called_with( | | 34 | + project=constants.PROJECT, location=constants.LOCATION | | 35 | + ) | | 36 | + | | 37 | + # Check index endpoint initialization with right index endpoint name | | 38 | + mock_index_endpoint_init.assert_called_with( | | 39 | + index_endpoint_name=constants.VECTOR_SEARCH_INDEX_ENDPOINT) | | 40 | + | | 41 | + # Check index_endpoint.match is called with right params. | | 42 | + mock_index_endpoint_match.assert_called_with( | | 43 | + deployed_index_id=constants.VECTOR_SEARCH_DEPLOYED_INDEX_ID, | | 44 | + queries=constants.VECTOR_SERACH_INDEX_QUERIES, | | 45 | + num_neighbors=10, | | 46 | + signed_jwt=constants.VECTOR_SEARCH_PRIVATE_ENDPOINT_SIGNED_JWT, | | 47 | + ) |
|