在虛擬私有雲網路中部署及管理索引端點

將索引部署至端點包含下列三項工作:

  1. 視需要建立 IndexEndpoint,或重複使用現有 IndexEndpoint。
  2. 取得 IndexEndpoint ID。
  3. 將索引部署至 IndexEndpoint。

在虛擬私有雲網路中建立IndexEndpoint

如要將 Index 部署至現有的 IndexEndpoint,可以略過這個步驟。

您必須先將 Index 部署至虛擬私有雲網路對等互連網路中的 IndexEndpoint,才能使用索引提供線上向量比對查詢。第一步是建立 IndexEndpoint。您可以將多個索引部署至共用相同虛擬私有雲網路的IndexEndpoint。

gcloud

以下範例使用 gcloud ai index-endpoints create 指令。

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

  • INDEX_ENDPOINT_NAME:索引端點的顯示名稱。
  • VPC_NETWORK_NAME:應與索引端點對等互連的 Compute Engine 網路名稱。
  • LOCATION:您使用 Agent Platform 的區域。
  • PROJECT_ID:您的 Google Cloud 專案 ID。

執行下列指令:

Linux、macOS 或 Cloud Shell

gcloud ai index-endpoints create \
    --display-name=INDEX_ENDPOINT_NAME \
    --network=VPC_NETWORK_NAME \
    --region=LOCATION \
    --project=PROJECT_ID

Windows (PowerShell)

gcloud ai index-endpoints create `
    --display-name=INDEX_ENDPOINT_NAME `
    --network=VPC_NETWORK_NAME `
    --region=LOCATION `
    --project=PROJECT_ID

Windows (cmd.exe)

gcloud ai index-endpoints create ^
    --display-name=INDEX_ENDPOINT_NAME ^
    --network=VPC_NETWORK_NAME ^
    --region=LOCATION ^
    --project=PROJECT_ID

您應該會收到類似以下的回應:

The Google Cloud CLI tool might take a few minutes to create the IndexEndpoint.

REST

使用任何要求資料之前,請先修改下列項目的值:

  • INDEX_ENDPOINT_NAME:索引端點的顯示名稱。
  • VPC_NETWORK_NAME:應與索引端點對等互連的 Compute Engine 網路名稱。
  • LOCATION:您使用 Agent Platform 的區域。
  • PROJECT_ID:您的 Google Cloud 專案 ID。
  • PROJECT_NUMBER:系統自動產生的專案編號。

HTTP 方法和網址:

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

JSON 要求主體:

{
  "display_name": "INDEX_ENDPOINT_NAME",
  "network": "VPC_NETWORK_NAME"
}

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

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

{
  "name": "projects/PROJECT_NUMBER/locations/LOCATION/indexEndpoints/INDEX_ENDPOINT_ID/operations/OPERATION_ID",
  "metadata": {
    "@type": "type.googleapis.com/google.cloud.aiplatform.v1.CreateIndexEndpointOperationMetadata",
    "genericMetadata": {
      "createTime": "2022-01-13T04:09:56.641107Z",
      "updateTime": "2022-01-13T04:09:56.641107Z"
    }
  }
}

您可以輪詢作業狀態,直到回應包含 "done": true 為止。

Terraform

下列範例使用 vertex_ai_index_endpoint Terraform 資源建立索引端點。

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

resource "google_vertex_ai_index_endpoint" "default" {
  display_name = "sample-endpoint"
  description  = "A sample index endpoint within a VPC network"
  region       = "us-central1"
  network      = "projects/${data.google_project.project.number}/global/networks/${google_compute_network.default.name}"
  depends_on = [
    google_service_networking_connection.default
  ]
}

resource "google_service_networking_connection" "default" {
  network                 = google_compute_network.default.id
  service                 = "servicenetworking.googleapis.com"
  reserved_peering_ranges = [google_compute_global_address.default.name]
  # Workaround to allow `terraform destroy`, see https://github.com/hashicorp/terraform-provider-google/issues/18729
  deletion_policy = "ABANDON"
}

