プライベート サービス アクセス(VPC ネットワーク ピアリング)または Private Service Connect インデックスをクエリする

VPC ネットワーク ピアリングまたは Private Service Connect インデックス エンドポイントをデプロイした後、クエリはデプロイ方法によって若干異なります。

Private Service Connect の自動化でデプロイする

Private Service Connect の自動化でデプロイされた IndexEndpoints の場合、Python SDK は Private Service Connect ネットワークを適切なエンドポイントに自動的にマッピングします。Python SDK を使用していない場合は、Private Service Connect の手動デプロイをクエリする手順に沿って、エンドポイント用に作成された IP アドレスに直接接続する必要があります。

Python

Vertex AI SDK for Python のインストールまたは更新の方法については、Vertex AI SDK for Python をインストールするをご覧ください。 詳細については、Python API リファレンス ドキュメントをご覧ください。

def vector_search_match_psc_automation(
    project: str,
    location: str,
    index_endpoint_name: str,
    deployed_index_id: str,
    queries: List[List[float]],
    num_neighbors: int,
    psc_network: str,
) -> List[List[aiplatform.matching_engine.matching_engine_index_endpoint.MatchNeighbor]]:
    """Query the vector search index deployed with PSC automation.

    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.
        ip_address (str): Required. The IP address of the PSC endpoint. Obtained
        from the created compute address used in the fordwarding rule to the
        endpoint's service attachment.
        psc_network (str): The network the endpoint was deployed to via PSC
        automation configuration. The format is
        projects/{project_id}/global/networks/{network_name}.

    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,
        psc_network=psc_network
    )
    return resp

Private Service Connect の手動構成でデプロイする

手動で構成された接続でデプロイされた Private Service Connect IndexEndpoints の場合、エンドポイントの Private Service Connect サービス アタッチメントに転送されたコンピューティング アドレスの IP アドレスを使用して、エンドポイントにアクセスします。

サービス アタッチメント URI に転送された IP アドレスが不明な場合は、gcloud ai index-endpoints describe コマンドと gcloud compute forwarding-rules list コマンドを使用して取得できます。

次のように置き換えます。

  • INDEX_ENDPOINT_ID: 完全修飾インデックス エンドポイント ID。
  • REGION: インデックス エンドポイントがデプロイされているリージョン。
SERVICE_ATTACHMENT_URI=`gcloud ai index-endpoints describe INDEX_ENDPOINT_ID \
  --region=REGION \
  --format="value(deployedIndexes.privateEndpoints.serviceAttachment)"`

gcloud compute forwarding-rules list --filter="TARGET:${SERVICE_ATTACHMENT_URI}"

出力には、IndexEndpoint のクエリに使用する内部 IP アドレスが含まれます。

Python

Vertex AI SDK for Python のインストールまたは更新の方法については、Vertex AI SDK for Python をインストールするをご覧ください。 詳細については、Python API リファレンス ドキュメントをご覧ください。

def vector_search_match_psc_manual(
    project: str,
    location: str,
    index_endpoint_name: str,
    deployed_index_id: str,
    queries: List[List[float]],
    num_neighbors: int,
    ip_address: str,
) -> List[List[aiplatform.matching_engine.matching_engine_index_endpoint.MatchNeighbor]]:
    """Query the vector search index deployed with PSC manual configuration.

    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.
        ip_address (str): Required. The IP address of the PSC endpoint. Obtained
        from the created compute address used in the forwarding rule to the
        endpoint's service attachment.

    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
    )

    # Set the IP address of the PSC endpoint.
    my_index_endpoint.private_service_connect_ip_address = ip_address

    # Query the index endpoint for matches.
    resp = my_index_endpoint.match(
        deployed_index_id=deployed_index_id,
        queries=queries,
        num_neighbors=num_neighbors
    )
    return resp

コマンドライン

DeployedIndex をクエリするには、ポート 10000TARGET_IP に接続し、Match メソッドまたは BatchMatch メソッドを呼び出します。また、特定のエンベディング ID を使用してクエリすることもできます。

以下の例では、オープンソース ツールの grpc_cli を使用して、デプロイされたインデックス サーバーに gRPC リクエストを送信します。

最初の例では、Match メソッドを使用して単一のクエリを送信します。

./grpc_cli call ${TARGET_IP}:10000 google.cloud.aiplatform.container.v1.MatchService.Match 'deployed_index_id: "${DEPLOYED_INDEX_ID}", float_val: [-0.1,..]'
  

2 番目の例では、2 つのクエリを同じ BatchMatch リクエストに統合しています。

./grpc_cli call ${TARGET_IP}:10000 google.cloud.aiplatform.container.v1.MatchService.BatchMatch 'requests: [{deployed_index_id: "${DEPLOYED_INDEX_ID}", requests: [{deployed_index_id: "${DEPLOYED_INDEX_ID}", float_val: [-0.1,..]}, {deployed_index_id: "${DEPLOYED_INDEX_ID}", float_val: [-0.2,..]}]}]'
  

