建立公開端點

如要使用 gcloud CLI 或 Vertex AI API 部署模型,請先建立公開端點

如果您已有公開端點,可以略過這個步驟,直接前往「使用 gcloud CLI 或 Vertex AI API 部署模型」。

本文說明如何建立新的公開端點。

專屬公開端點的預設要求逾時時間為 10 分鐘。 在 Vertex AI API 和 Vertex AI SDK for Python 中,您可以選擇加入含有新 inferenceTimeout 值的 clientConnectionConfig 物件,指定不同的要求逾時時間,如下列範例所示。逾時值上限為 3600 秒 (1 小時)。

Google Cloud 控制台

  1. 在 Google Cloud 控制台的 Vertex AI 專區中,前往「線上預測」頁面。
    前往「線上預測」頁面
  2. 按一下「 Create」(建立)
  3. 在「New endpoint」(新增端點) 窗格中:
    1. 輸入「端點名稱」
    2. 選取「標準」存取權類型。
    3. 勾選「啟用專屬 DNS」核取方塊。
    4. 按一下 [繼續]
  4. 按一下 [完成]

REST

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

  • LOCATION_ID:您的區域。
  • PROJECT_ID:您的專案 ID
  • ENDPOINT_NAME:端點的顯示名稱。
  • INFERENCE_TIMEOUT_SECS:(選用) 選填 inferenceTimeout 欄位中的秒數。

HTTP 方法和網址:

POST https://LOCATION_ID-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION_ID/endpoints

JSON 要求主體:

{
  "display_name": "ENDPOINT_NAME"
  "dedicatedEndpointEnabled": true,
  "clientConnectionConfig": {
    "inferenceTimeout": {
      "seconds": INFERENCE_TIMEOUT_SECS
    }
  }
}

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

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

{
  "name": "projects/PROJECT_NUMBER/locations/LOCATION_ID/endpoints/ENDPOINT_ID/operations/OPERATION_ID",
  "metadata": {
    "@type": "type.googleapis.com/google.cloud.aiplatform.v1.CreateEndpointOperationMetadata",
    "genericMetadata": {
      "createTime": "2020-11-05T17:45:42.812656Z",
      "updateTime": "2020-11-05T17:45:42.812656Z"
    }
  }
}
您可以輪詢作業狀態,直到回應包含 "done": true 為止。

Python

在試用這個範例之前,請先按照Python使用用戶端程式庫的 Vertex AI 快速入門中的操作說明進行設定。 詳情請參閱 Vertex AI Python API 參考說明文件

如要向 Vertex AI 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。

更改下列內容:

  • PROJECT_ID:您的專案 ID。
  • LOCATION_ID:您使用 Vertex AI 的區域。
  • ENDPOINT_NAME:端點的顯示名稱。
  • INFERENCE_TIMEOUT_SECS:(選用) 選用 inference_timeout 值中的秒數。
from google.cloud import aiplatform

PROJECT_ID = "PROJECT_ID"
LOCATION = "LOCATION_ID"
ENDPOINT_NAME = "ENDPOINT_NAME"
INFERENCE_TIMEOUT_SECS = "INFERENCE_TIMEOUT_SECS"

aiplatform.init(
    project=PROJECT_ID,
    location=LOCATION,
    api_endpoint=ENDPOINT_NAME,
)

dedicated_endpoint = aiplatform.Endpoint.create(
    display_name=DISPLAY_NAME,
    dedicated_endpoint_enabled=True,
    sync=True,
    inference_timeout=INFERENCE_TIMEOUT_SECS,
)

推論逾時設定

推論要求的預設逾時時間長度為 600 秒 (10 分鐘)。如果未在建立端點時明確指定推論逾時,系統就會套用這個逾時值。逾時值上限為一小時。

如要在建立端點時設定推論逾時,請使用 inference_timeout 參數,如下列程式碼片段所示:

timeout_endpoint = aiplatform.Endpoint.create(
    display_name="dedicated-endpoint-with-timeout",
    dedicated_endpoint_enabled=True,
    inference_timeout=1800,  # Unit: Seconds
)

端點建立後,可以使用 EndpointService.UpdateEndpointLongRunning 方法修改推論逾時設定。「EndpointService.UpdateEndpoint」方法不支援這項修改。

