GeezerVison is showing github.com — docs(samples): adding code sample for vector search create streaming … · iLLRaps3/python-aiplatform@71464e7
File treeExpand file treeCollapse file tree Open diff view settings samples/model-builder/vector_search
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: samples/model-builder/vector_search/vector_search_create_index_sample.py +33Lines changed: 33 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -48,3 +48,36 @@ def vector_search_create_index( | 48 | 48 |
| 49 | 49 |
| 50 | 50 | # [END aiplatform_sdk_vector_search_create_index_sample] | | 51 | + | | 52 | + | | 53 | +# [START aiplatform_sdk_vector_search_create_streaming_index_sample] | | 54 | +def vector_search_create_streaming_index( | | 55 | + project: str, location: str, display_name: str, gcs_uri: Optional[str] = None | | 56 | +) -> None: | | 57 | + """Create a vector search index. | | 58 | +
| | 59 | + Args: | | 60 | + project (str): Required. Project ID | | 61 | + location (str): Required. The region name | | 62 | + display_name (str): Required. The index display name | | 63 | + gcs_uri (str): Optional. The Google Cloud Storage uri for index content | | 64 | + """ | | 65 | + # Initialize the Vertex AI client | | 66 | + aiplatform.init(project=project, location=location, staging_bucket=gcs_uri) | | 67 | + | | 68 | + # Create Index | | 69 | + index = aiplatform.MatchingEngineIndex.create_tree_ah_index( | | 70 | + display_name=display_name, | | 71 | + description="Matching Engine Index", | | 72 | + dimensions=100, | | 73 | + approximate_neighbors_count=150, | | 74 | + leaf_node_embedding_count=500, | | 75 | + leaf_nodes_to_search_percent=7, | | 76 | + index_update_method="stream_update", # Options: stream_update, batch_update | | 77 | + distance_measure_type=aiplatform.matching_engine.matching_engine_index_config.DistanceMeasureType.DOT_PRODUCT_DISTANCE, | | 78 | + ) | | 79 | + | | 80 | + print(index.name) | | 81 | + | | 82 | + | | 83 | +# [END aiplatform_sdk_vector_search_create_streaming_index_sample] |
Collapse file Copy file name to clipboardExpand all lines: samples/model-builder/vector_search/vector_search_create_index_sample_test.py +32-1Lines changed: 32 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -44,6 +44,37 @@ def test_vector_search_create_index_sample( | 44 | 44 | approximate_neighbors_count=ANY, | 45 | 45 | leaf_node_embedding_count=ANY, | 46 | 46 | leaf_nodes_to_search_percent=ANY, | 47 | | - index_update_method=ANY, | | 47 | + index_update_method="batch_update", | | 48 | + distance_measure_type=ANY, | | 49 | + ) | | 50 | + | | 51 | + | | 52 | +def test_vector_search_create_streaming_index_sample( | | 53 | + mock_sdk_init, | | 54 | + mock_index_create_tree_ah_index, | | 55 | +): | | 56 | + vector_search_create_index_sample.vector_search_create_streaming_index( | | 57 | + project=constants.PROJECT, | | 58 | + location=constants.LOCATION, | | 59 | + display_name=constants.VECTOR_SEARCH_INDEX_DISPLAY_NAME, | | 60 | + gcs_uri=constants.VECTOR_SEARCH_GCS_URI, | | 61 | + ) | | 62 | + | | 63 | + # Check client initialization | | 64 | + mock_sdk_init.assert_called_with( | | 65 | + project=constants.PROJECT, | | 66 | + location=constants.LOCATION, | | 67 | + staging_bucket=constants.VECTOR_SEARCH_GCS_URI, | | 68 | + ) | | 69 | + | | 70 | + # Check index creation | | 71 | + mock_index_create_tree_ah_index.assert_called_with( | | 72 | + display_name=constants.VECTOR_SEARCH_INDEX_DISPLAY_NAME, | | 73 | + description=ANY, | | 74 | + dimensions=ANY, | | 75 | + approximate_neighbors_count=ANY, | | 76 | + leaf_node_embedding_count=ANY, | | 77 | + leaf_nodes_to_search_percent=ANY, | | 78 | + index_update_method="stream_update", | 48 | 79 | distance_measure_type=ANY, | 49 | 80 | ) |
|