注: 以下の翻訳の正確性は検証されていません。AIPを利用して英語版の原文から機械的に翻訳されたものです。
以下のドキュメントでは、Amazon SageMaker でホストされているモデルへのカスタム接続に使用する設定とモデルアダプターの例を示します。外部モデル統合の利点を確認し、ユースケースに適していることを確かめてください。
ステップごとのガイドについては、モデルアダプターを作成する方法と外部でホストされているモデルへの接続を作成する方法のドキュメントを参照してください。
まず、Code Repositories アプリケーションのモデルアダプターライブラリを使用して、モデルアダプターを公開し、タグを付けます。以下のモデルアダプターは、AWS SDK for Python (Boto3) ↗ とフレームワークを使用して、Amazon SageMaker でホストされているモデルへの接続を設定します。以下のコードは、バージョン Python 3.8.17、boto3 1.28.1、pandas 1.5.3 でテストされています。
このモデルアダプターは、以下を前提としています。
region_name - 接続設定として指定しますendpoint_name - 接続設定として指定しますaccess_key_id - 資格情報として指定しますsecret_access_key - 資格情報として指定します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 70import palantir_models as pm import models_api.models_api_executable as executable_api import boto3 import json import pandas as pd import logging from typing import Optional from botocore.exceptions import ClientError logger = logging.getLogger(__name__) class SageMakerTabularAdapter(pm.ExternalModelAdapter): """ :display-name: SageMaker Tabular Model Adapter :description: Default model adapter for SageMaker models that expect tabular input and output tabular data. """ def __init__(self, region_name, endpoint_name, access_key_id, secret_access_key): self.endpoint_name = endpoint_name self.runtime = boto3.client( 'runtime.sagemaker', aws_access_key_id=access_key_id, aws_secret_access_key=secret_access_key, region_name=region_name ) @classmethod def init_external(cls, external_context) -> "pm.ExternalModelAdapter": region_name = external_context.connection_config["region_name"] endpoint_name = external_context.connection_config["endpoint_name"] access_key_id = external_context.resolved_credentials["access_key_id"] secret_access_key = external_context.resolved_credentials["secret_access_key"] return cls( region_name, endpoint_name, access_key_id, secret_access_key ) @classmethod def api(cls): inputs = {"df_in": pm.Pandas()} outputs = {"df_out": pm.Pandas()} return inputs, outputs def predict(self, df_in): payload = { "instances": df_in.apply(lambda row: {"features": row.tolist()}, axis=1).tolist() } try: response = self.runtime.invoke_endpoint( EndpointName=self.endpoint_name, ContentType="application/json", Body=json.dumps(payload) ) except ClientError as error: logger.error("SageMaker inference call failed. This can indicate an error with this Model's egress " "policy. Double check your configured egress policy and ensure the remote endpoint is still " "available.") raise error try: # モデルの出力は JSON にシリアル化できることを前提としています # 結果がエグゼキューターで扱うには大きすぎる場合、このデシリアル化によって OOM が発生する可能性があります result = json.loads(response['Body'].read().decode()) except ValueError as error: logger.error("This SageMakerTabularAdapter expects results to be json serializable.") raise error return pd.json_normalize(result)
次に、このモデルアダプターを使用するように外部でホストされているモデルを設定し、モデルアダプターが期待する必要な設定と資格情報を指定します。この例では、モデルが us-east-1 でホストされていることを前提としていますが、これは設定可能です。
上記の SageMakerTabularAdapter では URL が不要なため、空欄のままにします。ただし、設定と資格情報のマップには、モデルアダプターで定義されているものと同じキーを使用して値を指定します。
以下では、runtime.sagemaker.us-east-1.amazonaws.com(ポート443)用に設定されたエグレスポリシーを使用します。

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

Amazon SageMaker 表形式モデルアダプターの例で必要とされる接続設定を定義します。
このアダプターには、以下の接続設定が必要です。

Amazon SageMaker 表形式モデルアダプターの例で必要とされる資格情報の設定を定義します。
このアダプターには、以下の資格情報の設定が必要です。

Amazon SageMaker モデルの設定が完了したので、このモデルをライブデプロイメントまたは Python トランスフォームでホストできます。
以下の画像は、ライブデプロイメントで Amazon SageMaker モデルに対して行ったクエリの例を示しています。
