Skip to content

外部モデルのカスタムモデルプロキシを作成

外部モデルのプロキシとしてカスタムモデルを作成するには、新しいプロキシモデルをモデルワークショップに追加します。 A proxy model contains proxy code created to connect with an external model, allowing you to use features like compliance documentation, challenger analysis, and custom model tests with a model running on infrastructure outside DataRobot.

プロキシモデルを作成するには、次の操作を行います。

  1. (オプション)モデルメタデータ(model-metadata.yaml)を介してカスタムモデルに ランタイムパラメーターを追加します。

  2. カスタムモデルファイル(custom.py)を使用して、カスタムモデルにプロキシコードを追加します。

  3. [Create a proxy model]{0} and [assemble the model]{1} files in the Model workshop.

プロキシコードを追加

外部モデル用のプロキシとして作成するカスタムモデルでは、custom.pyファイルにプロキシモデルと外部でホストされたモデルを接続するためのカスタムコードを含める必要があります。このコードがプロキシコードです。 See the custom model assembly documentation for more information on writing custom model code.

custom.pyファイル内のプロキシコードは、次のことを行う必要があります。

  • 必要なモジュールと、オプションで ランタイムパラメーターmodel-metadata.yamlからインポートします。

  • HTTPS接続または外部モデルで必要とされるネットワークプロトコルを介して、カスタムモデルを外部モデルに接続します。

  • 予測をリクエストし、必要に応じて予測データを変換します。

プロキシコードの再利用を簡素化するために、model-metadata.yamlファイルのモデルメタデータによって ランタイムパラメーターを追加できます。

model-metadata.yaml
name: runtime-parameter-example
type: inference
targetType: regression
runtimeParameterDefinitions:
- fieldName: endpoint
  type: string
  description: The name of the endpoint.
- fieldName: API_KEY
  type: credential
  description: The HTTP basic credential containing the endpoint's API key in the password field (the username field is ignored). 

モデルメタデータでランタイムパラメーターを定義した場合、それらをcustom.pyファイルにインポートしてプロキシコードで使用できます。 これらのパラメーターをインポートした後、それらをプロキシコードの変数に割り当てることができます。 これにより、外部モデルに接続して予測データを取得する予測リクエストを作成できます。 次の例では、custom.pyファイルの基本構造の概要を説明します。

custom.py
# Import modules required to make a prediction request.
import json
import ssl
import urllib.request
import pandas as pd
# Import SimpleNamespace to create an object to store runtime parameter variables.
from types import SimpleNamespace
# Import RuntimeParameters to use the runtime parameters set in the model metadata.
from datarobot_drum import RuntimeParameters

# Override the default load_model hook to read the runtime parameters.
def load_model(code_dir):
    # Assign runtime parameters to variables.
    api_key = RuntimeParameters.get("API_KEY")["password"]
    endpoint = RuntimeParameters.get("endpoint")

    # Create scoring endpoint URL.
    url = f"https://{endpoint}.example.com/score"

    # Return an object containing the variables necessary to make a prediction request.
    return SimpleNamespace(**locals())

# Write proxy code to request and convert scoring data from the external model.
def score(data, model, **kwargs):
    # Call make_remote_prediction_request.
    # Convert prediction data as necessary.

def make_remote_prediction_request(payload, url, api_key):
    # Connect to the scoring endpoint URL.
    # Request predictions from the external model. 

プロキシモデルの作成

外部モデルのプロキシとしてカスタムモデルを作成するには、新しいプロキシモデルを[モデルワークショップ]{0}に追加します。 A proxy model contains the proxy code you created to connect with your external model.

モデルワークショップを介してプロキシモデルを追加するには:

  1. レジストリ > モデルワークショップをクリックします。

  2. モデルワークショップタブで、+モデルを追加をクリックします。

  3. In the Add a model panel, select Proxy, and then add the model information:

    フィールド 説明
    モデル名 カスタムモデルのわかりやすい名前。
    ターゲットタイプ
    ターゲット名
    オプション設定
    予測しきい値 For binary classification models, a decimal value ranging from 0 to 1. Any prediction score exceeding this value is assigned to the positive class.
    言語 モデルの構築に使用されるプログラミング言語。
    説明 モデルのコンテンツと目的の説明。
  4. フィールドに入力したら、モデルを追加をクリックします。

Assemble a proxy model

To assemble the proxy model, bring the proxy code you created to connect with your external model into the Model workshop and ensure the proxy model has network access.

  1. On the Assemble tab, navigate to the Environment section and select a model environment from the Base environment dropdown menu.

    モデル環境

    モデル環境は、カスタムモデルのテストとデプロイに使用されます。 The Base Environment dropdown list includes drop-in model environments and any custom environments that you can create. デフォルトでは、カスタムモデルは、正常に構築され、選択された環境の最新バージョンを使用します。

  2. 依存関係セクションに入力するために、ファイルセクションでrequirements.txtファイルをアップロードし、DataRobotが最適なイメージを構築できるようにします。

  3. ファイルセクションで、必要なプロキシモデルファイルを追加します。 If you aren't pairing the model with a drop-in environment, this includes the custom model environment requirements and a start_server.sh file. ファイルの追加方法はいくつかあります。

      要素 説明
    1 ファイル ファイルをグループボックスにドラッグしてアップロードします。
    2 ソースから選択
    3 アップロード Click to browse for Local Files or a Local Folder or to pull files from a remote repository.
    4 作成

    ファイルに関する注意事項

    既存のファイルと同じ名前の新しいファイルを追加する場合、保存をクリックすると、ファイルセクションで古いファイルが置き換えられます。 ローカルフォルダーからファイルを追加する場合は、モデルファイルがすでにカスタムモデルのルートにあることを確認してください。 アップロードされたフォルダーは、モデル自体ではなく、モデルに必要な依存ファイルおよび追加のアセット用です。 モデルファイルがフォルダーに含まれている場合でも、ファイルがルートレベルに存在しない限り、DataRobotからモデルファイルにアクセスすることはできません。 ルートにあるファイルは、フォルダー内の依存関係を指定することができます。

  4. アセンブルタブの設定セクションで、リソースの横にある(編集をクリックし、ネットワークアクセス公開に設定します。

  5. モデルメタデータでランタイムパラメーターを提供した場合、環境を構築し、新しいバージョンを作成すると、ランタイムパラメーターセクションでそれらのパラメーターを設定できます。

  6. 最後に、カスタムモデルを登録して、デプロイ可能なプロキシモデルを作成できます。