預測類別

Vertex AI SDK 包含下列預測類別。其中一個類別用於批次預測。其他則與線上預測或向量搜尋預測有關。詳情請參閱「Gemini Enterprise Agent Platform 預測結果總覽」。

批次預測類別

批次預測是一組非同步預測要求。您可以要求透過模型資源進行批次預測,不必將模型部署至端點。如果您不需要立即取得回覆,並想透過單一要求處理資料,就適合使用批次預測。BatchPredictionJob 是 Vertex AI SDK 中專用於批次預測的類別。

BatchPredictionJob

BatchPredictionJob 類別代表一組非同步預測要求。建立批次預測工作的方法有兩種:

  1. 如要建立批次預測工作,建議使用已訓練 Modelbatch_predict 方法。這個方法需要下列參數:

    • instances_format:批次預測要求檔案的格式:jsonlcsvbigquerytf-recordtf-record-gzipfile-list
    • prediction_format:批次預測回應檔案的格式:jsonlcsvbigquerytf-recordtf-record-gzipfile-list
    • gcs_source: 一或多個 Cloud Storage 路徑的清單,指向批次預測要求。
    • gcs_destination_prefix:Gemini Enterprise Agent Platform 寫入預測結果的 Cloud Storage 路徑。

    以下程式碼範例說明如何呼叫 Model.batch_predict

    batch_prediction_job = model.batch_predict(
        instances_format="jsonl",
        predictions_format="jsonl",
        job_display_name="your_job_display_name_string",
        gcs_source=['gs://path/to/my/dataset.csv'],
        gcs_destination_prefix='gs://path/to/my/destination',
        model_parameters=None,
        starting_replica_count=1,
        max_replica_count=5,
        machine_type="n1-standard-4",
        sync=True
    )
    
  2. 建立批次預測工作的第二種方式是呼叫 BatchPredictionJob.create 方法。BatchPredictionJob.create 方法需要四個參數:

    • job_display_name:您指派給批次預測工作的名稱。 請注意,job_display_nameBatchPredictionJob.create 的必要屬性,但為 Model.batch_predict 的選用屬性。
    • model_name:用於批次預測工作的已訓練Model完整名稱或 ID。
    • instances_format:批次預測要求檔案的格式:jsonlcsvbigquerytf-recordtf-record-gzipfile-list
    • predictions_format:批次預測回應檔案的格式:jsonlcsvbigquerytf-recordtf-record-gzipfile-list

線上預測類別

「線上預測」是對模型端點發出的同步要求。您必須先將模型部署至端點,才能提出線上預測要求。如要根據應用程式輸入內容生成預測結果,或是需要快速取得預測結果,您可以選用「線上預測」模式。

Endpoint

必須先將模型部署至端點,才能透過模型執行線上預測。將模型部署至端點時,系統會將實體機器資源與模型建立關聯,以便提供線上預測結果。

您可以將多個模型部署至同一個端點,您也可以將一個模型部署至多個端點。詳情請參閱模型部署考量事項

如要建立 Endpoint 資源,請部署模型。呼叫 Model.deploy 方法時,系統會建立並傳回 Endpoint

以下是程式碼範例,說明如何建立自訂訓練作業、建立及訓練模型,然後將模型部署至端點。

# Create your custom training job

job = aiplatform.CustomTrainingJob(
    display_name="my_custom_training_job",
    script_path="task.py",
    container_uri="us-docker.pkg.dev/vertex-ai/training/tf-cpu.2-8:latest",
    requirements=["google-cloud-bigquery>=2.20.0", "db-dtypes"],
    model_serving_container_image_uri="us-docker.pkg.dev/vertex-ai/prediction/tf2-cpu.2-8:latest"
)

# Start the training and create your model
model = job.run(
    dataset=dataset,
    model_display_name="my_model_name",
    bigquery_destination=f"bq://{project_id}"
)

# Create an endpoint and deploy your model to that endpoint
endpoint = model.deploy(deployed_model_display_name="my_deployed_model")

# Get predictions using test data in a DataFrame named 'df_my_test_data'
predictions = endpoint.predict(instances=df_my_test_data)

PrivateEndpoint

