部署及管理公開索引端點

您必須先完成幾個步驟,才能查詢索引:

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

建立 IndexEndpoint

gcloud

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

  • INDEX_ENDPOINT_NAME:索引端點的顯示名稱。
  • LOCATION:您使用 Vertex AI 的區域。
  • PROJECT_ID:您的 Google Cloud 專案 ID

執行下列指令:

Linux、macOS 或 Cloud Shell

gcloud ai index-endpoints create \
    --display-name=INDEX_ENDPOINT_NAME \
    --public-endpoint-enabled \
    --region=LOCATION \
    --project=PROJECT_ID

Windows (PowerShell)

gcloud ai index-endpoints create `
    --display-name=INDEX_ENDPOINT_NAME `
    --public-endpoint-enabled `
    --region=LOCATION `
    --project=PROJECT_ID

Windows (cmd.exe)

gcloud ai index-endpoints create ^
    --display-name=INDEX_ENDPOINT_NAME ^
    --public-endpoint-enabled ^
    --region=LOCATION ^
    --project=PROJECT_ID

REST

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

  • INDEX_ENDPOINT_NAME:索引端點的顯示名稱。
  • LOCATION:您使用 Vertex AI 的區域。
  • 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",
 "publicEndpointEnabled": "true"
}

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

您應該會收到如下的 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 with a public endpoint"
  region                  = "us-central1"
  public_endpoint_enabled = true
}

# 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_create_index_endpoint(
    project: str, location: str, display_name: str
) -> None:
    """Create a vector search index endpoint.

    Args:
        project (str): Required. Project ID
        location (str): Required. The region name
        display_name (str): Required. The index endpoint display name
    """
    # Initialize the Vertex AI client
    aiplatform.init(project=project, location=location)

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

    print(index_endpoint.name)

控制台

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

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

    前往 Vector Search

  2. 系統會顯示有效索引清單。
  3. 選取頁面頂端的「索引端點」分頁標籤。系統會顯示索引端點。
  4. 按一下「建立新的索引端點」。系統會開啟「建立新的索引端點」面板。
  5. 輸入索引端點的顯示名稱。
  6. 在「Region」欄位中,從下拉式選單中選取所需地區。
  7. 在「Access」欄位中,選取「Standard」
  8. 按一下 [建立]。

將索引部署至端點

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_id"
}

resource "google_vertex_ai_index_endpoint" "default" {
  display_name            = "sample-endpoint"
  description             = "A sample index endpoint with a public endpoint"
  region                  = "us-central1"
  public_endpoint_enabled = true
}

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

取得索引網域名稱

索引部署完成後,您需要網域名稱才能使用索引進行線上查詢。該值位於 publicEndpointDomainName 下方。

curl -H "Content-Type: application/json" -H "Authorization: Bearer `gcloud auth print-access-token`"  ${ENDPOINT}/v1/projects/${PROJECT_ID}/locations/${REGION}/indexEndpoints/${INDEX_ENDPOINT_ID}

回應範例

{
  "name": "projects/181224308459/locations/us-central1/indexEndpoints/3370566089086861312",
  "displayName": "public-endpoint-test1",
  "deployedIndexes": [
    {
      "id": "test_index_public1",
      "index": "projects/181224308459/locations/us-central1/indexes/7733428228102029312",
      "displayName": "test_index_public1",
      "createTime": "2023-02-08T23:19:58.026843Z",
      "indexSyncTime": "2023-02-09T05:26:19.309417Z",
      "automaticResources": {
        "minReplicaCount": 2,
        "maxReplicaCount": 2
      },
      "deploymentGroup": "default"
    }
  ],
  "etag": "AMEw9yNkXQcSke8iqW9SYxfhj_hT9GCwPt1XlxVwJRSCxiXOYnG4CKrZM_X0oH-XN8tR",
  "createTime": "2023-02-08T22:44:20.285382Z",
  "updateTime": "2023-02-08T22:44:26.515162Z",
  "publicEndpointDomainName": "1957880287.us-central1-181224308459.vdb.vertexai.goog"
}

