使用 Private Service Connect 設定向量搜尋

Private Service Connect 可讓您在屬於不同群組、團隊、專案和機構的虛擬私有雲網路中,私人使用服務。您可以使用自訂的 VPC 網路內部 IP 位址發布及使用服務,並使用向量搜尋端點執行向量相似度搜尋。

在向量搜尋端點上啟用 Private Service Connect 時,請考量以下用途:

  1. 需要低延遲和安全的連線,才能與 Vector Search 服務後端連線。
  2. 專屬虛擬私有雲對等互連保留項目的 IP 空間有限。
  3. 需要從多個使用者虛擬私有雲網路存取服務後端。

如要進一步瞭解如何設定 Private Service Connect,請參閱虛擬私有雲端 (VPC) 說明文件中的「Private Service Connect 總覽」。

建立索引端點

建立端點時,您必須啟用 Private Service Connect。這個程序與在 Vertex AI 中建立其他端點類似。

REST

  • PROJECT:您建立 Vertex AI 資源的服務專案 ID。

  • REGION:網路區域。

  • DISPLAY_NAME:要提供給端點的顯示名稱。這個名稱會用來建立端點 ID,且無法在日後更新。

  • VPC_PROJECTS:如果是共用虛擬私有雲設定,這會是分隔符分隔的 VPC 主機專案清單。對於獨立的 VPC 設定,這與 PROJECT 相同。

PROJECT=PROJECT_ID
VPC_PROJECT=VPC_PROJECT_ID
REGION=us-central1
VERTEX_ENDPOINT=REGION-aiplatform.googleapis.com
curl -H "Content-Type: application/json" \
  -H "Authorization: Bearer `gcloud auth print-access-token`" \
  https://REGION-aiplatform.googleapis.com/v1/projects/PROJECT/locations/REGION/indexEndpoints \
  -d '{
     "displayName": "DISPLAY_NAME",
     "privateServiceConnectConfig": {
       "enablePrivateServiceConnect": true,
       "projectAllowlist": [ "VPC_PROJECT_1", "VPC_PROJECT_2", "VPC_PROJECT_N"]
     }
   }

控制台

如何建立端點:

  1. 按一下下方按鈕,前往 Google Cloud 控制台中的 Vector Search。系統會顯示有效索引清單。

    前往 Vector Search

  2. 選取「索引端點」分頁標籤。索引端點會顯示在畫面上。

  3. 按一下「建立新端點」。系統會開啟「Create a new index endpoint」面板。

  4. 在「Display name」 中,輸入索引端點的顯示名稱。這個名稱會用來建立端點 ID,且無法在日後更新。

  5. 從「Region」下拉式選單中選取一個地區。

  6. 在「存取」下方,按一下「Private Service Connect (預先發布版)」

  7. 系統會顯示文字欄位,您可以在此指定要使用的 VPC 專案。新增要使用的虛擬私有雲專案 ID 或編號。

  8. 按一下 [建立]。

關於索引部署選項

您可以透過自動或手動服務連線部署索引。

  • 使用 Private Service Connect 自動化功能部署:設定服務連線政策並部署索引。設定服務連線政策後,您就能部署至特定網路,而無須每次都建立運算位址和轉送規則。
  • 透過手動連線部署:部署索引,並手動建立運算地址和轉送規則。如果您需要為同一個服務附件 URI 使用多個 IP 位址,可以選擇這個選項,但這不是常見的用途。

使用 Private Service Connect 自動化功能部署

您可以設定服務連線政策,這樣就不必在每次索引部署後手動建立運算位址和轉送規則。

  1. 首先,請建立服務連線政策,指定要部署索引的網路、服務類別和區域。這項設定只需執行一次。如果您已完成這項操作,請跳至下一個程序。
  2. 部署索引

限制

自動化功能僅允許每個網路的每個專案使用一個 IP 位址。如果需要使用多個 IP 位址,請參閱「手動部署索引」。

建立服務連線政策

您必須是網路管理員,才能建立服務連線政策,以便自動部署索引。

如要自動部署索引,請按照下列步驟操作:

  1. 建立服務連線政策。

    • PROJECT:您要建立 Vertex AI 資源的服務專案。

    • VPC_PROJECT:用戶端虛擬私有雲所在的專案。如果是單一虛擬私有雲設定,則與 $PROJECT 相同。在共用虛擬私有雲設定中,這是指 VPC 主專案。

    • NETWORK_NAME:要部署至的網路名稱,格式為 projects//global/networks/

    • REGION:網路區域。

    • PSC_SUBNETS:要使用的 Private Service Connect 子網路。

    gcloud network-connectivity service-connection-policies create <policy_name> \
    --project=<vpc_project> --network=<network_name> # in the format projects/<project_id>/global/networks/<network_name> \
    --service-class=gcp-vertexai --region=<region> --subnets=<psc subnets>
    
  2. 查看服務連線政策。

    gcloud network-connectivity service-connection-policies list --project=<vpc_project> -–region=<region>
    

如要進一步瞭解服務連線政策,請參閱「設定服務連線政策」。

部署索引

REST

  • PROJECT:您要建立 Vertex AI 資源的服務專案。

  • VPC_PROJECT:客戶端虛擬私有雲所在的專案。對於共用虛擬私有雲設定,這是虛擬私有雲主機專案。

  • DISPLAY_NAME:要提供給端點的顯示名稱。這個名稱會用來建立端點 ID,且無法在日後更新。

  • NETWORK_NAME:要部署至的網路名稱,格式為 projects//global/networks/

  • REGION:網路區域。

  • PSC_SUBNETS:要使用的 Private Service Connect 子網路。

PROJECT=PROJECT
VPC_PROJECTS=VPC_PROJECTS
REGION=REGION
curl -X POST -H "Authorization: Bearer $(gcloud auth print-access-token)"
-H "Content-Type: application/json; charset=utf-8" "https://REGIONAL_ENDPOINT.googleapis.com/v1/projects/PROJECT_NUMBER/locations/REGION/indexEndpoints/INDEX_ENDPOINT_ID:deployIndex"
-d '{
  "deployedIndex": {
    "id": "DEPLOYED_INDEX_ID",
    "index": "projects/PROJECT/locations/us-central1/indexes/INDEX_ID ",
    "displayName": "DISPLAY_NAME",
    "psc_automation_configs": [
      { "project_id": "PROJECT_1", "network": "NETWORK_NAME_1" },
      { "project_id": "PROJECT_2", "network": "NETWORK_NAME_2" },
      { "project_id": "PROJECT_N", "network": "NETWORK_NAME_N" }]
    }
}'

控制台

如何部署端點:

  1. 按一下下方按鈕,前往 Google Cloud 控制台中的 Vector Search。系統會顯示有效索引清單。

    前往 Vector Search

  2. 按一下要部署的 Vector Search 端點 (已啟用 Private Service Connect) 的「Deploy」按鈕。系統會顯示「Deploy index」滑出式面板。

  3. 在「Display name」中,輸入已部署索引的顯示名稱。這個名稱會用於建立 ID,且無法在日後更新。

  4. 按一下「端點」,然後選擇要部署索引的端點。

  5. 您可以按一下「Machine type」手動選擇要部署索引的機器類型。根據預設,系統會根據索引資料分割大小自動選取機器類型。

您現在需要新增 Private Service Connect 自動化設定。方法如下:

  1. 在「PSC 自動化設定」部分,按一下「新增設定」

  2. 輸入要連線的 VPC 專案名稱。

  3. 輸入要連線的 VPC 專案網路名稱。格式必須為 projects/<project_number>/global/networks/<network_name>

  4. 您可以視需要按一下「Add new config」(新增設定) ,然後繼續新增 Private Service Connect 自動化設定。

  5. 按一下「Deploy」即可完成。

