カスタムモデルのクエリサービスエンドポイント
プレビュー
Mosaic AI Model Serving は パブリック プレビュー 段階にあり、 us-east1
と us-central1
でサポートされています。
この記事では、サーブされたモデルのスコアリング要求を書式設定する方法と、それらの要求をモデルサービングエンドポイントに送信する方法について説明します。 このガイダンスは、Databricks が従来の ML モデルまたは MLflow 形式でパッケージ化されたカスタマイズされた Python モデルとして定義する カスタム モデル の提供に関連しています。 モデルは Unity Catalogに登録されている必要があります。 例としては、scikit-learn、XGBoost、PyTorch、Hugging Face トランスフォーマーモデルなどがあります。 この機能とサポートされているモデル カテゴリの詳細については、「Mosaic AI Model Servingを使用したモデルのデプロイ」を参照してください。
生成AI ワークロードと LLM ワークロードのクエリ要求については、「 Query 基盤モデル」を参照してください。
必要条件
- A モデルサービング エンドポイント.
- MLflow Deployment SDK には、MLflow 2.9 以降が必要です。
- 受け入れられた形式での採点リクエスト。
- REST API または MLflow デプロイ SDK を使用してスコアリング要求を送信するには、Databricks API トークンが必要です。
クエリの方法と例
Mosaic AI Model Serving は、提供されたモデルにスコアリングリクエストを送信するための次のオプションを提供します。
メソッド | 詳細 |
---|---|
サービングUI | Databricks ワークスペースの [サービス エンドポイント ] ページから [ クエリ エンドポイント ] を選択します。JSON形式のモデル入力データを挿入し、[ リクエストを送信 ]をクリックします。 モデルに入力例がログに記録されている場合は、[ 例を表示 ] を使用して読み込みます。 |
REST API | REST API を使用してモデルを呼び出し、クエリを実行します。 詳細は POST /serving-endpoints/{name}/invocations を参照してください。 複数のモデルを提供するエンドポイントへの要求のスコアリングについては、「 エンドポイントの背後にある個々のモデルのクエリ」を参照してください。 |
MLflow デプロイ SDK | MLflow Deployments SDK の predict() 関数を使用して、モデルに対してクエリを実行します。 |
Pandas データフレーム のスコアリングの例
次の例では、 https://<databricks-instance>/model/iris-classifier/Production/invocations
のようなMODEL_VERSION_URI
(<databricks-instance>
は Databricks インスタンスの名前、Databricks REST API トークンは DATABRICKS_API_TOKEN
) を想定しています。
サポートされているスコアリング形式を参照してください。
- REST API
- MLflow Deployments SDK
- SQL
- PowerBI
Score a model accepting dataframe split input format.
curl -X POST -u token:$DATABRICKS_API_TOKEN $MODEL_VERSION_URI \
-H 'Content-Type: application/json' \
-d '{"dataframe_split": [{
"columns": ["sepal length (cm)", "sepal width (cm)", "petal length (cm)", "petal width (cm)"],
"data": [[5.1, 3.5, 1.4, 0.2], [4.9, 3.0, 1.4, 0.2]]
}]
}'
Score a model accepting tensor inputs. Tensor inputs should be formatted as described in TensorFlow Serving’s API documentation.
curl -X POST -u token:$DATABRICKS_API_TOKEN $MODEL_VERSION_URI \
-H 'Content-Type: application/json' \
-d '{"inputs": [[5.1, 3.5, 1.4, 0.2]]}'
The following example uses the predict()
API from the MLflow Deployments SDK.
import mlflow.deployments
export DATABRICKS_HOST="https://<workspace_host>.databricks.com"
export DATABRICKS_TOKEN="dapi-your-databricks-token"
client = mlflow.deployments.get_deploy_client("databricks")
response = client.predict(
endpoint="test-model-endpoint",
inputs={"dataframe_split": {
"index": [0, 1],
"columns": ["sepal length (cm)", "sepal width (cm)", "petal length (cm)", "petal width (cm)"],
"data": [[5.1, 3.5, 1.4, 0.2], [4.9, 3.0, 1.4, 0.2]]
}
}
)
The following example uses the built-in SQL function, ai_query. This function is Public Preview and the definition might change.
The following example queries the model behind the sentiment-analysis
endpoint with the text
dataset and specifies the return type of the request.
SELECT text, ai_query(
"sentiment-analysis",
text,
returnType => "STRUCT<label:STRING, score:DOUBLE>"
) AS predict
FROM
catalog.schema.customer_reviews
You can score a dataset in Power BI Desktop using the following steps:
-
Open dataset you want to score.
-
Go to Transform Data.
-
Right-click in the left panel and select Create New Query.
-
Go to View > Advanced Editor.
-
Replace the query body with the code snippet below, after filling in an appropriate
DATABRICKS_API_TOKEN
andMODEL_VERSION_URI
.(dataset as table ) as table =>
let
call_predict = (dataset as table ) as list =>
let
apiToken = DATABRICKS_API_TOKEN,
modelUri = MODEL_VERSION_URI,
responseList = Json.Document(Web.Contents(modelUri,
[
Headers = [
#"Content-Type" = "application/json",
#"Authorization" = Text.Format("Bearer #{0}", {apiToken})
],
Content = {"dataframe_records": Json.FromValue(dataset)}
]
))
in
responseList,
predictionList = List.Combine(List.Transform(Table.Split(dataset, 256), (x) => call_predict(x))),
predictionsTable = Table.FromList(predictionList, (x) => {x}, {"Prediction"}),
datasetWithPrediction = Table.Join(
Table.AddIndexColumn(predictionsTable, "index"), "index",
Table.AddIndexColumn(dataset, "index"), "index")
in
datasetWithPrediction -
Name the query with your desired model name.
-
Open the advanced query editor for your dataset and apply the model function.
テンソル入力の例
次の例では、テンソル入力を受け入れるモデルをスコアリングします。 Tensor 入力は、 TensorFlow Serving の API ドキュメントで説明されているようにフォーマットする必要があります。 この例では、https://<databricks-instance>/model/iris-classifier/Production/invocations
のようなMODEL_VERSION_URI
(<databricks-instance>
は Databricks インスタンスの名前、Databricks REST API トークンは DATABRICKS_API_TOKEN
) を想定しています。
curl -X POST -u token:$DATABRICKS_API_TOKEN $MODEL_VERSION_URI \
-H 'Content-Type: application/json' \
-d '{"inputs": [[5.1, 3.5, 1.4, 0.2]]}'
サポートされているスコアリング形式
カスタムモデルの場合、モデルサービングは Pandas データフレーム またはTensor入力でのスコアリングリクエストをサポートします。
Pandas データフレーム
リクエストを送信するには、サポートされているキーの 1 つと、入力形式に対応する JSON オブジェクトを使用して、JSON でシリアル化された Pandas データフレーム を構築する必要があります。
-
(推奨)
dataframe_split
形式は、split
方向の JSON シリアル化された Pandas データフレーム です。JSON{
"dataframe_split": {
"index": [0, 1],
"columns": ["sepal length (cm)", "sepal width (cm)", "petal length (cm)", "petal width (cm)"],
"data": [
[5.1, 3.5, 1.4, 0.2],
[4.9, 3.0, 1.4, 0.2]
]
}
} -
dataframe_records
は、records
方向の JSON シリアル化された Pandas データフレーム です。
この形式は、列の順序の保持を保証するものではなく、records
形式よりもsplit
形式が優先されます。
{
"dataframe_records": [
{
"sepal length (cm)": 5.1,
"sepal width (cm)": 3.5,
"petal length (cm)": 1.4,
"petal width (cm)": 0.2
},
{
"sepal length (cm)": 4.9,
"sepal width (cm)": 3,
"petal length (cm)": 1.4,
"petal width (cm)": 0.2
},
{
"sepal length (cm)": 4.7,
"sepal width (cm)": 3.2,
"petal length (cm)": 1.3,
"petal width (cm)": 0.2
}
]
}
エンドポイントからの応答には、モデルからの出力が含まれ、JSON でシリアル化され、 predictions
キーでラップされます。
{
"predictions": [0, 1, 1, 1, 0]
}
テンソル入力
モデルがテンソルを想定する場合 ( TensorFlow モデルや PyTorch モデルなど)、リクエストの送信には instances
と inputs
の 2 つの形式オプションがサポートされています。
行ごとに複数の名前付きテンソルがある場合は、各行に各テンソルを1つずつ持つ必要があります。
-
instances
は、行形式のテンソルを受け入れるテンソルベースの形式です。 すべての入力テンソルの 0 番目の次元が同じ場合は、この形式を使用します。 概念的には、インスタンスリストの各テンソルをリストの残りの部分にある同じ名前の他のテンソルと結合して、モデルの完全な入力テンソルを構築できますが、これはすべてのテンソルが同じ0次元を持つ場合にのみ可能です。JSON{ "instances": [1, 2, 3] }
次の例は、複数の名前付きテンソルを指定する方法を示しています。
JSON{
"instances": [
{
"t1": "a",
"t2": [1, 2, 3, 4, 5],
"t3": [
[1, 2],
[3, 4],
[5, 6]
]
},
{
"t1": "b",
"t2": [6, 7, 8, 9, 10],
"t3": [
[7, 8],
[9, 10],
[11, 12]
]
}
]
} -
inputs
テンソルを含むクエリを列形式で送信します。 このリクエストは、実際にはt2
(3) のテンソルインスタンスの数がt1
やt3
とは異なるため、この入力をinstances
形式で表すことはできないため、異なります。JSON{
"inputs": {
"t1": ["a", "b"],
"t2": [
[1, 2, 3, 4, 5],
[6, 7, 8, 9, 10]
],
"t3": [
[
[1, 2],
[3, 4],
[5, 6]
],
[
[7, 8],
[9, 10],
[11, 12]
]
]
}
}
エンドポイントからの応答は、次の形式です。
{
"predictions": [0, 1, 1, 1, 0]
}
ノートブックの例
Python モデルを使用してモデルサービングエンドポイントをテストする方法の例については、次のノートブックを参照してください。