使用 Azure Machine Learning SDK 和 CLI(classic)建立一個樞紐

僅適用於:Foundry(經典)入口。 這篇文章無法在新的 Foundry 入口網站中提供。 了解更多關於新入口網站的資訊。

本文中的連結可能會開啟新版 Microsoft Foundry 文件的內容,而非您目前正在瀏覽的 Foundry(經典版)文件。

在本文中,您將學習如何利用 Azure Machine Learning SDK 和 Azure CLI(搭配機器學習擴充功能)來建立以下 Microsoft Foundry 資源:

  • 鑄造中心
  • Foundry 連線

集線器僅用於 集線器型專案Foundry 專案不會使用集線器。 更多資訊請參閱 專案類型

先決條件

建立你的環境

請使用以下分頁選擇您使用的是 Python SDK 還是 Azure CLI:

  1. 安裝套件。 (如果在筆記本的單元格中,請改用 %pip install 。)

    pip install azure-ai-ml
    pip install azure-identity
    
  2. 提供您的訂閱詳情:

    # Enter details of your subscription
    subscription_id = "<SUBSCRIPTION_ID>"
    resource_group = "<RESOURCE_GROUP>"
  3. 取得訂閱的控制代碼。 本文中所有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)
  4. (可選)如果你有多個帳號,請把你想用的Microsoft Entra ID的租戶 ID 加入 DefaultAzureCredential。 在Azure入口網站Microsoft Entra ID外部身份下查詢您的租戶ID。

    DefaultAzureCredential(interactive_browser_tenant_id="<TENANT_ID>")
    
  5. (可選)如果你在 Azure Government - USAzure China 21Vianet 區域工作,請指定你想要驗證的區域。 你可以用 DefaultAzureCredential來指定區域。 以下範例進行身份驗證至 Azure 政府 - 美國地區:

    from azure.identity import AzureAuthorityHosts
    DefaultAzureCredential(authority=AzureAuthorityHosts.AZURE_GOVERNMENT)
    
  6. 確認連結。

    for hub in ml_client.workspaces.list():
        print(f"  - {hub.name}")
    

若收到認證錯誤,請確保Azure憑證已設定(執行 az login 或透過 Azure Identity SDK 設定憑證)。 如果你收到權限錯誤,請檢查你在訂閱或資源群組中是否有貢獻者角色。

參考資料MLClientDefaultAzureCredential

建立 Foundry 中樞與 Microsoft Foundry 連線

請參考以下範例來建立新的集點。 用你自己的數值替換範例字串值:

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
)

# Create the hub and wait for completion
created_hub = ml_client.workspaces.begin_create(my_hub).result()
print(f"Created hub: {created_hub.name}")

此程式碼會建立一個包含指定名稱、位置與顯示名稱的新樞紐。 Azure 會自動配置相關 Azure 儲存體 和 Azure Key Vault 資源。

參考資料HubMLClient.workspaces.begin_create

建立 Foundry 連結

在同一資源群組中,建立自己的 Foundry 資源Azure OpenAI 資源後,你可以將其連接到你的集線器。 你也可以在同一訂閱內,從任何資源群組連接 Azure AI 搜尋服務

  1. 將你的集線器納入你的 ml_client 連線:

    • 請輸入您的訂閱資訊。 對於 <AML_WORKSPACE_NAME>,請輸入你的集線器名稱:

      # Enter details of your AML workspace
      subscription_id = "<SUBSCRIPTION_ID>"
      resource_group = "<RESOURCE_GROUP>"
      workspace = "<AML_WORKSPACE_NAME>"
    • 取得中樞的控制代碼:

      # get a handle to the workspace
      from azure.ai.ml import MLClient
      from azure.identity import DefaultAzureCredential
      
      ml_client = MLClient(
          DefaultAzureCredential(), subscription_id, resource_group, workspace
      )
  2. ml_client 來建立與你的 Foundry 工具的連結。 你可以在 Azure portal 中的 資源管理:金鑰與端點 找到端點。 對於 Foundry 資源,請使用 AI Services 端點。 對於 Azure AI 搜尋服務,請使用端點的 URL。

    from azure.ai.ml.entities import AzureAIServicesConnection
    
    # Construct a connection to Azure AI Services
    my_connection_name = "my-ai-services-connection"  # Any name you want
    aiservices_resource_name = "<your-resource-name>"  # From Azure portal
    my_endpoint = "<your-endpoint>"  # From Azure portal
    my_api_keys = None  # Leave blank to use Azure Entra ID (AAD) authentication
    my_ai_services_resource_id = f"/subscriptions/{subscription_id}/resourceGroups/{resource_group}/providers/Microsoft.CognitiveServices/accounts/{aiservices_resource_name}"
    
    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)
    print(f"Created connection: {my_connection.name}")
    

    參考資料AzureAIServicesConnection、MLClient.connections

