本文的指引能幫助你在等待端點部署流程前,先發現模型的問題。 Databricks 建議透過這些驗證步驟,以確保使用模型服務時有更好的開發體驗。
在部署之前測試預測
將模型部署到服務端點之前,請先使用 mlflow.models.predict 和輸入範例,以虛擬環境測試離機預測。 MLflow 提供驗證 API,可模擬部署環境,並允許測試修改的相依性。
有兩個部署前驗證選項: MLflow Python API 和 MLflow CLI。 如需更詳細的指引,請參閱 MLflow 文件,裡面有測試預測的詳細說明。
您可以指定下列參數:
- 部署至模型服務之模型的
model_uri。 - 下列其中一項:
- 模型的
input_data呼叫之預期格式的mlflow.pyfunc.PyFuncModel.predict()。 -
input_path,它可定義檔案,其中包含將載入並用於呼叫predict的輸入資料。
- 模型的
- 格式為
content_type或csv的json。 - 可選用的
output_path,用於將預測寫入檔案。 如果忽略此參數,預測會列印至stdout。 - 環境管理程式 ,
env_manager用來建置服務環境:- 預設值為
virtualenv。 建議用於服務驗證。 -
local可供使用,但在進行服務驗證時可能會出錯。 通常僅用於快速偵錯。
- 預設值為
- 是否使用
install_mlflow在虛擬環境中安裝您環境中目前版本的 MLflow。 這個設定預設為False。 - 是否要更新及測試不同版本的套件相依性,以進行疑難解答或偵錯。 您可以使用 override 參數,將此指定為字串依賴關係的覆寫或新增項目清單,
pip_requirements_override。
例如:
import mlflow
run_id = "..."
model_uri = f"runs:/{run_id}/model"
mlflow.models.predict(
model_uri=model_uri,
input_data={"col1": 34.2, "col2": 11.2, "col3": "green"},
content_type="json",
env_manager="virtualenv",
install_mlflow=False,
pip_requirements_override=["pillow==10.3.0", "scipy==1.13.0"],
)
更新模型相依性
如果記錄模型所指定的相依性有任何問題,您可以使用 MLflow CLI 或 mlflow.models.model.update_model_requirements() MLflow Python API 來更新需求,而不需要記錄其他模型。
下列範例示範如何就地更新已記錄模型的 pip_requirements.txt。
您可以使用指定的套件版本更新現有的定義,或將不存在的需求新增至 pip_requirements.txt 檔案。 此檔案位於指定 model_uri 位置的 MLflow 模型成品內。
from mlflow.models.model import update_model_requirements
update_model_requirements(
model_uri=model_uri,
operation="add",
requirement_list=["pillow==10.2.0", "scipy==1.12.0"],
)
在部署之前驗證模型輸入
模型服務端點期望採用特殊格式的 JSON 輸入。 你可以在部署前, validate_serving_input 使用 MLflow 驗證你的模型輸入是否能在服務端點運作。
如果您的模型以有效的輸入範例來記錄,以下是在執行結果的工件標籤中自動產生的代碼範例。
from mlflow.models import validate_serving_input
model_uri = 'runs:/<run_id>/<artifact_path>'
serving_payload = """{
"messages": [
{
"content": "How many product categories are there?",
"role": "user"
}
]
}
"""
# Validate the serving payload works on the model
validate_serving_input(model_uri, serving_payload)
你也可以利用 convert_input_example_to_serving_input API 產生有效的 JSON 服務輸入,將任何輸入範例與已記錄的模型進行測試。
from mlflow.models import validate_serving_input
from mlflow.models import convert_input_example_to_serving_input
model_uri = 'runs:/<run_id>/<artifact_path>'
# Define INPUT_EXAMPLE with your own input example to the model
# A valid input example is a data instance suitable for pyfunc prediction
serving_payload = convert_input_example_to_serving_input(INPUT_EXAMPLE)
# Validate the serving payload works on the model
validate_serving_input(model_uri, serving_payload)
手動測試模型的服務
你可以手動測試模型的服務行為,步驟如下:
- 開啟筆記本並連接到使用 Databricks Runtime 版本的 All-Purpose 叢集,而不是 Databricks Runtime for 機器學習。
- 使用 MLflow 載入模型,然後嘗試從該處進行偵錯。
您也可以在本地電腦上載入模型,並進行除錯。 使用下列指引在本機載入模型:
import os
import mlflow
os.environ["MLFLOW_TRACKING_URI"] = "databricks://PROFILE"
ARTIFACT_URI = "model_uri"
if '.' in ARTIFACT_URI:
mlflow.set_registry_uri('databricks-uc')
local_path = mlflow.artifacts.download_artifacts(ARTIFACT_URI)
print(local_path)
conda env create -f local_path/artifact_path/conda.yaml
conda activate mlflow-env
mlflow.pyfunc.load_model(local_path/artifact_path)