啟用自動調度資源

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

如要啟用自動調度資源,請在部署索引時指定 maxReplicaCountminReplicaCount

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:您使用 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_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:您使用 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_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

如要瞭解如何安裝或更新 Python 適用的 Vertex AI SDK,請參閱「安裝 Python 適用的 Vertex AI SDK」。 詳情請參閱 Vertex AI SDK for 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 控制台的 Vertex AI 專區中,前往「部署及使用」專區。選取「向量搜尋」

    前往 Vector Search

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

DeployedIndex 進行突變

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

  • 索引部署後,使用者不得變更 machineType
  • 如果要求中未指定 maxReplicaCountDeployedIndex 會繼續使用現有的 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:您使用 Vertex AI 的區域。
  • 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:您使用 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: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"
  # 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 with a public endpoint"
  region                  = "us-central1"
  public_endpoint_enabled = true
}

# 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_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,
    )

影響效能的部署設定

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

設定 效能影響
機型

硬體選項會直接影響所選的分片大小。視您在索引建立時指定的分割選項而定,每個機器類型都會在效能和成本之間取得平衡。

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

  • E2 標準
  • E2 highmem
  • N1 標準
  • N2D 標準
備用資源數量下限

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

如果工作負載會降至低水準,然後迅速增加至較高水準,建議您將 minReplicaCount 設為可因應初期流量激增的數字。

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

清單 IndexEndpoints

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

gcloud

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

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

  • LOCATION:您使用 Vertex AI 的區域。
  • 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:您使用 Vertex AI 的區域。
  • 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

如要瞭解如何安裝或更新 Python 適用的 Vertex AI SDK,請參閱「安裝 Python 適用的 Vertex AI SDK」。 詳情請參閱 Vertex AI SDK for 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 控制台的 Vertex AI 專區中,前往「部署及使用」專區。選取「向量搜尋」

    前往 Vector Search

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

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

取消部署索引

如要從端點解除部署索引,請執行下列程式碼:

gcloud

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

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

  • INDEX_ENDPOINT_ID:索引端點的 ID。
  • DEPLOYED_INDEX_ID:使用者指定的字串,用於唯一識別已部署的索引。開頭必須是英文字母,且只能包含英文字母、數字或底線。如需格式規範,請參閱 DeployedIndex.id
  • LOCATION:您使用 Vertex AI 的區域。
  • 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:您使用 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: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

如要瞭解如何安裝或更新 Python 適用的 Vertex AI SDK,請參閱「安裝 Python 適用的 Vertex AI SDK」。 詳情請參閱 Vertex AI SDK for 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 控制台的 Vertex AI 專區中,前往「部署及使用」專區。選取「向量搜尋」

    前往 Vector Search

  2. 系統會顯示有效索引清單。
  3. 選取要取消部署的索引。系統會開啟索引詳細資料頁面。
  4. 在「已部署的索引」部分下方,找出要取消部署的索引版本。
  5. 按一下與索引同列的 選項選單,然後選取「Undeploy」
  6. 系統會顯示確認畫面。按一下「Undeploy」(取消部署)。注意:最多可能需要 30 分鐘才能完成解除部署。

刪除 IndexEndpoint

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

gcloud

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

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

  • INDEX_ENDPOINT_ID:索引端點的 ID。
  • LOCATION:您使用 Vertex AI 的區域。
  • 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:您使用 Vertex AI 的區域。
  • 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

如要瞭解如何安裝或更新 Python 適用的 Vertex AI SDK,請參閱「安裝 Python 適用的 Vertex AI SDK」。 詳情請參閱 Vertex AI SDK for 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 控制台的 Vertex AI 專區中,前往「部署及使用」專區。選取「向量搜尋」

    前往 Vector Search

  2. 選取頁面頂端的「索引端點」分頁標籤。
  3. 系統會顯示所有現有的索引端點。
  4. 按一下與要刪除的索引位於同一列的選項選單,然後選取「Delete」
  5. 系統會顯示確認畫面。按一下 [Delete] (刪除),索引端點已刪除。