次の方法で共有


Azure Machine Learning SDK と CLI を使用してハブを作成する

重要

この記事で説明する機能の一部は、プレビューでのみ使用できる場合があります。 このプレビューはサービス レベル アグリーメントなしで提供されており、運用環境ではお勧めしません。 特定の機能はサポート対象ではなく、機能が制限されることがあります。 詳しくは、Microsoft Azure プレビューの追加使用条件に関するページをご覧ください。

この記事では、Azure Machine Learning SDK と Azure CLI (機械学習拡張機能を含む) を使用して次の AI Studio リソースを作成する方法を学習します。

  • Azure AI Studio ハブ
  • Azure AI サービス接続

前提条件

  • Azure サブスクリプション。 Azure サブスクリプションをお持ちでない場合は、開始する前に 無料アカウント を作成してください。 無料または有料版の Azure AI Studio を今すぐお試しください。

環境を設定する

次のタブを使用して、Python SDK と Azure CLI のどちらを使用しているかを選択してください。

  1. SDK クイック スタートの説明に従って Python をインストールします。

  2. Azure Machine Learning SDK v2 をインストールします

  3. azure-identity をインストールします: pip install azure-identity。 ノートブック セルで %pip install azure-identity を使います。

  4. サブスクリプションの詳細を指定します。

    # Enter details of your subscription
    subscription_id = "<SUBSCRIPTION_ID>"
    resource_group = "<RESOURCE_GROUP>"
  5. サブスクリプションへのハンドルを取得します。 この記事のすべての 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)
  6. (省略可能) 複数のアカウントがある場合は、使う Microsoft Entra ID のテナント ID を DefaultAzureCredential に追加します。 Azure portal で、[Microsoft Entra ID] の [外部 ID] からテナント ID を見つけます。

    DefaultAzureCredential(interactive_browser_tenant_id="<TENANT_ID>")
    
  7. (省略可能) Azure Government - US または Azure China 21Vianet リージョン内で作業している場合は、認証するリージョンを指定します。 DefaultAzureCredential を使用してそのリージョンを指定できます。 次の例では、Azure Government - US リージョンに対して認証を行います。

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

AI Studio ハブと 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 サービスを作成したら、それをハブに接続できます。

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)