要求/回應記錄

要求/回應記錄功能會擷取 API 互動。不過,為遵守 BigQuery 限制,記錄檔會排除大小超過 10 MB 的酬載。

如要在建立端點時啟用及設定要求/回應記錄,請使用下列參數,如後續程式碼片段所示:

logging_endpoint = aiplatform.Endpoint.create(
    display_name="dedicated-endpoint-with-logging",
    dedicated_endpoint_enabled=True,
    enable_request_response_logging=True,
    request_response_logging_sampling_rate=1.0,  # Default: 0.0
    request_response_logging_bq_destination_table="bq://test_logging",
    # If not set, a new BigQuery table will be created with the name:
    # bq://{project_id}.logging_{endpoint_display_name}_{endpoint_id}.request_response_logging
)

端點建立後,您可以使用 EndpointService.UpdateEndpointLongRunning 方法修改要求/回應記錄設定。EndpointService.UpdateEndpoint 方法不支援這項修改。

建立共用的公開端點

Google Cloud 控制台

  1. 在 Google Cloud 控制台的 Vertex AI 專區中,前往「線上預測」頁面。
    前往「線上預測」頁面
  2. 按一下「 Create」(建立)
  3. 在「New endpoint」(新增端點) 窗格中:
    1. 輸入「端點名稱」
    2. 選取「標準」存取權類型。
    3. 按一下 [繼續]
  4. 按一下 [完成]

gcloud

下列範例使用 gcloud ai endpoints create 指令

gcloud ai endpoints create \
    --region=LOCATION_ID \
    --display-name=ENDPOINT_NAME

更改下列內容:

  • LOCATION_ID:您使用 Vertex AI 的區域。
  • ENDPOINT_NAME:端點的顯示名稱。

Google Cloud CLI 工具可能需要幾秒鐘才能建立端點。

REST

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

  • LOCATION_ID:您的區域。
  • PROJECT_ID:您的專案 ID
  • ENDPOINT_NAME:端點的顯示名稱。

HTTP 方法和網址:

POST https://LOCATION_ID-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION_ID/endpoints

JSON 要求主體:

{
  "display_name": "ENDPOINT_NAME"
}

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

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

{
  "name": "projects/PROJECT_NUMBER/locations/LOCATION_ID/endpoints/ENDPOINT_ID/operations/OPERATION_ID",
  "metadata": {
    "@type": "type.googleapis.com/google.cloud.aiplatform.v1.CreateEndpointOperationMetadata",
    "genericMetadata": {
      "createTime": "2020-11-05T17:45:42.812656Z",
      "updateTime": "2020-11-05T17:45:42.812656Z"
    }
  }
}
您可以輪詢作業狀態,直到回應包含 "done": true 為止。

Terraform

下列範例使用 google_vertex_ai_endpoint Terraform 資源建立端點。

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

# Endpoint name must be unique for the project
resource "random_id" "endpoint_id" {
  byte_length = 4
}

resource "google_vertex_ai_endpoint" "default" {
  name         = substr(random_id.endpoint_id.dec, 0, 10)
  display_name = "sample-endpoint"
  description  = "A sample Vertex AI endpoint"
  location     = "us-central1"
  labels = {
    label-one = "value-one"
  }
}

Java

在試用這個範例之前,請先按照Java使用用戶端程式庫的 Vertex AI 快速入門中的操作說明進行設定。 詳情請參閱 Vertex AI Java API 參考說明文件

如要向 Vertex AI 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。


import com.google.api.gax.longrunning.OperationFuture;
import com.google.cloud.aiplatform.v1.CreateEndpointOperationMetadata;
import com.google.cloud.aiplatform.v1.Endpoint;
import com.google.cloud.aiplatform.v1.EndpointServiceClient;
import com.google.cloud.aiplatform.v1.EndpointServiceSettings;
import com.google.cloud.aiplatform.v1.LocationName;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class CreateEndpointSample {

  public static void main(String[] args)
      throws IOException, InterruptedException, ExecutionException, TimeoutException {
    // TODO(developer): Replace these variables before running the sample.
    String project = "YOUR_PROJECT_ID";
    String endpointDisplayName = "YOUR_ENDPOINT_DISPLAY_NAME";
    createEndpointSample(project, endpointDisplayName);
  }

  static void createEndpointSample(String project, String endpointDisplayName)
      throws IOException, InterruptedException, ExecutionException, TimeoutException {
    EndpointServiceSettings endpointServiceSettings =
        EndpointServiceSettings.newBuilder()
            .setEndpoint("us-central1-aiplatform.googleapis.com:443")
            .build();

    // Initialize client that will be used to send requests. This client only needs to be created
    // once, and can be reused for multiple requests. After completing all of your requests, call
    // the "close" method on the client to safely clean up any remaining background resources.
    try (EndpointServiceClient endpointServiceClient =
        EndpointServiceClient.create(endpointServiceSettings)) {
      String location = "us-central1";
      LocationName locationName = LocationName.of(project, location);
      Endpoint endpoint = Endpoint.newBuilder().setDisplayName(endpointDisplayName).build();

      OperationFuture<Endpoint, CreateEndpointOperationMetadata> endpointFuture =
          endpointServiceClient.createEndpointAsync(locationName, endpoint);
      System.out.format("Operation name: %s\n", endpointFuture.getInitialFuture().get().getName());
      System.out.println("Waiting for operation to finish...");
      Endpoint endpointResponse = endpointFuture.get(300, TimeUnit.SECONDS);

      System.out.println("Create Endpoint Response");
      System.out.format("Name: %s\n", endpointResponse.getName());
      System.out.format("Display Name: %s\n", endpointResponse.getDisplayName());
      System.out.format("Description: %s\n", endpointResponse.getDescription());
      System.out.format("Labels: %s\n", endpointResponse.getLabelsMap());
      System.out.format("Create Time: %s\n", endpointResponse.getCreateTime());
      System.out.format("Update Time: %s\n", endpointResponse.getUpdateTime());
    }
  }
}

Node.js

在試用這個範例之前,請先按照Node.js使用用戶端程式庫的 Vertex AI 快速入門中的操作說明進行設定。 詳情請參閱 Vertex AI Node.js API 參考說明文件

如要向 Vertex AI 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。

/**
 * TODO(developer): Uncomment these variables before running the sample.\
 * (Not necessary if passing values as arguments)
 */

// const endpointDisplayName = 'YOUR_ENDPOINT_DISPLAY_NAME';
// const project = 'YOUR_PROJECT_ID';
// const location = 'YOUR_PROJECT_LOCATION';

// Imports the Google Cloud Endpoint Service Client library
const {EndpointServiceClient} = require('@google-cloud/aiplatform');

// Specifies the location of the api endpoint
const clientOptions = {
  apiEndpoint: 'us-central1-aiplatform.googleapis.com',
};

// Instantiates a client
const endpointServiceClient = new EndpointServiceClient(clientOptions);

async function createEndpoint() {
  // Configure the parent resource
  const parent = `projects/${project}/locations/${location}`;
  const endpoint = {
    displayName: endpointDisplayName,
  };
  const request = {
    parent,
    endpoint,
  };

  // Get and print out a list of all the endpoints for this resource
  const [response] = await endpointServiceClient.createEndpoint(request);
  console.log(`Long running operation : ${response.name}`);

  // Wait for operation to complete
  await response.promise();
  const result = response.result;

  console.log('Create endpoint response');
  console.log(`\tName : ${result.name}`);
  console.log(`\tDisplay name : ${result.displayName}`);
  console.log(`\tDescription : ${result.description}`);
  console.log(`\tLabels : ${JSON.stringify(result.labels)}`);
  console.log(`\tCreate time : ${JSON.stringify(result.createTime)}`);
  console.log(`\tUpdate time : ${JSON.stringify(result.updateTime)}`);
}
createEndpoint();

Python

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

def create_endpoint_sample(
    project: str,
    display_name: str,
    location: str,
):
    aiplatform.init(project=project, location=location)

    endpoint = aiplatform.Endpoint.create(
        display_name=display_name,
        project=project,
        location=location,
    )

    print(endpoint.display_name)
    print(endpoint.resource_name)
    return endpoint

後續步驟