私人端點類似於 Endpoint 資源,但預測結果會透過安全網路傳送至 Gemini Enterprise Agent Platform 線上預測服務。如果貴機構想確保所有流量都是私密流量,請使用私人端點。

如要使用私人端點,必須將 Gemini Enterprise Agent Platform 設定為與虛擬私有雲 (VPC) 對等互連。私人預測端點必須使用虛擬私有雲,才能直接連線至 Gemini Enterprise Agent Platform。詳情請參閱「設定虛擬私有雲網路對等互連」和「使用私人端點進行線上預測」。

ModelDeploymentMonitoringJob

使用 ModelDeploymentMonitoringJob 資源監控模型,並在模型出現可能影響預測品質的偏差時收到快訊。

如果輸入資料與模型訓練資料不同,就算模型本身沒有變動,效能也可能會降低。模型監控功能會分析輸入資料,偵測特徵偏差偏移情形:

  • 當正式環境的特徵資料分布與模型訓練所用的特徵資料不同時,就會發生Skew
  • 當正式環境的特徵資料隨時間大幅變動時,就會發生偏移

詳情請參閱「Gemini Enterprise 簡介:Agent Platform 模型監控」。如需如何使用 Vertex AI SDK 實作 Gemini Enterprise Agent Platform 監控的範例,請參閱 GitHub 上的 Gemini Enterprise Agent Platform model monitoring with 可解釋 AI feature attributions 筆記本。

Vector Search 預測類別

Vector Search 是一項代管服務,可建構相似度索引或向量,執行相似度比對。執行相似度比對時,大致可分為兩個步驟:

  1. 建立資料的向量表示法。資料可以是文字、圖片、影片、音訊或表格型資料。

  2. 向量搜尋會使用您建立的向量端點,以低延遲時間執行大規模搜尋,找出相似向量。

詳情請參閱 Vector Search 總覽,以及 GitHub 上的建立 Vector Search 搜尋索引筆記本。

MatchingEngineIndex

MatchingEngineIndex 類別代表您建立的索引或向量,Vector Search 會使用這些索引或向量執行相似度搜尋。

您可以使用下列兩種搜尋演算法建立索引:

  1. TreeAhConfig 使用淺層樹狀結構 AH 演算法 (使用不對稱雜湊的淺層樹狀結構)。使用 MatchingEngineIndex.create_tree_ah_index 建立使用 tree-AH 演算法的索引。
  2. BruteForceConfig 使用標準線性搜尋) 使用 MatchingEngineIndex.create_brute_force_index 建立使用標準線性搜尋的索引。

如要進一步瞭解如何設定索引,請參閱「設定索引」。

以下程式碼範例說明如何建立使用樹狀結構 AH 演算法的索引:

my_tree_ah_index = aiplatform.Index.create_tree_ah_index(
    display_name="my_display_name",
    contents_delta_uri="gs://my_bucket/embeddings",
    dimensions=1,
    approximate_neighbors_count=150,
    distance_measure_type="SQUARED_L2_DISTANCE",
    leaf_node_embedding_count=100,
    leaf_nodes_to_search_percent=50,
    description="my description",
    labels={ "label_name": "label_value" }
)

以下程式碼範例說明如何建立使用暴力搜尋演算法的索引:

my_brute_force_index = aiplatform.Index.create_brute_force_index(
    display_name="my_display_name",
    contents_delta_uri="gs://my_bucket/embeddings",
    dimensions=1,
    approximate_neighbors_count=150,
    distance_measure_type="SQUARED_L2_DISTANCE",
    description="my description",
    labels={ "label_name": "label_value" }
)

MatchingEngineIndexEndpoint

使用 MatchingEngineIndexEndpoint 類別建立及擷取端點。將模型部署至端點後,您會取得用於執行查詢的 IP 位址。

以下程式碼範例說明如何建立比對引擎索引端點,然後將比對引擎索引部署至該端點:

my_index_endpoint = aiplatform.MatchingEngineIndexEndpoint.create(
    display_name="sample_index_endpoint",
    description="index endpoint description",
    network="projects/123456789123/global/networks/my_vpc"
)

my_index_endpoint = my_index_endpoint.deploy_index(
    index=my_tree_ah_index, deployed_index_id="my_matching_engine_index_id"
)

後續步驟