共用方式為


使用 Azure 機器學習 SDK 和 CLI 建立中樞

重要

本文所述的部分功能可能僅適用於預覽版。 此預覽版本沒有服務等級協定,不建議將其用於生產工作負載。 可能不支援特定功能,或可能已經限制功能。 如需詳細資訊,請參閱 Microsoft Azure 預覽版增補使用條款

在本文中,您將瞭解如何使用 Azure 機器學習 SDK 和 Azure CLI 建立下列 AI Studio 資源(搭配機器學習延伸模組):

  • Azure AI Studio 中樞
  • Azure AI Services 連線

必要條件

設定您的環境

使用下列索引標籤來選取您是否使用 Python SDK 或 Azure CLI:

  1. 依照 SDK 快速入門 中所述安裝 Python。

  2. 安裝 Azure Machine Learning SDK v2

  3. 安裝 azure-identity:pip install azure-identity。 如果在筆記本資料格中, 請使用 %pip install azure-identity

  4. 提供您的訂用帳戶詳細資料:

    # Enter details of your subscription
    subscription_id = "<SUBSCRIPTION_ID>"
    resource_group = "<RESOURCE_GROUP>"
  5. 取得訂用帳戶的控制代碼。 本文中的所有 Python 程式碼都會使用 ml_client

    # get a handle to the subscription
    
    from azure.ai.ml import MLClient
    from azure.identity import DefaultAzureCredential
    
    ml_client = MLClient(DefaultAzureCredential(), subscription_id, resource_group)
  6. (選擇性) 如果您有多個帳戶,請將您想要使用的 Microsoft Entra ID 租用戶識別碼新增至 DefaultAzureCredential。 在 Azure 入口網站的 [Microsoft Entra ID,外部身分識別] 下,尋找您的租用戶識別碼。

    DefaultAzureCredential(interactive_browser_tenant_id="<TENANT_ID>")
    
  7. (選擇性) 如果您正在 Azure Government - USAzure China 21Vianet 區域,請指定您要驗證的區域。 您可以使用 DefaultAzureCredential 來指定區域。 下列範例會向 Azure Government - 美國區域進行驗證:

    from azure.identity import AzureAuthorityHosts
    DefaultAzureCredential(authority=AzureAuthorityHosts.AZURE_GOVERNMENT)
    

建立 AI Studio 中樞和 AI 服務連線

使用下列範例來建立新的中樞。 請以您自己的值取代範例字串:

from azure.ai.ml.entities import Hub

my_hub_name = "myexamplehub"
my_location = "East US"
my_display_name = "My Example Hub"

# construct a basic hub
my_hub = Hub(name=my_hub_name, 
            location=my_location,
            display_name=my_display_name)

created_hub = ml_client.workspaces.begin_create(my_hub).result()

建立 AI 服務連線

建立自己的 AI 服務之後,您可以將它連線到您的中樞:

from azure.ai.ml.entities import AzureAIServicesConnection

# constrict an AI Services connection
my_connection_name = "myaiservivce"
my_endpoint = "demo.endpoint" # this could also be called target
my_api_keys = None # leave blank for Authentication type = AAD
my_ai_services_resource_id = "" # ARM id required

my_connection = AzureAIServicesConnection(name=my_connection_name,
                                    endpoint=my_endpoint, 
                                    api_key= my_api_keys,
                                    ai_services_resource_id=my_ai_services_resource_id)

# Create the connection
ml_client.connections.create_or_update(my_connection)