用現有的依賴資源建立一個樞紐

預設情況下,中樞會自動建立相關的 Azure 儲存體 與 Azure Key Vault 資源。 如果你想重用現有的 Azure 儲存體 或 Azure Key Vault 資源,可以在建立集線器時指定它們。 在以下範例中,請將佔位符值替換為你自己的資源 ID:

提示

你可以從Azure入口網站取得儲存帳號和金鑰庫的資源 ID,方法是進入資源的總覽頁面,選擇 JSON view。 資源 ID 位於 ID 欄位。 你也可以使用 Azure CLI 來取得資源 ID。 例如,使用 az storage account show --name {my_storage_account_name} --query "id"az keyvault show --name {my_key_vault_name} --query "id"

from azure.ai.ml.entities import Hub

my_hub_name = "myexamplehub"
my_location = "East US"
my_display_name = "My Example Hub"
my_resource_group = "myresourcegroupname"
my_storage_account_id = "/subscriptions/<subscription-id>/resourceGroups/<resource-group>/providers/Microsoft.Storage/storageAccounts/<storage-account-name>"
my_key_vault_id = "/subscriptions/<subscription-id>/resourceGroups/<resource-group>/providers/Microsoft.KeyVault/vaults/<key-vault-name>"

# Construct a hub with existing dependency resources
my_hub = Hub(
    name=my_hub_name,
    location=my_location,
    display_name=my_display_name,
    resource_group=my_resource_group,
    storage_account_id=my_storage_account_id,
    key_vault_id=my_key_vault_id
)

# Create the hub
created_hub = ml_client.workspaces.begin_create(my_hub).result()
print(f"Created hub with existing resources: {created_hub.name}")

要查找現有資源的資源 ID,請造訪 Azure 入口網站,導覽至資源的 Overview頁面,選擇 JSON view。 資源 ID 會出現在 ID 欄位。 或者,可以用 Azure CLI:

# Get Storage account resource ID
az storage account show --name <storage-account-name> --resource-group <resource-group> --query "id"

# Get Key Vault resource ID
az keyvault show --name <key-vault-name> --resource-group <resource-group> --query "id"

參考資料Hub

更新 Azure 應用程式 Insights 和 Azure Container Registry

要在提示流程中使用自訂環境,您需要為您的樞紐設定 Azure Container Registry。 要使用 Azure 應用程式 Insights 進行 Prompt Flow 部署,你需要為你的中樞配置一個 Azure 應用程式 Insights 資源。 更新工作區附加的 Azure Container Registry 或 Application Insights 資源,可能會破壞先前工作脈絡、部署的推論端點,或你在工作空間中重跑先前工作的能力。 與 Foundry 樞紐關聯後,Azure Container Registry 與 Application Insights 資源無法解除關聯(設為 null)。

你可以使用 Azure 入口網站、Azure SDK/CLI 選項或基礎結構即代碼範本,來更新中樞的 Azure 應用程式 Insights 和 Azure 容器登錄服務。

from azure.ai.ml.entities import Hub

my_app_insights = "{APPLICATION_INSIGHTS_ARM_ID}"
my_container_registry = "{CONTAINER_REGISTRY_ARM_ID}"

# construct a hub with Application Insights and Container Registry
my_hub = Hub(name="myexamplehub", 
             location="East US", 
             application_insights=my_app_insights,
             container_registry=my_container_registry)

# update_dependent_resources is used to give consent to update the workspace dependent resources.
updated_hub = ml_client.workspaces.begin_update(workspace=my_hub, update_dependent_resources=True).result()
print(f"Hub updated: {updated_hub.name}")

此腳本會更新現有的樞紐,加入指定的應用程式洞察與容器登錄資源。 參數 update_dependent_resources=True 確認了更新。

參考資料:Hub,MLClient.workspaces.begin_update()