resource "google_compute_global_address" "default" {
  name          = "sample-address"
  purpose       = "VPC_PEERING"
  address_type  = "INTERNAL"
  prefix_length = 16
  network       = google_compute_network.default.id
}

resource "google_compute_network" "default" {
  name = "sample-network"
}

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 for Python,請參閱「安裝 Vertex AI SDK for Python」。詳情請參閱 Python API 參考文件。

def vector_search_create_index_endpoint_vpc(
    project: str, location: str, display_name: str, network: str
) -> aiplatform.MatchingEngineIndexEndpoint:
    """Create a vector search index endpoint within a VPC network.

    Args:
        project (str): Required. Project ID
        location (str): Required. The region name
        display_name (str): Required. The index endpoint display name
        network(str): Required. The VPC network name, in the format of
            projects/{project number}/global/networks/{network name}.

    Returns:
        aiplatform.MatchingEngineIndexEndpoint - The created index endpoint.
    """
    # Initialize the Vertex AI client
    aiplatform.init(project=project, location=location)

    # Create Index Endpoint
    index_endpoint = aiplatform.MatchingEngineIndexEndpoint.create(
        display_name=display_name,
        network=network,
        description="Matching Engine VPC Index Endpoint",
    )

    return index_endpoint

控制台

請按照下列操作說明建立索引端點。

  1. 前往 Google Cloud 控制台 Agent Platform 專區的「Deploy and Use」(部署及使用) 部分。選取「向量搜尋」

    前往 Vector Search

  2. 系統會顯示有效索引清單。
  3. 選取頁面頂端的「Index endpoints」(索引端點) 分頁標籤。系統會顯示索引端點。
  4. 按一下「 建立新的索引端點」。「Create a new index endpoint」(建立新的索引端點) 面板隨即開啟。
  5. 輸入索引端點的顯示名稱。
  6. 在「Region」(區域) 欄位中,從下拉式選單選取區域。
  7. 在「Access」(存取權) 欄位中,選取「Private」(私人)。
  8. 輸入對等互連虛擬私有雲網路詳細資料。輸入應與工作對等互連的 Compute Engine 網路全名。格式應為 projects/{project_num}/global/networks/{network_id}
  9. 點選「建立」。

部署索引

gcloud

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

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

  • INDEX_ENDPOINT_ID:索引端點的 ID。
  • DEPLOYED_INDEX_ID:使用者指定的字串,用於專門識別已部署的索引。開頭須為英文字母,且只能使用英文字母、數字或底線。如需格式規範,請參閱 DeployedIndex.id。
  • DEPLOYED_INDEX_ENDPOINT_NAME:已部署索引端點的顯示名稱。
  • INDEX_ID:索引的 ID。
  • LOCATION:您使用 Agent Platform 的區域。
  • 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

您應該會收到類似以下的回應:

The Google Cloud CLI tool might take a few minutes to create the IndexEndpoint.

REST

使用任何要求資料之前,請先修改下列項目的值:

  • INDEX_ENDPOINT_ID:索引端點的 ID。
  • DEPLOYED_INDEX_ID:使用者指定的字串,用於專門識別已部署的索引。開頭須為英文字母,且只能使用英文字母、數字或底線。如需格式規範,請參閱 DeployedIndex.id。
  • DEPLOYED_INDEX_ENDPOINT_NAME:已部署索引端點的顯示名稱。
  • INDEX_ID:索引的 ID。
  • LOCATION:您使用 Agent Platform 的區域。
  • 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_vpc"
}

resource "google_vertex_ai_index_endpoint" "default" {
  display_name = "sample-endpoint"
  description  = "A sample index endpoint within a VPC network"
  region       = "us-central1"
  network      = "projects/${data.google_project.project.number}/global/networks/${google_compute_network.default.name}"
  depends_on = [
    google_service_networking_connection.default
  ]
}

