Create a hub using the Azure Machine Learning SDK and CLI

Important

Some of the features described in this article might only be available in 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 Studio resources using the Azure Machine Learning SDK and Azure CLI (with machine learning extension):

  • An Azure AI Studio 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 Studio 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)