使用 Private Service Connect 设置 Vector Search

Private Service Connect 允许跨属于不同群组、团队、项目和组织的 VPC 网络以不公开方式使用服务。您可以使用由您定义且位于您的 VPC 网络内部的 IP 地址发布和使用服务,以便 Vector Search 端点执行向量相似度搜索。

在 Vector Search 端点上启用 Private Service Connect 适用于以下使用场景:

  1. 要求低延迟,并与 Vector Search 服务后端建立安全连接。
  2. 专用 VPC 对等互连预留的 IP 空间有限。
  3. 需要从多个用户 VPC 网络访问服务后端。

如需详细了解如何设置 Private Service Connect,请参阅 Virtual Private Cloud (VPC) 文档中的 Private Service Connect 概览

创建索引端点

您必须在创建端点时启用 Private Service Connect。此过程类似于在 Vertex AI 中创建其他端点。

REST

  • PROJECT:要在其中创建 Vertex AI 资源的服务项目的 ID。

  • REGION:网络区域。

  • DISPLAY_NAME:为端点指定的显示名称。此名称将用于为端点创建 ID,日后无法更新。

  • VPC_PROJECTS:对于共享 VPC 设置,此参数是 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

  2. 选择索引端点标签页。系统会显示您的索引端点。

  3. 点击 创建新的端点。系统会打开创建新的索引端点面板。

  4. 显示名称中,输入索引端点的显示名称。此名称将用于为端点创建 ID,日后无法更新。

  5. 地区下拉列表中选择一个区域。

  6. 访问下,点击 Private Service Connect(预览版)

  7. 系统会显示一个文本字段,您可以在其中指定要使用的 VPC 项目。添加您要使用的 VPC 项目的 ID 或编号。

  8. 点击创建

索引部署选项简介

您可以通过自动或手动服务连接部署索引。

  • 使用 Private Service Connect 自动化功能进行部署:设置服务连接政策并部署索引。设置服务连接政策后,您无需每次都创建计算地址和转发规则,即可部署到特定网络。
  • 手动连接部署:部署索引,并手动创建计算地址和转发规则。如果您需要为同一服务附件 URI 使用多个 IP 地址,则可以选择此选项,但这不是常见的用例。

使用 Private Service Connect 自动化功能进行部署

您可以设置服务连接政策,这样便无需在每次部署索引后手动创建计算地址和转发规则。

  1. 首先,创建服务连接政策,指定要部署索引的网络、服务类和区域。此设置是一次性的。如果您已完成此操作,请跳至下一个步骤。
  2. 部署索引

限制

Automation 仅允许每个项目每个网络使用一个 IP 地址。如果您需要使用多个 IP 地址,请参阅手动部署索引

创建服务连接政策

您必须是网络管理员,才能创建服务连接政策以自动部署索引。

如需自动部署索引,请按以下步骤操作:

  1. 创建服务连接政策。

    • PROJECT:要在其中创建 Vertex AI 资源的服务项目。

    • VPC_PROJECT:您的客户端 VPC 所在的项目。对于单个 VPC 设置,此值与 $PROJECT 相同。对于共享 VPC 设置,这是 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:您的客户端 VPC 所在的项目。对于共享 VPC 设置,这是 VPC 宿主项目。

  • 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

  2. 点击要部署的启用了 Private Service Connect 的 Vector Search 端点的部署按钮。系统随即会显示部署索引滑出式面板。

  3. 显示名称中,输入已部署索引的显示名称。此名称用于创建 ID,以后无法更新。

  4. 点击端点,然后选择要部署到的索引端点。

  5. (可选)点击机器类型,手动选择要将该索引部署到哪种类型的机器。默认情况下,系统会根据索引分片大小自动选择机器类型。

现在,您需要添加 Private Service Connect 自动化配置。为此,请执行以下操作:

  1. PSC 自动化配置部分中,点击 Add new config(添加新配置)。

  2. 输入要连接到的 VPC 项目的名称。

  3. 输入要连接的 VPC 项目的网络名称。其格式必须为 projects/<project_number>/global/networks/<network_name>

  4. (可选)点击 Add new config(添加新配置),然后继续添加 Private Service Connect 自动化配置。

  5. 点击部署以完成。

删除服务连接政策

如果您需要删除服务连接政策,请运行以下命令:

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

如需了解如何安装或更新 Vertex AI SDK for Python,请参阅安装 Vertex AI SDK for Python。 如需了解详情,请参阅 Python 版 Vertex AI SDK 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

    前往 Vector Search。系统会显示活跃索引列表。

  2. 选择您要部署的索引的名称。系统会打开索引详情页面。
  3. 在索引详情页面中,点击 部署到端点。系统会打开索引部署面板。
  4. 输入显示名称,此名称充当 ID 且无法更新。
  5. 端点下拉列表中,选择要将此索引部署到的端点。注意:如果索引已部署到该端点,则该端点不可用。
  6. 可选:在机器类型字段中,选择标准内存或高内存。
  7. 可选。选择启用自动扩缩功能,以根据工作负载的需求自动调整节点数量。如果停用自动扩缩,则默认副本数为 2。
  8. 点击部署以将索引部署到端点。注意:部署过程大约需要 30 分钟。

在 VPC 项目中创建转发规则

索引部署完成后,索引端点会返回服务连接 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 创建了端点并创建了索引,可以开始运行查询了。

如需查询索引,请参阅查询索引以获取最近的邻居

后续步骤