resource "google_service_networking_connection" "default" {
  network                 = google_compute_network.default.id
  service                 = "servicenetworking.googleapis.com"
  reserved_peering_ranges = [google_compute_global_address.default.name]
  # Workaround to allow `terraform destroy`, see https://github.com/hashicorp/terraform-provider-google/issues/18729
  deletion_policy = "ABANDON"
}

resource "google_compute_global_address" "default" {
  name          = "sample-address"
  purpose       = "VPC_PEERING"
  address_type  = "INTERNAL"
  prefix_length = 16
  network       = google_compute_network.default.id
}

resource "google_compute_network" "default" {
  name = "sample-network"
}

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 for Python,請參閱「安裝 Vertex AI SDK for Python」。詳情請參閱 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 控制台 Agent Platform 專區的「Deploy and Use」(部署及使用) 部分。選取「向量搜尋」

    前往 Vector Search

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

啟用自動調度資源

Vector Search 支援自動調度資源,可依據工作負載需求,自動調整節點數量。當需求過高時,系統會在節點集區中新增節點,但不會超過您指定的大小上限。當需求較低時,節點集區會縮減至您指定的最小規模。您可以監控目前的副本,查看實際使用的節點和變更。

如要啟用自動調度資源功能,請在部署索引時指定 maxReplicaCount 和 minReplicaCount:

gcloud

以下範例使用 gcloud ai index-endpoints deploy-index 指令。

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

  • INDEX_ENDPOINT_ID:索引端點的 ID。
  • DEPLOYED_INDEX_ID:使用者指定的字串,用於專門識別已部署的索引。開頭須為英文字母,且只能使用英文字母、數字或底線。如需格式規範,請參閱 DeployedIndex.id。
  • DEPLOYED_INDEX_NAME:已部署索引的顯示名稱。
  • INDEX_ID:索引的 ID。
  • MIN_REPLICA_COUNT:無論何時,已部署的索引至少會部署於這個數量的機器副本。指定的值必須大於或等於 1。
  • MAX_REPLICA_COUNT:已部署的索引最多可以部署於多少項機器備用資源。
  • LOCATION:您使用 Agent Platform 的區域。
  • 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_NAME \
    --index=INDEX_ID \
    --min-replica-count=MIN_REPLICA_COUNT \
    --max-replica-count=MAX_REPLICA_COUNT \
    --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_NAME `
    --index=INDEX_ID `
    --min-replica-count=MIN_REPLICA_COUNT `
    --max-replica-count=MAX_REPLICA_COUNT `
    --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_NAME ^
    --index=INDEX_ID ^
    --min-replica-count=MIN_REPLICA_COUNT ^
    --max-replica-count=MAX_REPLICA_COUNT ^
    --region=LOCATION ^
    --project=PROJECT_ID

REST

使用任何要求資料之前,請先修改下列項目的值:

  • INDEX_ENDPOINT_ID:索引端點的 ID。
  • DEPLOYED_INDEX_ID:使用者指定的字串,用於專門識別已部署的索引。開頭須為英文字母,且只能使用英文字母、數字或底線。如需格式規範,請參閱 DeployedIndex.id。
  • DEPLOYED_INDEX_NAME:已部署索引的顯示名稱。
  • INDEX_ID:索引的 ID。
  • MIN_REPLICA_COUNT:無論何時,已部署的索引至少會部署於這個數量的機器副本。指定的值必須大於或等於 1。
  • MAX_REPLICA_COUNT:已部署的索引最多可以部署於多少項機器備用資源。
  • LOCATION:您使用 Agent Platform 的區域。
  • 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_NUMBER/locations/LOCATION/indexes/INDEX_ID",
   "displayName": "DEPLOYED_INDEX_NAME",
   "automaticResources": {
     "minReplicaCount": MIN_REPLICA_COUNT,
     "maxReplicaCount": MAX_REPLICA_COUNT
   }
 }
}

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