刪除服務連線政策

如要刪除服務連線政策,請執行下列指令:

gcloud network-connectivity service-connection-policies delete --project=<vpc_project> –-region=<region> <policy_name>

如要進一步瞭解服務連線政策,請參閱「設定服務連線政策」。

使用手動連線進行部署

在 VPC 專案中部署索引並建立轉送規則。

部署索引

索引已準備就緒,因此您可以在這個步驟中,將索引部署至啟用 Private Service Connect 的端點。

gcloud

本範例使用 gcloud ai index-endpoints deploy-index 指令

使用下列任何指令資料之前,請先替換以下項目:

  • INDEX_ENDPOINT_ID:索引端點的 ID。
  • DEPLOYED_INDEX_ID:使用者指定的字串,用於唯一識別已部署的索引。開頭必須是英文字母,且只能包含英文字母、數字或底線。如需格式規範,請參閱 DeployedIndex.id
  • DEPLOYED_INDEX_ENDPOINT_NAME:已部署索引端點的顯示名稱。
  • INDEX_ID:索引的 ID。
  • LOCATION:您使用 Vertex AI 的區域。
  • PROJECT_ID:您的 Google Cloud 專案 ID

執行下列指令:

Linux、macOS 或 Cloud Shell

gcloud ai index-endpoints deploy-index INDEX_ENDPOINT_ID \
    --deployed-index-id=DEPLOYED_INDEX_ID \
    --display-name=DEPLOYED_INDEX_ENDPOINT_NAME \
    --index=INDEX_ID \
    --region=LOCATION \
    --project=PROJECT_ID

Windows (PowerShell)

