Share via


針對部署為 Web 服務的模型設定驗證

適用於:Python SDK azureml 第 1 版

Azure Machine Learning 可讓您將已定型的機器學習模型部署為 Web 服務。 在本文中,您將了解如何設定這些部署的驗證。

Azure Machine Learning 所建立的模型部署可以設定為使用兩種驗證方法的其中一種:

  • 金鑰型:使用靜態金鑰向 Web 服務驗證。

  • 權杖型:必須從 Azure Machine Learning 工作區中取得暫時權杖 (使用 Microsoft Entra ID),並用來向 Web 服務驗證。 此權杖會在一段時間後到期,必須更新,才能繼續使用 Web 服務。

    注意

    只有在部署至 Azure Kubernetes Service 時,才可使用權杖型驗證。

金鑰式驗證

Azure Kubernetes Service 上部署的 Web 服務預設「啟用」金鑰型驗證。

Azure 容器執行個體 (ACI) 部署的服務預設「停用」金鑰型驗證,但您可以在建立 ACI Web 服務時設定 auth_enabled=True 來啟用。 下列程式碼示範啟用金鑰型驗證來建立 ACI 部署設定。

from azureml.core.webservice import AciWebservice

aci_config = AciWebservice.deploy_configuration(cpu_cores = 1,
                                                memory_gb = 1,
                                                auth_enabled=True)

其後,您可使用 Model 類別,在部署中使用自訂 ACI 設定。

from azureml.core.model import Model, InferenceConfig


inference_config = InferenceConfig(entry_script="score.py",
                                   environment=myenv)
aci_service = Model.deploy(workspace=ws,
                       name="aci_service_sample",
                       models=[model],
                       inference_config=inference_config,
                       deployment_config=aci_config)
aci_service.wait_for_deployment(True)

若要擷取驗證金鑰,請使用 aci_service.get_keys()。 若要重新產生金鑰,請使用 regen_key() 函式,並傳遞 PrimarySecondary

aci_service.regen_key("Primary")
# or
aci_service.regen_key("Secondary")

權杖型驗證

當啟用 Web 服務的權杖驗證時,使用者必須向 Web 服務出示 Machine Learning JSON Web 權杖才能加以存取。 權杖會在指定的時間範圍之後過期,且需要重新整理才能繼續進行呼叫。

  • 在部署至 Azure Kubernetes Service 時,依預設會停用權杖驗證。
  • 部署至 Azure 容器執行個體時,不支援權杖驗證。
  • 權杖驗證無法與金鑰型驗證同時使用

若要控制權杖驗證,請在建立或更新部署時使用 token_auth_enabled 參數:

from azureml.core.webservice import AksWebservice
from azureml.core.model import Model, InferenceConfig

# Create the config
aks_config = AksWebservice.deploy_configuration()

#  Enable token auth and disable (key) auth on the webservice
aks_config = AksWebservice.deploy_configuration(token_auth_enabled=True, auth_enabled=False)

aks_service_name ='aks-service-1'

# deploy the model
aks_service = Model.deploy(workspace=ws,
                           name=aks_service_name,
                           models=[model],
                           inference_config=inference_config,
                           deployment_config=aks_config,
                           deployment_target=aks_target)

aks_service.wait_for_deployment(show_output = True)

如果已啟用權杖驗證,即可使用 get_token 方法來擷取 JSON Web 權杖 (JWT),以及該權杖的到期時間:

提示

如果您使用服務主體來取得權杖,並希望主體有最低必要存取權可取得權杖,請將主體指派為工作區的讀取者角色。

token, refresh_by = aks_service.get_token()
print(token)

重要

您將必須在權杖的 refresh_by 時間之後要求新的權杖。 如果需要在 Python SDK 以外重新整理權杖,則其中一個選項是使用 REST API 搭配服務主體驗證來定期進行 service.get_token() 呼叫,如先前所述。

強烈建議在與 Azure Kubernetes Service 叢集相同的區域中建立 Azure Machine Learning 工作區。

若要使用權杖進行驗證,Web 服務會呼叫 Azure Machine Learning 工作區的建立區域。 如果無法使用工作區區域,則即使叢集與工作區位於不同的區域,您也無法擷取 Web 服務的權杖。 結果,在工作區區域再次可用之前,無法使用 Microsoft Entra 驗證。

此外,叢集區域和工作區區域相距越遠,擷取權杖所需的時間越長。

下一步

如需向已部署的模型驗證的詳細資訊,請參閱針對部署為 Web 服務的模型建立用戶端