您應該會收到如下的 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": "2023-10-19T17:53:16.502088Z",
     "updateTime": "2023-10-19T17:53:16.502088Z"
   },
   "deployedIndexId": "DEPLOYED_INDEX_ID"
 }
}

Python

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

def vector_search_deploy_autoscaling_index(
    project: str,
    location: str,
    index_name: str,
    index_endpoint_name: str,
    deployed_index_id: str,
    min_replica_count: int,
    max_replica_count: int,
) -> 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.
        min_replica_count (int): Required. The minimum number of replicas to
          deploy.
        max_replica_count (int): Required. The maximum number of replicas to
          deploy.
    """
    # 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. Specifying min and max replica counts will
    # enable autoscaling.
    index_endpoint.deploy_index(
        index=index,
        deployed_index_id=deployed_index_id,
        min_replica_count=min_replica_count,
        max_replica_count=max_replica_count,
    )

控制台

您只能在部署索引時,透過控制台啟用自動調度資源功能。

  1. 前往 Google Cloud 控制台 Agent Platform 專區的「Deploy and Use」(部署及使用) 部分。選取「向量搜尋」

    前往 Vector Search

  2. 系統會顯示有效索引清單。
  3. 選取要部署的索引名稱。索引詳細資料頁面隨即開啟。
  4. 在索引詳細資料頁面,點選「 Deploy to endpoint」(部署至端點)。索引部署面板隨即開啟。
  5. 輸入顯示名稱。這個名稱會做為 ID,無法更新。
  6. 從「Endpoint」(端點) 下拉式選單中,選取要部署這個索引的端點。 注意:如果索引已部署至端點,就無法使用該端點。
  7. 選用:在「Machine type」(機型) 欄位中,選取「standard」或「high-memory」。
  8. 選用。選取「Enable autoscaling」(啟用自動調度資源),即可依據工作負載需求,自動調整節點數量。如已停用自動調度資源功能,副本的預設數量為 2。
  • 如果未設定 minReplicaCount 和 maxReplicaCount,則預設值為 2。
  • 如果只設定 maxReplicaCount,minReplicaCount 預設會設為 2。
  • 如果只設定 minReplicaCount,maxReplicaCount 會設為等於 minReplicaCount。

更改DeployedIndex

您可以使用 MutateDeployedIndex API,更新已部署索引的部署資源 (例如 minReplicaCount 和 maxReplicaCount)。

  • 索引部署後,使用者不得變更 machineType。
  • 如果要求中未指定 maxReplicaCount,DeployedIndex 會繼續使用現有的 maxReplicaCount。

gcloud

以下範例使用 gcloud ai index-endpoints mutate-deployed-index 指令。

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

  • INDEX_ENDPOINT_ID:索引端點的 ID。
  • DEPLOYED_INDEX_ID:使用者指定的字串,用於專門識別已部署的索引。開頭須為英文字母,且只能使用英文字母、數字或底線。如需格式規範,請參閱 DeployedIndex.id。
  • MIN_REPLICA_COUNT:無論何時,已部署的索引至少會部署於這個數量的機器副本。指定的值必須大於或等於 1。
  • MAX_REPLICA_COUNT:已部署的索引最多可以部署於多少項機器備用資源。
  • LOCATION:您使用 Agent Platform 的區域。
  • PROJECT_ID:您的 Google Cloud 專案 ID。

執行下列指令:

Linux、macOS 或 Cloud Shell

gcloud ai index-endpoints mutate-deployed-index INDEX_ENDPOINT_ID \
    --deployed-index-id=DEPLOYED_INDEX_ID \
    --min-replica-count=MIN_REPLICA_COUNT \
    --max-replica-count=MAX_REPLICA_COUNT \
    --region=LOCATION \
    --project=PROJECT_ID

Windows (PowerShell)

gcloud ai index-endpoints mutate-deployed-index INDEX_ENDPOINT_ID `
    --deployed-index-id=DEPLOYED_INDEX_ID `
    --min-replica-count=MIN_REPLICA_COUNT `
    --max-replica-count=MAX_REPLICA_COUNT `
    --region=LOCATION `
    --project=PROJECT_ID

Windows (cmd.exe)

gcloud ai index-endpoints mutate-deployed-index INDEX_ENDPOINT_ID ^
    --deployed-index-id=DEPLOYED_INDEX_ID ^
    --min-replica-count=MIN_REPLICA_COUNT ^
    --max-replica-count=MAX_REPLICA_COUNT ^
    --region=LOCATION ^
    --project=PROJECT_ID

REST

使用任何要求資料之前,請先修改下列項目的值:

  • INDEX_ENDPOINT_ID:索引端點的 ID。
  • DEPLOYED_INDEX_ID:使用者指定的字串,用於專門識別已部署的索引。開頭須為英文字母,且只能使用英文字母、數字或底線。如需格式規範,請參閱 DeployedIndex.id。
  • MIN_REPLICA_COUNT:無論何時,已部署的索引至少會部署於這個數量的機器副本。指定的值必須大於或等於 1。
  • MAX_REPLICA_COUNT:已部署的索引最多可以部署於多少項機器備用資源。
  • LOCATION:您使用 Agent Platform 的區域。
  • PROJECT_ID:您的 Google Cloud 專案 ID。
  • PROJECT_NUMBER:系統自動產生的專案編號。

HTTP 方法和網址:

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

JSON 要求主體:

{
  "deployedIndex": {
    "id": "DEPLOYED_INDEX_ID",
    "index": "projects/PROJECT_ID/locations/LOCATION/indexes/INDEX_ID",
    "displayName": "DEPLOYED_INDEX_NAME",
    "min_replica_count": "MIN_REPLICA_COUNT",
    "max_replica_count": "MAX_REPLICA_COUNT"
  }
}

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

您應該會收到如下的 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": "2020-10-19T17:53:16.502088Z",
    "updateTime": "2020-10-19T17:53:16.502088Z"
  },
  "deployedIndexId": "DEPLOYED_INDEX_ID"
}
}

Terraform

如要瞭解如何套用或移除 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_mutate_vpc"
  # This example assumes the deployed index endpoint's resources configuration
  # differs from the values specified below. Terraform will mutate the deployed
  # index endpoint's resource configuration to match.
  automatic_resources {
    min_replica_count = 3
    max_replica_count = 5
  }
}

resource "google_vertex_ai_index_endpoint" "default" {
  display_name = "sample-endpoint"
  description  = "A sample index endpoint within a VPC network"
  region       = "us-central1"
  network      = "projects/${data.google_project.project.number}/global/networks/${google_compute_network.default.name}"
  depends_on = [
    google_service_networking_connection.default
  ]
}

resource "google_service_networking_connection" "default" {
  network                 = google_compute_network.default.id
  service                 = "servicenetworking.googleapis.com"
  reserved_peering_ranges = [google_compute_global_address.default.name]
  # Workaround to allow `terraform destroy`, see https://github.com/hashicorp/terraform-provider-google/issues/18729
  deletion_policy = "ABANDON"
}

resource "google_compute_global_address" "default" {
  name          = "sample-address"
  purpose       = "VPC_PEERING"
  address_type  = "INTERNAL"
  prefix_length = 16
  network       = google_compute_network.default.id
}

resource "google_compute_network" "default" {
  name = "sample-network"
}

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 for Python,請參閱「安裝 Vertex AI SDK for Python」。詳情請參閱 Python API 參考文件。

def vector_search_mutate_deployed_index(
    project: str,
    location: str,
    index_endpoint_name: str,
    deployed_index_id: str,
    min_replica_count: int,
    max_replica_count: int,
) -> None:
    """Mutate the deployment resources of an already deployed 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.
        deployed_index_id (str): Required. The ID of the DeployedIndex to run
          the queries against.
        min_replica_count (int): Required. The minimum number of replicas to
          deploy.
        max_replica_count (int): Required. The maximum number of replicas to
          deploy.
    """
    # Initialize the Vertex AI client
    aiplatform.init(project=project, location=location)

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

    # Mutate the deployed index
    index_endpoint.mutate_deployed_index(
        deployed_index_id=deployed_index_id,
        min_replica_count=min_replica_count,
        max_replica_count=max_replica_count,
    )

影響效能的部署作業設定

使用向量搜尋時,下列部署設定可能會影響延遲時間、可用性和費用。這項指引適用於大多數情況。不過,請務必測試設定,確保設定適用於您的用途。

設定 效能影響
機型

硬體選擇會直接與所選 shard 大小互動。視您在建立索引時指定的 shard 選擇而定,每種機器類型在效能和成本之間各有優缺點。

請參閱定價頁面,瞭解可用的硬體和定價。一般來說,效能會依下列順序提升:

  • E2 標準
  • E2 高記憶體
  • N1 標準
  • N2D 標準
副本數量下限

minReplicaCount 會保留最低容量,確保可用性和延遲時間,避免系統在流量從低點快速增加時發生冷啟動問題。

如果工作負載會降至低水平,然後快速增加至較高水平,建議將 minReplicaCount 設為可容納初始流量爆發的數字。

備用資源數量上限 maxReplicaCount 主要用於控管用量成本。您可以選擇避免費用超過特定門檻,但代價是延遲時間增加及可用性降低。

清單 IndexEndpoints

如要列出 IndexEndpoint 資源,並查看任何相關聯 DeployedIndex 執行個體的資訊,請執行下列程式碼:

gcloud

以下範例使用 gcloud ai index-endpoints list 指令。

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

  • LOCATION:您使用 Agent Platform 的區域。
  • PROJECT_ID:您的 Google Cloud 專案 ID。

執行下列指令:

Linux、macOS 或 Cloud Shell

gcloud ai index-endpoints list \
    --region=LOCATION \
    --project=PROJECT_ID

Windows (PowerShell)

gcloud ai index-endpoints list `
    --region=LOCATION `
    --project=PROJECT_ID

Windows (cmd.exe)

gcloud ai index-endpoints list ^
    --region=LOCATION ^
    --project=PROJECT_ID

REST

使用任何要求資料之前,請先修改下列項目的值:

  • LOCATION:您使用 Agent Platform 的區域。
  • PROJECT_ID:您的 Google Cloud 專案 ID。
  • PROJECT_NUMBER:系統自動產生的專案編號。

HTTP 方法和網址:

GET https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/indexEndpoints

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

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

{
 "indexEndpoints": [
   {
     "name": "projects/PROJECT_NUMBER/locations/LOCATION/indexEndpoints/INDEX_ENDPOINT_ID",
     "displayName": "INDEX_ENDPOINT_DISPLAY_NAME",
     "deployedIndexes": [
       {
         "id": "DEPLOYED_INDEX_ID",
         "index": "projects/PROJECT_NUMBER/locations/LOCATION/indexes/INDEX_ID",
         "displayName": "DEPLOYED_INDEX_DISPLAY_NAME",
         "createTime": "2021-06-04T02:23:40.178286Z",
         "privateEndpoints": {
           "matchGrpcAddress": "GRPC_ADDRESS"
         },
         "indexSyncTime": "2022-01-13T04:22:00.151916Z",
         "automaticResources": {
           "minReplicaCount": 2,
           "maxReplicaCount": 10
         }
       }
     ],
     "etag": "AMEw9yP367UitPkLo-khZ1OQvqIK8Q0vLAzZVF7QjdZ5O3l7Zow-mzBo2l6xmiuuMljV",
     "createTime": "2021-03-17T04:47:28.460373Z",
     "updateTime": "2021-06-04T02:23:40.930513Z",
     "network": "VPC_NETWORK_NAME"
   }
 ]
}

Python

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

def vector_search_list_index_endpoint(
    project: str, location: str
) -> List[aiplatform.MatchingEngineIndexEndpoint]:
    """List vector search index endpoints.

    Args:
        project (str): Required. Project ID
        location (str): Required. The region name

    Returns:
        List of aiplatform.MatchingEngineIndexEndpoint
    """
    # Initialize the Vertex AI client
    aiplatform.init(project=project, location=location)

    # List Index Endpoints
    return aiplatform.MatchingEngineIndexEndpoint.list()

控制台

請按照下列操作說明查看索引端點清單。

  1. 前往 Google Cloud 控制台 Agent Platform 專區的「Deploy and Use」(部署及使用) 部分。選取「向量搜尋」

    前往 Vector Search

  2. 選取頁面頂端的「Index endpoint」(索引端點) 分頁標籤。
  3. 系統會顯示所有現有的索引端點。

詳情請參閱 IndexEndpoint 的參考文件。

取消部署索引

如要取消部署索引,請執行下列程式碼:

gcloud

以下範例使用 gcloud ai index-endpoints undeploy-index 指令。

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

  • INDEX_ENDPOINT_ID:索引端點的 ID。
  • DEPLOYED_INDEX_ID:使用者指定的字串,用於專門識別已部署的索引。開頭須為英文字母,且只能使用英文字母、數字或底線。如需格式規範,請參閱 DeployedIndex.id。
  • LOCATION:您使用 Agent Platform 的區域。
  • PROJECT_ID:您的 Google Cloud 專案 ID。

執行下列指令:

Linux、macOS 或 Cloud Shell

gcloud ai index-endpoints undeploy-index INDEX_ENDPOINT_ID \
    --deployed-index-id=DEPLOYED_INDEX_ID \
    --region=LOCATION \
    --project=PROJECT_ID

Windows (PowerShell)

gcloud ai index-endpoints undeploy-index INDEX_ENDPOINT_ID `
    --deployed-index-id=DEPLOYED_INDEX_ID `
    --region=LOCATION `
    --project=PROJECT_ID

Windows (cmd.exe)

gcloud ai index-endpoints undeploy-index INDEX_ENDPOINT_ID ^
    --deployed-index-id=DEPLOYED_INDEX_ID ^
    --region=LOCATION ^
    --project=PROJECT_ID

REST

使用任何要求資料之前,請先修改下列項目的值:

  • INDEX_ENDPOINT_ID:索引端點的 ID。
  • DEPLOYED_INDEX_ID:使用者指定的字串,用於專門識別已部署的索引。開頭須為英文字母,且只能使用英文字母、數字或底線。如需格式規範,請參閱 DeployedIndex.id。
  • LOCATION:您使用 Agent Platform 的區域。
  • PROJECT_ID:您的 Google Cloud 專案 ID。
  • PROJECT_NUMBER:系統自動產生的專案編號。

HTTP 方法和網址:

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

JSON 要求主體:

{
 "deployed_index_id": "DEPLOYED_INDEX_ID"
}

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

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

{
 "name": "projects/PROJECT_NUMBER/locations/LOCATION/indexEndpoints/INDEX_ENDPOINT_ID/operations/OPERATION_ID",
 "metadata": {
   "@type": "type.googleapis.com/google.cloud.aiplatform.v1.UndeployIndexOperationMetadata",
   "genericMetadata": {
     "createTime": "2022-01-13T04:09:56.641107Z",
     "updateTime": "2022-01-13T04:09:56.641107Z"
   }
 }
}

Python

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

def vector_search_undeploy_index(
    project: str,
    location: str,
    index_endpoint_name: str,
    deployed_index_id: str,
) -> None:
    """Mutate the deployment resources of an already deployed 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.
        deployed_index_id (str): Required. The ID of the DeployedIndex to run
          the queries against.
    """
    # Initialize the Vertex AI client
    aiplatform.init(project=project, location=location)

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

    # Undeploy the index
    index_endpoint.undeploy_index(
        deployed_index_id=deployed_index_id,
    )

控制台

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

  1. 前往 Google Cloud 控制台 Agent Platform 專區的「Deploy and Use」(部署及使用) 部分。選取「向量搜尋」

    前往 Vector Search

  2. 系統會顯示有效索引清單。
  3. 選取要取消部署的索引。索引詳細資料頁面隨即開啟。
  4. 在「Deployed indexes」(已部署的索引) 專區下方,找出要取消部署的索引端點。
  5. 按一下與索引端點位於同一列的選項選單,然後選取「Undeploy」(取消部署)。
  6. 確認畫面隨即開啟。按一下「Undeploy」(取消部署)。注意:取消部署作業最多可能需要 30 分鐘。

刪除IndexEndpoint

刪除 IndexEndpoint 之前,您必須undeploy部署至端點的所有索引。

gcloud

以下範例使用 gcloud ai index-endpoints delete 指令。

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

  • INDEX_ENDPOINT_ID:索引端點的 ID。
  • LOCATION:您使用 Agent Platform 的區域。
  • PROJECT_ID:您的 Google Cloud 專案 ID。

執行下列指令:

Linux、macOS 或 Cloud Shell

gcloud ai index-endpoints delete INDEX_ENDPOINT_ID \
    --region=LOCATION \
    --project=PROJECT_ID

Windows (PowerShell)

gcloud ai index-endpoints delete INDEX_ENDPOINT_ID `
    --region=LOCATION `
    --project=PROJECT_ID

Windows (cmd.exe)

gcloud ai index-endpoints delete INDEX_ENDPOINT_ID ^
    --region=LOCATION ^
    --project=PROJECT_ID

REST

使用任何要求資料之前,請先修改下列項目的值:

  • INDEX_ENDPOINT_ID:索引端點的 ID。
  • LOCATION:您使用 Agent Platform 的區域。
  • PROJECT_ID:您的 Google Cloud 專案 ID。
  • PROJECT_NUMBER:系統自動產生的專案編號。

HTTP 方法和網址:

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

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

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

{
 "name": "projects/PROJECT_NUMBER/locations/LOCATION/indexEndpoints/INDEX_ENDPOINT_ID/operations/OPERATION_ID",
 "metadata": {
   "@type": "type.googleapis.com/google.cloud.aiplatform.v1.DeleteOperationMetadata",
   "genericMetadata": {
     "createTime": "2022-01-13T04:36:19.142203Z",
     "updateTime": "2022-01-13T04:36:19.142203Z"
   }
 },
 "done": true,
 "response": {
   "@type": "type.googleapis.com/google.protobuf.Empty"
 }
}

Python

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

def vector_search_delete_index_endpoint(
    project: str, location: str, index_endpoint_name: str, force: bool = False
) -> None:
    """Delete a vector search index endpoint.

    Args:
        project (str): Required. Project ID
        location (str): Required. The region name
        index_endpoint_name (str): Required. Index endpoint to run the query
          against.
        force (bool): Required. If true, undeploy any deployed indexes on this
          endpoint before deletion.
    """
    # Initialize the Vertex AI client
    aiplatform.init(project=project, location=location)

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

    # Delete the index endpoint
    index_endpoint.delete(force=force)

控制台

請按照下列操作說明刪除索引端點。

  1. 前往 Google Cloud 控制台 Agent Platform 專區的「Deploy and Use」(部署及使用) 部分。選取「向量搜尋」

    前往 Vector Search

  2. 選取頁面頂端的「Index endpoints」(索引端點) 分頁標籤。
  3. 系統會顯示所有現有的索引端點。
  4. 按一下要刪除的索引端點所在資料列中的選項選單,然後選取「Delete」(刪除)。
  5. 確認畫面隨即開啟。按一下「Delete」(刪除)。索引端點已刪除。