gcloud ai index-endpoints deploy-index INDEX_ENDPOINT_ID `
    --deployed-index-id=DEPLOYED_INDEX_ID `
    --display-name=DEPLOYED_INDEX_ENDPOINT_NAME `
    --index=INDEX_ID `
    --region=LOCATION `
    --project=PROJECT_ID

Windows (cmd.exe)

gcloud ai index-endpoints deploy-index INDEX_ENDPOINT_ID ^
    --deployed-index-id=DEPLOYED_INDEX_ID ^
    --display-name=DEPLOYED_INDEX_ENDPOINT_NAME ^
    --index=INDEX_ID ^
    --region=LOCATION ^
    --project=PROJECT_ID

REST

使用任何要求資料之前,請先替換以下項目:

  • INDEX_ENDPOINT_ID:索引端點的 ID。
  • DEPLOYED_INDEX_ID:使用者指定的字串,用於唯一識別已部署的索引。開頭必須是英文字母,且只能包含英文字母、數字或底線。如需格式規範,請參閱 DeployedIndex.id
  • DEPLOYED_INDEX_ENDPOINT_NAME:已部署索引端點的顯示名稱。
  • INDEX_ID:索引的 ID。
  • LOCATION:您使用 Vertex AI 的區域。
  • PROJECT_ID:您的 Google Cloud 專案 ID
  • PROJECT_NUMBER:系統自動產生的專案編號

HTTP 方法和網址:

POST https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/indexEndpoints/INDEX_ENDPOINT_ID:deployIndex

JSON 要求主體:

{
 "deployedIndex": {
   "id": "DEPLOYED_INDEX_ID",
   "index": "projects/PROJECT_ID/locations/LOCATION/indexes/INDEX_ID",
   "displayName": "DEPLOYED_INDEX_ENDPOINT_NAME"
 }
}

如要傳送要求,請展開以下其中一個選項:

您應該會收到如下的 JSON 回應:

{
 "name": "projects/PROJECT_NUMBER/locations/LOCATION/indexEndpoints/INDEX_ENDPOINT_ID/operations/OPERATION_ID",
 "metadata": {
   "@type": "type.googleapis.com/google.cloud.aiplatform.v1.DeployIndexOperationMetadata",
   "genericMetadata": {
     "createTime": "2022-10-19T17:53:16.502088Z",
     "updateTime": "2022-10-19T17:53:16.502088Z"
   },
   "deployedIndexId": "DEPLOYED_INDEX_ID"
 }
}

Terraform

以下範例使用 vertex_ai_index_endpoint_deployed_index Terraform 資源建立已部署的索引端點。

如要瞭解如何套用或移除 Terraform 設定,請參閱「基本 Terraform 指令」。

provider "google" {
  region = "us-central1"
}

resource "google_vertex_ai_index_endpoint_deployed_index" "default" {
  depends_on        = [google_vertex_ai_index_endpoint.default]
  index_endpoint    = google_vertex_ai_index_endpoint.default.id
  index             = google_vertex_ai_index.default.id
  deployed_index_id = "deployed_index_for_psc"
}

resource "google_vertex_ai_index_endpoint" "default" {
  display_name = "sample-endpoint"
  description  = "A sample index endpoint with Private Service Connect enabled"
  region       = "us-central1"
  private_service_connect_config {
    enable_private_service_connect = true
    project_allowlist = [
      data.google_project.project.project_id,
    ]
  }
}

data "google_project" "project" {}

# Cloud Storage bucket name must be unique
resource "random_id" "default" {
  byte_length = 8
}

# Create a Cloud Storage bucket
resource "google_storage_bucket" "bucket" {
  name                        = "vertex-ai-index-bucket-${random_id.default.hex}"
  location                    = "us-central1"
  uniform_bucket_level_access = true
}

# Create index content
resource "google_storage_bucket_object" "data" {
  name    = "contents/data.json"
  bucket  = google_storage_bucket.bucket.name
  content = <<EOF
{"id": "42", "embedding": [0.5, 1.0], "restricts": [{"namespace": "class", "allow": ["cat", "pet"]},{"namespace": "category", "allow": ["feline"]}]}
{"id": "43", "embedding": [0.6, 1.0], "restricts": [{"namespace": "class", "allow": ["dog", "pet"]},{"namespace": "category", "allow": ["canine"]}]}
EOF
}

resource "google_vertex_ai_index" "default" {
  region       = "us-central1"
  display_name = "sample-index-batch-update"
  description  = "A sample index for batch update"
  labels = {
    foo = "bar"
  }

  metadata {
    contents_delta_uri = "gs://${google_storage_bucket.bucket.name}/contents"
    config {
      dimensions                  = 2
      approximate_neighbors_count = 150
      distance_measure_type       = "DOT_PRODUCT_DISTANCE"
      algorithm_config {
        tree_ah_config {
          leaf_node_embedding_count    = 500
          leaf_nodes_to_search_percent = 7
        }
      }
    }
  }
  index_update_method = "BATCH_UPDATE"

  timeouts {
    create = "2h"
    update = "1h"
  }
}

Python 適用的 Vertex AI SDK

如要瞭解如何安裝或更新 Python 適用的 Vertex AI SDK,請參閱「安裝 Python 適用的 Vertex AI SDK」。 詳情請參閱 Vertex AI SDK for Python API 參考說明文件

def vector_search_deploy_index(
    project: str,
    location: str,
    index_name: str,
    index_endpoint_name: str,
    deployed_index_id: str,
) -> None:
    """Deploy a vector search index to a vector search index endpoint.

    Args:
        project (str): Required. Project ID
        location (str): Required. The region name
        index_name (str): Required. The index to update. A fully-qualified index
          resource name or a index ID.  Example:
          "projects/123/locations/us-central1/indexes/my_index_id" or
          "my_index_id".
        index_endpoint_name (str): Required. Index endpoint to deploy the index
          to.
        deployed_index_id (str): Required. The user specified ID of the
          DeployedIndex.
    """
    # Initialize the Vertex AI client
    aiplatform.init(project=project, location=location)

    # Create the index instance from an existing index
    index = aiplatform.MatchingEngineIndex(index_name=index_name)

    # Create the index endpoint instance from an existing endpoint.
    index_endpoint = aiplatform.MatchingEngineIndexEndpoint(
        index_endpoint_name=index_endpoint_name
    )

    # Deploy Index to Endpoint
    index_endpoint = index_endpoint.deploy_index(
        index=index, deployed_index_id=deployed_index_id
    )

    print(index_endpoint.deployed_indexes)

控制台

請按照下列操作說明部署索引。

  1. 在 Google Cloud 控制台的 Vertex AI 專區中,前往「部署及使用」專區。選取「向量搜尋」

    前往 Vector Search。系統會顯示有效索引清單。

  2. 選取要部署的索引名稱。系統會開啟索引詳細資料頁面。
  3. 在索引詳細資料頁面中,按一下「Deploy to endpoint」。索引部署面板隨即開啟。
  4. 輸入顯示名稱 - 這個名稱會做為 ID,無法更新。
  5. 在「Endpoint」下拉式選單中,選取要部署這個索引的端點。 注意:如果索引已部署至端點,則無法使用該端點。
  6. 選用:在「Machine type」欄位中,選取「standard」或「high-memory」。
  7. (選用步驟) 選取「啟用自動調度資源」,即可根據工作負載需求自動調整節點數量。如已停用自動調度資源功能,備用資源的預設數量為 2。
  8. 按一下「Deploy」,將索引部署至端點。注意:部署作業大約需要 30 分鐘。

在虛擬私有雲專案中建立轉送規則

索引部署作業完成後,索引端點會傳回服務附件 URI,而非 IP 位址。您需要建立運算子位址,並在 VPC 專案中建立轉送規則,以便使用已建立的運算子位址指定服務附件。如要建立運算位址,請使用以下範例:

gcloud compute addresses create ${ADDRESS_NAME:?} \
    --region=${REGION:?} \
    --subnet=${SUBNET_NAME:?} \
    --project=${VPC_PROJECT:?}

如要使用已建立的運算地址,建立以服務連結 URI 為目標的轉送規則,請使用以下範例:

SERVICE_ATTACHMENT_URI=`gcloud ai index-endpoints describe {INDEX_ENDPOINT_ID}
--format="value(deployedIndexes.privateEndpoints.serviceAttachment)"`

gcloud compute forwarding-rules create ${ENDPOINT_NAME:?} \
    --network=${NETWORK_NAME:?} \
    --address=${ADDRESS_NAME:?} \
    --target-service-attachment=${SERVICE_ATTACHMENT_URI:?} \
    --project=${VPC_PROJECT:?} \
    --region=${REGION:?}

(選用) 建立 IP 位址的 DNS 記錄

如果您不想記住實際的 IP 位址,也可以建立 DNS 記錄來連線及載入。本步驟為選擇性步驟。

DNS_NAME_SUFFIX=matchingengine.vertexai.goog. # Don't forget the "." in the end.
DNS_NAME=${INDEX_ENDPOINT_ID:?}.${REGION:?}.${DNS_NAME_SUFFIX:?}

gcloud dns managed-zones create ${DNS_ZONE_NAME:?} \
    --dns-name=${DNS_NAME_SUFFIX:?} \
    --visibility=private \
    --project=${VPC_PROJECT:?} \
    --region=${REGION:?}

gcloud dns record-sets create ${DNS_NAME:?} \
    --rrdatas=${IP_ADDRESS:?} \
    --type=A --ttl=60 \
    --zone=${DNS_ZONE_NAME:?} \
    --project=${VPC_PROJECT:?} \
    --region=${REGION:?}

將查詢傳送至索引端點

您已使用 Private Service Connect 建立端點並建立索引,現在可以開始執行查詢。

如要查詢索引,請參閱「查詢索引以取得最近鄰」。

後續步驟