-
Notifications
You must be signed in to change notification settings - Fork 348
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add support for self-signed JWT for queries on private endpoints
PiperOrigin-RevId: 689941402
- Loading branch information
1 parent
91c2120
commit 5025d03
Showing
10 changed files
with
504 additions
and
11 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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
67 changes: 67 additions & 0 deletions
67
samples/model-builder/vector_search/vector_search_match_sample.py
This file contains 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,67 @@ | ||
# Copyright 2023 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from typing import List | ||
|
||
from google.cloud import aiplatform | ||
|
||
|
||
# [START aiplatform_sdk_vector_search_match_jwt_sample] | ||
def vector_search_match_jwt( | ||
project: str, | ||
location: str, | ||
index_endpoint_name: str, | ||
deployed_index_id: str, | ||
queries: List[List[float]], | ||
num_neighbors: int, | ||
signed_jwt: str, | ||
) -> List[List[aiplatform.matching_engine.matching_engine_index_endpoint.MatchNeighbor]]: | ||
"""Query the vector search index. | ||
Args: | ||
project (str): Required. Project ID | ||
location (str): Required. The region name | ||
index_endpoint_name (str): Required. Index endpoint to run the query | ||
against. The endpoint must be a private endpoint. | ||
deployed_index_id (str): Required. The ID of the DeployedIndex to run | ||
the queries against. | ||
queries (List[List[float]]): Required. A list of queries. Each query is | ||
a list of floats, representing a single embedding. | ||
num_neighbors (int): Required. The number of neighbors to return. | ||
signed_jwt (str): Required. The signed JWT token for the private | ||
endpoint. The endpoint must be configured to accept tokens from JWT's | ||
issuer and encoded audience. | ||
Returns: | ||
List[List[aiplatform.matching_engine.matching_engine_index_endpoint.MatchNeighbor]] - A list of nearest neighbors for each query. | ||
""" | ||
# Initialize the Vertex AI client | ||
aiplatform.init(project=project, location=location) | ||
|
||
# Create the index endpoint instance from an existing endpoint. | ||
my_index_endpoint = aiplatform.MatchingEngineIndexEndpoint( | ||
index_endpoint_name=index_endpoint_name | ||
) | ||
|
||
# Query the index endpoint for matches. | ||
resp = my_index_endpoint.match( | ||
deployed_index_id=deployed_index_id, | ||
queries=queries, | ||
num_neighbors=num_neighbors, | ||
signed_jwt=signed_jwt, | ||
) | ||
return resp | ||
|
||
# [END aiplatform_sdk_vector_search_match_jwt_sample] | ||
|
47 changes: 47 additions & 0 deletions
47
samples/model-builder/vector_search/vector_search_match_sample_test.py
This file contains 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,47 @@ | ||
# Copyright 2023 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import test_constants as constants | ||
from vector_search import vector_search_match_sample | ||
|
||
|
||
def test_vector_search_match_jwt_sample( | ||
mock_sdk_init, mock_index_endpoint_init, mock_index_endpoint_match | ||
): | ||
vector_search_match_sample.vector_search_match_jwt( | ||
project=constants.PROJECT, | ||
location=constants.LOCATION, | ||
index_endpoint_name=constants.VECTOR_SEARCH_INDEX_ENDPOINT, | ||
deployed_index_id=constants.VECTOR_SEARCH_DEPLOYED_INDEX_ID, | ||
queries=constants.VECTOR_SERACH_INDEX_QUERIES, | ||
num_neighbors=10, | ||
signed_jwt=constants.VECTOR_SEARCH_PRIVATE_ENDPOINT_SIGNED_JWT, | ||
) | ||
|
||
# Check client initialization | ||
mock_sdk_init.assert_called_with( | ||
project=constants.PROJECT, location=constants.LOCATION | ||
) | ||
|
||
# Check index endpoint initialization with right index endpoint name | ||
mock_index_endpoint_init.assert_called_with( | ||
index_endpoint_name=constants.VECTOR_SEARCH_INDEX_ENDPOINT) | ||
|
||
# Check index_endpoint.match is called with right params. | ||
mock_index_endpoint_match.assert_called_with( | ||
deployed_index_id=constants.VECTOR_SEARCH_DEPLOYED_INDEX_ID, | ||
queries=constants.VECTOR_SERACH_INDEX_QUERIES, | ||
num_neighbors=10, | ||
signed_jwt=constants.VECTOR_SEARCH_PRIVATE_ENDPOINT_SIGNED_JWT, | ||
) |
Oops, something went wrong.