你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

将工作区管理升级到 SDK v2

使用 V2 开发平台时,工作区在功能上保持不变。 但是,需要注意与网络相关的更改。 有关详细信息,请参阅 Azure 资源管理器上的新 API 平台的网络隔离更改

本文比较 SDK v1 和 SDK v2 中的方案。

创建工作区

  • SDK v1

    from azureml.core import Workspace
    
    ws = Workspace.create(
        name='my_workspace',
        location='eastus',
        subscription_id = '<SUBSCRIPTION_ID>'
        resource_group = '<RESOURCE_GROUP>'
    )
    
  • SDK v2

    from azure.ai.ml import MLClient
    from azure.ai.ml.entities import Workspace
    from azure.identity import DefaultAzureCredential
    
    # specify the details of your subscription
    subscription_id = "<SUBSCRIPTION_ID>"
    resource_group = "<RESOURCE_GROUP>"
    
    # get a handle to the subscription
    ml_client = MLClient(DefaultAzureCredential(), subscription_id, resource_group)
    
    # specify the workspace details
    ws = Workspace(
        name="my_workspace",
        location="eastus",
        display_name="My workspace",
        description="This example shows how to create a workspace",
        tags=dict(purpose="demo"),
    )
    
    ml_client.workspaces.begin_create(ws)
    
  • SDK v1

    from azureml.core import Workspace
    
    ws = Workspace.create(
        name='my_workspace',
        location='eastus',
        subscription_id = '<SUBSCRIPTION_ID>'
        resource_group = '<RESOURCE_GROUP>'
    )
    
    ple = PrivateEndPointConfig(
        name='my_private_link_endpoint',
        vnet_name='<VNET_NAME>',
        vnet_subnet_name='<VNET_SUBNET_NAME>',
        vnet_subscription_id='<SUBSCRIPTION_ID>', 
        vnet_resource_group='<RESOURCE_GROUP>'
    )
    
    ws.add_private_endpoint(ple, private_endpoint_auto_approval=True)
    
  • SDK v2

    from azure.ai.ml import MLClient
    from azure.ai.ml.entities import Workspace
    from azure.identity import DefaultAzureCredential
    
    # specify the details of your subscription
    subscription_id = "<SUBSCRIPTION_ID>"
    resource_group = "<RESOURCE_GROUP>"
    
    # get a handle to the subscription
    ml_client = MLClient(DefaultAzureCredential(), subscription_id, resource_group)
    
    ws = Workspace(
        name="private_link_endpoint_workspace,
        location="eastus",
        display_name="Private Link endpoint workspace",
        description="When using private link, you must set the image_build_compute property to a cluster name to use for Docker image environment building. You can also specify whether the workspace should be accessible over the internet.",
        image_build_compute="cpu-compute",
        public_network_access="Disabled",
        tags=dict(purpose="demonstration"),
    )
    
    ml_client.workspaces.begin_create(ws)
    

使用参数加载/连接到工作区

  • SDK v1

    from azureml.core import Workspace
    ws = Workspace.from_config()
    
    # specify the details of your subscription
    subscription_id = "<SUBSCRIPTION_ID>"
    resource_group = "<RESOURCE_GROUP>"
    
    # get handle on the workspace
    ws = Workspace.get(
        subscription_id='<SUBSCRIPTION_ID>',
        resource_group='<RESOURCE_GROUP>',
        name='my_workspace',
    )
    
  • SDK v2

    from azure.ai.ml import MLClient
    from azure.ai.ml.entities import Workspace
    from azure.identity import DefaultAzureCredential
    
    # specify the details of your subscription
    subscription_id = "<SUBSCRIPTION_ID>"
    resource_group = "<RESOURCE_GROUP>"
    
    # get handle on the workspace
    ws = MLClient(
        DefaultAzureCredential(),
        subscription_id='<SUBSCRIPTION_ID>',
        resource_group_name='<RESOURCE_GROUP>',
        workspace_name='my_workspace'
    )
    

使用配置文件加载/连接到工作区

  • SDK v1

    from azureml.core import Workspace
    
    ws = Workspace.from_config()
    ws.get_details()
    
  • SDK v2

    from azure.ai.ml import MLClient
    from azure.ai.ml.entities import Workspace
    from azure.identity import DefaultAzureCredential
    
    ws = MLClient.from_config(
        DefaultAzureCredential()
    )
    

SDK v1 和 SDK v2 中关键功能的映射

SDK v1 中的功能 SDK v2 中的粗略映射
SDK v1 中的方法/API(使用指向参考文档的链接) SDK v2 中的方法/API(使用指向参考文档的链接)

有关详细信息,请参阅: