重要
本文中标记了“(预览版)”的项目目前为公共预览版。 此预览版未提供服务级别协议,不建议将其用于生产工作负载。 某些功能可能不受支持或者受限。 有关详细信息,请参阅 Microsoft Azure 预览版补充使用条款。
本文介绍如何使用 Azure 机器学习 SDK 和 Azure CLI 创建以下 Azure AI Foundry 资源(使用机器学习扩展):
- 一个 Azure AI Foundry 中心
- Azure AI 服务连接
注释
中心仅用于 基于中心的项目。 Foundry 项目不使用中心。 有关详细信息,请参阅 项目类型。
先决条件
- 一份 Azure 订阅。 如果没有 Azure 订阅,请在开始操作前先创建一个免费帐户。 立即试用 Azure AI Foundry 的免费或付费版本。
配置你的环境
使用以下选项卡选择是使用 Python SDK 还是 Azure CLI:
安装 azure-identity:
pip install azure-identity
。 如果在笔记本单元格中,请使用%pip install azure-identity
。提供订阅的详细信息:
# Enter details of your subscription subscription_id = "<SUBSCRIPTION_ID>" resource_group = "<RESOURCE_GROUP>"
获取订阅的句柄。 本文中的所有 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)
(可选)如果您有多个帐户,请在
DefaultAzureCredential
中添加您希望使用的 Microsoft Entra ID 的租户 ID。 在 Azure 门户 中,在 Microsoft Entra ID,外部标识 下找到你的租户 ID。DefaultAzureCredential(interactive_browser_tenant_id="<TENANT_ID>")
(可选)如果你在 Azure 政府 - 美国或 Azure 中国世纪互联区域工作,请指定要在其中进行身份验证的区域。 可以使用
DefaultAzureCredential
指定区域。 以下示例向 Azure 政府 - 美国区域进行身份验证:from azure.identity import AzureAuthorityHosts DefaultAzureCredential(authority=AzureAuthorityHosts.AZURE_GOVERNMENT)
创建 Azure AI Foundry 中心和 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 服务后,可以将其连接到中心。
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 )
使用
ml_client
创建与 AI Services 的连接:from azure.ai.ml.entities import AzureAIServicesConnection # construct an AI Services connection my_connection_name = "myaiservivce" # any name you want aiservices_resource_name = <resource_name> # copy from Azure AI Foundry portal my_endpoint = "<endpoint>" # copy from Azure AI Foundry portal my_api_keys = None # leave blank for Authentication type = AAD 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)
使用现有依赖项资源创建 Azure AI Foundry 中心
还可以使用现有资源(例如 Azure 存储和 Azure 密钥保管库)创建中心。 在以下示例中,将示例字符串值替换为你自己的值:
小窍门
可以通过转到资源概述并选择“JSON 视图”,从 Azure 门户检索存储帐户和密钥保管库的资源 ID。 资源 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/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()