これらの API は、サービスとピアリングされた同じ VPC で実行されているクライアントから呼び出す必要があります。

embedding_id を使用してクエリを実行するには、次の例を使用します。

./grpc_cli call ${TARGET_IP}:10000  google.cloud.aiplatform.container.v1.MatchService.Match "deployed_index_id:'"test_index1"',embedding_id: '"606431"'"

この例では、トークンと数値の制限を使用してクエリを送信します。

./grpc_cli call ${TARGET_IP}:10000 google.cloud.aiplatform.container.v1.MatchService.Match 'deployed_index_id: "${DEPLOYED_INDEX_ID}", float_val: [1, 1], "sparse_embedding": {"values": [111.0,111.1,111.2], "dimensions": [10,20,30]}, numeric_restricts: [{name: "double-ns", value_double: 0.3, op: LESS_EQUAL}, {name: "double-ns", value_double: -1.2, op: GREATER}, {name: "double-ns", value_double: 0., op: NOT_EQUAL}], restricts: [{name: "color", allow_tokens: ["red"]}]'

詳細については、クライアント ライブラリの説明をご覧ください。

コンソール

コンソールから VPC インデックスをクエリするには、次の手順を実施します。

  1. Google Cloud コンソールの [Vertex AI] セクションで、[デプロイと使用] セクションに移動します。[ベクトル検索] を選択します。

    [ベクトル検索] に移動

  2. クエリを実行する VPC インデックスを選択します。[インデックスの情報] ページが開きます。
  3. [デプロイされたインデックス] セクションまで下にスクロールし、クエリを実行するデプロイされたインデックスを選択します。[デプロイされるインデックスの情報] ページが開きます。
  4. [インデックスのクエリ] セクションから、クエリ パラメータを選択します。ベクトルや特定のデータポイントでクエリを実行できます。
  5. オープンソース ツールの grpc_cli または Vertex AI SDK for Python を使用してクエリを実行します。

VPC ネットワーク ピアリングでデプロイする

Python

Vertex AI SDK for Python のインストールまたは更新の方法については、Vertex AI SDK for Python をインストールするをご覧ください。 詳細については、Python API リファレンス ドキュメントをご覧ください。

注: Python SDK は、VPC ネットワーク ピアリングでデプロイされた IndexEndpoint の IP アドレスを自動的に検索します。

def vector_search_match_hybrid_queries(
    project: str,
    location: str,
    index_endpoint_name: str,
    deployed_index_id: str,
    num_neighbors: int,
) -> 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.
        num_neighbors (int): Required. The number of neighbors to return.

    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
    )

    # Example queries containing hybrid datapoints, sparse-only datapoints, and
    # dense-only datapoints.
    hybrid_queries = [
        aiplatform.matching_engine.matching_engine_index_endpoint.HybridQuery(
            dense_embedding=[1, 2, 3],
            sparse_embedding_dimensions=[10, 20, 30],
            sparse_embedding_values=[1.0, 1.0, 1.0],
            rrf_ranking_alpha=0.5,
        ),
        aiplatform.matching_engine.matching_engine_index_endpoint.HybridQuery(
            dense_embedding=[1, 2, 3],
            sparse_embedding_dimensions=[10, 20, 30],
            sparse_embedding_values=[0.1, 0.2, 0.3],
        ),
        aiplatform.matching_engine.matching_engine_index_endpoint.HybridQuery(
            sparse_embedding_dimensions=[10, 20, 30],
            sparse_embedding_values=[0.1, 0.2, 0.3],
        ),
        aiplatform.matching_engine.matching_engine_index_endpoint.HybridQuery(
            dense_embedding=[1, 2, 3]
        ),
    ]

    # Query the index endpoint for matches.
    resp = my_index_endpoint.match(
        deployed_index_id=deployed_index_id,
        queries=hybrid_queries,
        num_neighbors=num_neighbors,
    )
    return resp

コマンドライン

DeployedIndex には TARGET_IP があり、IndexEndpoints のリストで取得できます。

DeployedIndex をクエリするには、ポート 10000TARGET_IP に接続し、Match メソッドまたは BatchMatch メソッドを呼び出します。また、特定のエンベディング ID を使用してクエリすることもできます。

以下の例では、オープンソース ツールの grpc_cli を使用して、デプロイされたインデックス サーバーに gRPC リクエストを送信します。

最初の例では、Match メソッドを使用して単一のクエリを送信します。

./grpc_cli call ${TARGET_IP}:10000 google.cloud.aiplatform.container.v1.MatchService.Match 'deployed_index_id: "${DEPLOYED_INDEX_ID}", float_val: [-0.1,..]'
  

2 番目の例では、2 つのクエリを同じ BatchMatch リクエストに統合しています。

