Share via


將部署端點升級至 SDK v2

使用 SDK/CLI v1,您可以在 ACI 或 AKS 上部署模型做為 Web 服務。 現有的 v1 模型部署和 Web 服務會繼續正常運作,但使用 SDK/CLI v1 在 ACI 或 AKS 上部署模型,因為 Web 服務現在被視為 舊版。 針對新的模型部署,建議您升級至 v2。

在 v2 中,我們提供受控端點或 Kubernetes 端點。 如需 v1 和 v2 的比較,請參閱 端點和部署

有數個部署漏斗圖,例如第 2 版中的受控線上端點、kubernetes 線上端點 (包括 Azure Kubernetes Services、已啟用 Arc 的 Kubernetes),以及第 1 版中的 Azure 容器執行個體 (ACI) 和 Kubernetes Services (AKS) webservices。 在本文中,我們將著重於部署至 ACI webservices (v1) 和受控線上端點 (v2) 的比較。

本文中的範例示範如何:

  • 將模型部署至 Azure
  • 使用端點評分
  • 刪除 webservice/端點

建立推斷資源

  • SDK v1
    1. 設定模型、環境和評分指令碼:

      # configure a model. example for registering a model 
      from azureml.core.model import Model
      model = Model.register(ws, model_name="bidaf_onnx", model_path="./model.onnx")
      
      # configure an environment
      from azureml.core import Environment
      env = Environment(name='myenv')
      python_packages = ['nltk', 'numpy', 'onnxruntime']
      for package in python_packages:
          env.python.conda_dependencies.add_pip_package(package)
      
      # configure an inference configuration with a scoring script
      from azureml.core.model import InferenceConfig
      inference_config = InferenceConfig(
          environment=env,
          source_directory="./source_dir",
          entry_script="./score.py",
      )
      
    2. 設定及部署 ACI webservice

      from azureml.core.webservice import AciWebservice
      
      # defince compute resources for ACI
      deployment_config = AciWebservice.deploy_configuration(
          cpu_cores=0.5, memory_gb=1, auth_enabled=True
      )
      
      # define an ACI webservice
      service = Model.deploy(
          ws,
          "myservice",
          [model],
          inference_config,
          deployment_config,
          overwrite=True,
      )
      
      # create the service 
      service.wait_for_deployment(show_output=True)
      

如需註冊模型的詳細資訊,請參閱從本地檔案註冊模型

  • SDK v2

    1. 設定模型、環境和評分指令碼:

      from azure.ai.ml.entities import Model
      # configure a model
      model = Model(path="../model-1/model/sklearn_regression_model.pkl")
      
      # configure an environment
      from azure.ai.ml.entities import Environment
      env = Environment(
          conda_file="../model-1/environment/conda.yml",
          image="mcr.microsoft.com/azureml/openmpi3.1.2-ubuntu18.04:20210727.v1",
      )
      
      # configure an inference configuration with a scoring script
      from azure.ai.ml.entities import CodeConfiguration
      code_config = CodeConfiguration(
              code="../model-1/onlinescoring", scoring_script="score.py"
          )
      
    2. 設定和建立線上端點

      import datetime
      from azure.ai.ml.entities import ManagedOnlineEndpoint
      
      # create a unique endpoint name with current datetime to avoid conflicts
      online_endpoint_name = "endpoint-" + datetime.datetime.now().strftime("%m%d%H%M%f")
      
      # define an online endpoint
      endpoint = ManagedOnlineEndpoint(
          name=online_endpoint_name,
          description="this is a sample online endpoint",
          auth_mode="key",
          tags={"foo": "bar"},
      )
      
      # create the endpoint:
      ml_client.begin_create_or_update(endpoint)
      
    3. 設定和建立線上部署

      from azure.ai.ml.entities import ManagedOnlineDeployment
      
      # define a deployment
      blue_deployment = ManagedOnlineDeployment(
          name="blue",
          endpoint_name=online_endpoint_name,
          model=model,
          environment=env,
          code_configuration=code_config,
          instance_type="Standard_F2s_v2",
          instance_count=1,
      )
      
      # create the deployment:
      ml_client.begin_create_or_update(blue_deployment)
      
      # blue deployment takes 100 traffic
      endpoint.traffic = {"blue": 100}
      ml_client.begin_create_or_update(endpoint)
      

如需端點和部署概念的詳細資訊,請參閱什麼是線上端點?

提交要求

  • SDK v1

    import json
    data = {
        "query": "What color is the fox",
        "context": "The quick brown fox jumped over the lazy dog.",
    }
    data = json.dumps(data)
    predictions = service.run(input_data=data)
    print(predictions)
    
  • SDK v2

    # test the endpoint (the request will route to blue deployment as set above)
    ml_client.online_endpoints.invoke(
        endpoint_name=online_endpoint_name,
        request_file="../model-1/sample-request.json",
    )
    
    # test the specific (blue) deployment
    ml_client.online_endpoints.invoke(
        endpoint_name=online_endpoint_name,
        deployment_name="blue",
        request_file="../model-1/sample-request.json",
    )
    

刪除資源

  • SDK v1

    service.delete()
    
  • SDK v2

    ml_client.online_endpoints.begin_delete(name=online_endpoint_name)
    

SDK v1 和 SDK v2 中主要功能的對應

SDK v1 中的功能 SDK v2 中的粗略對應
azureml.core.model.Model 類別 azure.ai.ml.entities.Model 類別
azureml.core.Environment 類別 azure.ai.ml.entities.Environment 類別
azureml.core.model.InferenceConfig 類別 azure.ai.ml.entities.CodeConfiguration 類別
azureml.core.webservice.AciWebservice 類別 azure.ai.ml.entities.OnlineDeployment 類別 (和 azure.ai.ml.entities.ManagedOnlineEndpoint 類別)
Model.deployWebservice.deploy ml_client.begin_create_or_update(online_deployment)
Webservice.run ml_client.online_endpoints.invoke
Webservice.delete ml_client.online_endpoints.delete

如需相關資訊,請參閱

v2 文件:

v1 文件: