モデルの接続と開発モデル外部でホストされているモデル例:Vertex AI モデルの統合

注: 以下の翻訳の正確性は検証されていません。AIPを利用して英語版の原文から機械的に翻訳されたものです。

例:Vertex AI モデルを統合する

以下のドキュメントでは、Google の Vertex AI でホストされているモデルへのカスタム接続に使用する設定とモデルアダプターの例を示します。外部モデル統合のメリットを確認し、この方法がユースケースに適していることを確認してください。

ステップごとのガイドについては、モデルアダプターを作成する方法と外部でホストされているモデルへの接続を作成する方法のドキュメントを参照してください。

Vertex AI の表形式モデルアダプターの例

まず、Code Repositories アプリケーションのモデルアダプターライブラリを使用して、モデルアダプターを公開し、タグを付けます。以下のモデルアダプターは、Vertex AI SDK for Python ↗ とフレームワークを使用して、Vertex AI でホストされているモデルへの接続を設定します。以下のコードは、バージョン Python 3.8.17、pandas 1.5.3、 google-cloud-aiplatform 1.32.0、google-auth 2.23.0、google-auth-oauthlib 1.1.0 でテストされています。

このモデルアダプターは、以下を前提としています。

  • このモデルアダプターは、モデルに渡されるデータが表形式であることを前提としています。
  • このモデルアダプターは、入力データを JSON にシリアライズし、ホストされている Vertex AI モデルに送信します。
  • このモデルアダプターは、レスポンスを JSON から pandas データフレームにデシリアライズできることを前提としています。
  • このモデルアダプターは、Vertex AI クライアントを構築するために4個の入力を受け取ります。
    • region_name - 接続設定として指定
    • project_id - 接続設定として指定
    • endpoint_id - 接続設定として指定
    • google_application_credentials - 資格情報として指定
Copied!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 import palantir_models as pm import models_api.models_api_executable as executable_api from google.oauth2 import service_account from google.cloud import aiplatform from google.api_core import exceptions import json import pandas as pd import logging from typing import Optional logger = logging.getLogger(__name__) class VertexAITabularAdapter(pm.ExternalModelAdapter): """ :display-name: Vertex AI Tabular Model Adapter :description: Default model adapter for Vertex AI models that expect tabular input and output data. """ def __init__(self, project_id, region_name, endpoint_id, google_application_credentials): self.endpoint_id = endpoint_id # google_application_credentials は、Google が提供するシークレットの # key_file を表す有効な文字列であることが期待されます credentials = service_account.Credentials.from_service_account_info( json.loads(google_application_credentials), scopes=["https://www.googleapis.com/auth/cloud-platform"] ) aiplatform.init(project=project_id, location=region_name, credentials=credentials) @classmethod def init_external(cls, external_context) -> "pm.ExternalModelAdapter": project_id = external_context.connection_config["project_id"] region_name = external_context.connection_config["region_name"] endpoint_id = external_context.connection_config["endpoint_id"] google_application_credentials = external_context.resolved_credentials["google_application_credentials"] return cls( project_id, region_name, endpoint_id, google_application_credentials ) @classmethod def api(cls): inputs = {"df_in": pm.Pandas()} outputs = {"df_out": pm.Pandas()} return inputs, outputs def predict(self, df_in): instances = df_in.to_dict(orient='records') try: endpoint = aiplatform.Endpoint(endpoint_name=self.endpoint_id) except ValueError as error: logger.error("Error initializing endpoint object double check the inputted endpoint_id, project_id and " "region_name.") raise error try: # モデルの出力は json にシリアライズできると想定しています # 結果がエグゼキューターに対して大きすぎる場合、OOM が発生する可能性があります prediction_result = endpoint.predict(instances=instances) except exceptions.Forbidden as error: logger.error("Error performing inference provided google_application_credentials do not have sufficient " "permissions.") raise error except exceptions.BadRequest as error: logger.error("Error performing inference double check your input dataframe.") raise error return pd.json_normalize(prediction_result.predictions)

Vertex AI の表形式モデルの設定

次に、このモデルアダプターを使用するように外部でホストされているモデルを設定し、モデルアダプターが期待する必要な設定と資格情報を指定します。この例では、モデルが us-central1 でホストされていることを前提としていますが、この値は変更できます。

上記の VertexAITabularAdapter は URL を必要としないため、空欄のままにします。ただし、設定と資格情報のマップには、モデルアダプターで定義されているものと同じキーを使用して値を指定します。

エグレスポリシーを選択する

以下では、us-central1-aiplatform.googleapis.com(ポート443)用に設定されたエグレスポリシーを使用します。

モデリング目標アプリケーションの Vertex AI 用エグレスポリシー

モデルアダプターを設定する

外部ホストモデルを接続ダイアログで、公開済みのモデルアダプターを選択します。

Palantir Foundry の Vertex AI 用モデルアダプター設定パネル

接続設定を行う

Vertex AI の表形式モデルアダプターの例で必要とされる接続設定を定義します。

このアダプターには、以下の接続設定が必要です。

  • region_name - モデルがホストされている Google のリージョン名。
  • project_id - この外部でホストされているモデルが属するプロジェクトの一意の識別子。
  • endpoint_id - 外部でホストされているモデルの一意の識別子。

Palantir Foundry の Vertex AI 用接続設定パネル

資格情報の設定を行う

Vertex AI の表形式モデルアダプターの例で必要とされる資格情報の設定を定義します。

このアダプターには、以下の資格情報の設定が必要です。

  • google_application_credentials - サービスアカウントの資格情報の取得に使用できる、サービスアカウントの秘密鍵ファイルの内容全体です。Google Cloud Console の資格情報ページ ↗ を使用して秘密鍵を作成できます。

Palantir Foundry の Vertex AI 用資格情報設定パネル

Vertex AI の表形式モデルの使用

Vertex AI モデルの設定が完了したので、このモデルをライブデプロイメントまたは Python トランスフォームでホストできます。

以下の画像は、ライブデプロイメントで Vertex AI モデルに対して実行したクエリの例を示しています。

VertexAITabularAdapter を使用したクエリの例