./grpc_cli call ${TARGET_IP}:10000 google.cloud.aiplatform.container.v1.MatchService.BatchMatch 'requests: [{deployed_index_id: "${DEPLOYED_INDEX_ID}", requests: [{deployed_index_id: "${DEPLOYED_INDEX_ID}", float_val: [-0.1,..]}, {deployed_index_id: "${DEPLOYED_INDEX_ID}", float_val: [-0.2,..]}]}]'
  

これらの API は、サービスとピアリングされた同じ VPC で実行されているクライアントから呼び出す必要があります。

embedding_id を使用してクエリを実行するには、次の例を使用します。

./grpc_cli call ${TARGET_IP}:10000  google.cloud.aiplatform.container.v1.MatchService.Match "deployed_index_id:'"test_index1"',embedding_id: '"606431"'"

この例では、トークンと数値の制限を使用してクエリを送信します。

./grpc_cli call ${TARGET_IP}:10000 google.cloud.aiplatform.container.v1.MatchService.Match 'deployed_index_id: "${DEPLOYED_INDEX_ID}", float_val: [1, 1], "sparse_embedding": {"values": [111.0,111.1,111.2], "dimensions": [10,20,30]}, numeric_restricts: [{name: "double-ns", value_double: 0.3, op: LESS_EQUAL}, {name: "double-ns", value_double: -1.2, op: GREATER}, {name: "double-ns", value_double: 0., op: NOT_EQUAL}], restricts: [{name: "color", allow_tokens: ["red"]}]'

詳細については、クライアント ライブラリの説明をご覧ください。

コンソール

コンソールから VPC インデックスをクエリするには、次の手順を実施します。

  1. Google Cloud コンソールの [Vertex AI] セクションで、[デプロイと使用] セクションに移動します。[ベクトル検索] を選択します。

    [ベクトル検索] に移動

  2. クエリを実行する VPC インデックスを選択します。[インデックスの情報] ページが開きます。
  3. [デプロイされたインデックス] セクションまで下にスクロールし、クエリを実行するデプロイされたインデックスを選択します。[デプロイされるインデックスの情報] ページが開きます。
  4. [インデックスのクエリ] セクションから、クエリ パラメータを選択します。ベクトルや特定のデータポイントでクエリを実行できます。
  5. オープンソース ツールの grpc_cli または Vertex AI SDK for Python を使用してクエリを実行します。

パフォーマンスに影響するクエリ時間の設定

ベクトル検索を使用する場合、クエリ時のパラメータはレイテンシ、可用性、コストに影響する可能性があります。このガイダンスはほとんどのケースに適用されます。 ただし、必ず構成をテストして、ユースケースに適していることを確認してください。

パラメータの定義については、インデックス構成パラメータをご覧ください。

パラメータ 概要 パフォーマンスへの影響
approximateNeighborsCount

各シャードから取得するおおよその結果数をアルゴリズムに指定します。

approximateNeighborsCount の値は常に setNeighborsCount の値より大きくする必要があります。setNeighborsCount の値が小さい場合は、approximateNeighborsCount の値を 10 倍にすることをおすすめします。setNeighborsCount 値が大きい場合は、小さい乗数を使用できます。

このフィールドに対応する REST API 名は approximate_neighbor_count です。

approximateNeighborsCount の値を大きくすると、次のようにパフォーマンスに影響する可能性があります。

  • 再現率: 増加
  • レイテンシ: 増加する可能性がある
  • 可用性: 影響なし
  • 費用: 検索時に処理されるデータが増えるため、費用が増加する可能性がある

approximateNeighborsCount の値を小さくすると、次のようにパフォーマンスに影響する可能性があります。

  • 再現率: 減少
  • レイテンシ: 低下する可能性がある
  • 可用性: 影響なし
  • 費用: 検索時に処理されるデータが少ないため、費用が削減される
setNeighborCount

クエリで返す結果の数を指定します。

このフィールドに対応する REST API 名は neighbor_count です。

300 以下の値は、ほとんどのユースケースでパフォーマンスを維持できます。値が大きい場合は、特定のユースケースでテストします。

fractionLeafNodesToSearch 最近傍を検索する際に参照するリーフノードの割合を制御します。これは、リーフノードあたりのエンベディングが多いほど、リーフあたりのデータが多く検査される leafNodeEmbeddingCount に関連しています。

このフィールドに対応する REST API 名は fraction_leaf_nodes_to_search_override です。

fractionLeafNodesToSearch の値を大きくすると、次のようにパフォーマンスに影響する可能性があります。

  • 再現率: 増加
  • レイテンシ: 増加
  • 可用性: 影響なし
  • 費用: レイテンシが長くなると、マシンリソースの占有量が増えるため、費用が増加する可能性がある

fractionLeafNodesToSearch の値を小さくすると、次のようにパフォーマンスに影響する可能性があります。

  • 再現率: 減少
  • レイテンシ: 短縮
  • 可用性: 影響なし
  • 費用: レイテンシが短いほどマシンリソースを占有しなくなるため、費用が削減される

次のステップ