Muokkaa

Jaa


Create a hub using the Azure Machine Learning SDK and CLI

Important

Items marked (preview) in this article are currently in public preview. This preview is provided without a service-level agreement, and we don't recommend it for production workloads. Certain features might not be supported or might have constrained capabilities. For more information, see Supplemental Terms of Use for Microsoft Azure Previews.

In this article, you learn how to create the following AI Foundry resources using the Azure Machine Learning SDK and Azure CLI (with machine learning extension):

  • An Azure AI Foundry hub
  • An Azure AI Services connection

Prerequisites

Set up your environment

Use the following tabs to select whether you're using the Python SDK or Azure CLI:

  1. Install Python as described in the SDK quickstart.

  2. Install the Azure Machine Learning SDK v2.

  3. Install azure-identity: pip install azure-identity. If in a notebook cell, use %pip install azure-identity.

  4. Provide your subscription details:

    # Enter details of your subscription
    subscription_id = "<SUBSCRIPTION_ID>"
    resource_group = "<RESOURCE_GROUP>"
  5. Get a handle to the subscription. All the Python code in this article uses 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. (Optional) If you have multiple accounts, add the tenant ID of the Microsoft Entra ID you wish to use into the DefaultAzureCredential. Find your tenant ID from the Azure portal under Microsoft Entra ID, External Identities.

    DefaultAzureCredential(interactive_browser_tenant_id="<TENANT_ID>")
    
  7. (Optional) If you're working on in the Azure Government - US or Azure China 21Vianet regions, specify the region into which you want to authenticate. You can specify the region with DefaultAzureCredential. The following example authenticates to the Azure Government - US region:

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

Create the AI Foundry hub and AI Services connection

Use the following examples to create a new hub. Replace example string values with your own values:

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()

Create an AI Services connection

After creating your own AI Services, you can connect it to your hub:

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)

Create an AI Foundry hub using existing dependency resources

You can also create a hub using existing resources such as Azure Storage and Azure Key Vault. In the following examples, replace the example string values with your own values:

Tip

You can retrieve the resource ID of the storage account and key vault from the Azure Portal by going to the resource's overview and selecting JSON view. The resource ID is located in the id field. You can also use the Azure CLI to retrieve the resource ID. For example, az storage account show --name {my_storage_account_name} --query "id" and 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/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/resourceGroups/myresourcegroupname/providers/Microsoft.Storage/storageAccounts/mystorageaccountname"
my_key_vault_id = "/subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/resourceGroups/myresourcegroupname/providers/Microsoft.KeyVault/vaults/mykeyvaultname"

# construct a basic hub
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)

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