注: 以下の翻訳の正確性は検証されていません。AIPを利用して英語版の原文から機械的に翻訳されたものです。
このページでは、Databricks でホストされているモデルへのカスタム接続用の設定とモデルアダプターの例を紹介します。外部モデルがユースケースに適しているかを判断するには、外部モデル統合のメリットを確認してください。
ステップごとのガイドについては、モデルアダプターを作成する方法と外部ホストモデルへの接続を作成する方法のドキュメントを参照してください。
この例では、次の操作を行います。
外部モデルに接続する最初のステップは、Code Repositories のモデルアダプターライブラリを使用してモデルアダプターを公開し、タグを付けることです。以下のモデルアダプターの例では、Databricks Model Serving ↗ にデプロイされた Unity Catalog モデルへの接続を設定します。以下のコードは、バージョン Python 3.11.11、databricks-sdk 0.55.0、pandas 2.2.3 でテスト済みです。この例では、Databricks 機械学習チュートリアル ↗ のワイン品質予測問題を使用します。
このモデルアダプターは、次の前提に基づいています。
host:接続設定で指定します(Databricks インスタンスのホスト URL)。serving_endpoint_name:接続設定で指定します。token:資格情報として指定します。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 55import logging import palantir_models as pm import pandas as pd from databricks.sdk import WorkspaceClient from databricks.sdk.service.serving import QueryEndpointResponse logger = logging.getLogger(__name__) class DatabricksExternalModelAdapter(pm.ExternalModelAdapter): def __init__(self, host, serving_endpoint_name, creds): self.serving_endpoint_name = serving_endpoint_name self.client = WorkspaceClient(host=host, token=creds["token"]) @classmethod def init_external(cls, external_context: pm.ExternalContext) -> "pm.ExternalModelAdapter": return cls( host=external_context.connection_config["host"], serving_endpoint_name=external_context.connection_config["serving_endpoint_name"], creds=external_context.resolved_credentials, ) @classmethod def api(cls): input_cols = [ "fixed_acidity", "volatile_acidity", "citric_acid", "residual_sugar", "chlorides", "free_sulfur_dioxide", "total_sulfur_dioxide", "density", "pH", "sulphates", "alcohol", "is_red", ] inputs = {"df_in": pm.Pandas(columns=[(col, str) for col in input_cols])} outputs = {"df_out": pm.Pandas(columns=[("predictions", bool)])} return inputs, outputs def predict(self, df_in): try: response: QueryEndpointResponse = self.client.serving_endpoints.query( name=self.serving_endpoint_name, dataframe_records=df_in.to_dict(orient="records") ) except Exception as err: logger.error(f"An error occurred while querying the model serving endpoint: {err}") raise err else: return pd.DataFrame(response.as_dict()).drop("served-model-name", axis=1)
モデルアダプターを公開してタグを付けた後、モデルアダプターが期待する必須の設定と資格情報を指定するために、このモデルアダプターを使用するように外部ホストモデルを設定する必要があります。
上記の DatabricksExternalModelAdapter には URL が必要であり、Databricks モデルサービングエンドポイントの完全な URL を設定する必要があります。
以下では、Databricks ワークスペースのドメイン(例:<workspace-name>.cloud.databricks.com またはカスタムドメイン)に対してポート443で設定されたエグレスポリシーを使用します。

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

Databricks アダプターが期待する host と serving_endpoint_name を設定して、ベース URL を Databricks サービングエンドポイントの完全な URL に設定します。

Databricks 表形式モデルアダプターの例で必要とされる資格情報の設定を定義します。
このアダプターには、次の資格情報の設定が必要です。
token:モデルサービングエンドポイントにアクセスする権限を持つ Databricks 個人用アクセストークン。
Databricks モデルの設定が完了したので、このモデルをライブデプロイメントまたは Python トランスフォームでホストできます。
以下の画像は、ライブデプロイメントの Databricks モデルに対して実行されたクエリの例を示しています。

以下のコード例に示すように、トランスフォームでアダプターを使用することもできます。
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 49import pandas as pd from transforms.api import Output, transform, lightweight from palantir_models.transforms import ModelInput from transforms.external.systems import EgressPolicy, use_external_systems @lightweight @use_external_systems( egress=EgressPolicy('<databricks_egress_policy>') ) @transform( out=Output("<output_dataset_path>"), model=ModelInput("<external_model_path>"), ) def compute(egress, out, model): df_in = pd.DataFrame([ { "fixed_acidity": "7.4", "volatile_acidity": "0.7", "citric_acid": "0.0", "residual_sugar": "1.9", "chlorides": "0.076", "free_sulfur_dioxide": "11", "total_sulfur_dioxide": "34", "density": "0.9978", "pH": "3.51", "sulphates": "0.56", "alcohol": "9.4", "is_red": "1.0" }, { "fixed_acidity": "7.4", "volatile_acidity": "0.7", "citric_acid": "0.0", "residual_sugar": "1.9", "chlorides": "0.076", "free_sulfur_dioxide": "11", "total_sulfur_dioxide": "34", "density": "0.9978", "pH": "3.51", "sulphates": "0.56", "alcohol": "9.4", "is_red": "1.0" } ] ) out.write_pandas( model.predict(df_in) )