# Configure Azure Machine Learning and Microsoft Foundry

Throughout this guide you'll see references to both Azure AI Foundry and Microsoft Foundry simultaneously, but that's because Microsoft Foundry has superseded Azure AI Foundry, but still referencing to it for the "classic" experience, which is the one that supports Hub-based projects and then, models from the Hugging Face Hub.

This guide explains how to configure Azure Machine Learning and Microsoft Foundry in your Microsoft Azure subscription. These pre-requisites are required to run the Microsoft Foundry examples in this documentation, as well as any example on Azure Machine Learning.

You can either follow along the below steps, or either read more about those in the [Azure Machine Learning Tutorial: Create resources you need to get started](https://learn.microsoft.com/en-us/azure/machine-learning/quickstart-create-resources?view=azureml-api-2).

Also note that the steps below will use the `az` CLI i.e., the Azure CLI, but there are other alternatives such as e.g. the Azure SDK for Python, or even the Azure Portal, so pick the one you feel more comfortable with.

## Azure Account

A Microsoft Azure account with an active subscription. If you don't have a Microsoft Azure account, you can now [create one for free](https://azure.microsoft.com/en-us/pricing/purchase-options/azure-account), including 200 USD worth of credits to use within the next 30 days after the account creation.

## Azure CLI

The Azure CLI (`az`) installed on the instance that you're running this example on, see [the installation steps](https://learn.microsoft.com/en-us/cli/azure/install-azure-cli?view=azure-cli-latest), and follow the steps of the preferred method based on your instance. Then log in into your subscription as follows:

```bash
az login
```

More information at [Sign in with Azure CLI - Login and Authentication](https://learn.microsoft.com/en-us/cli/azure/authenticate-azure-cli?view=azure-cli-latest).

## Azure CLI extension for Azure Machine Learning

Besides the Azure CLI (`az`), you also need to install the Azure Machine Learning CLI extension (`az ml`) which will be used to create the Azure Machine Learning and Microsoft Foundry required resources.

First you will need to list the current extensions and remove any `ml`-related extension before installing the latest one i.e., v2.

```bash
az extension list
az extension remove --name azure-cli-ml
az extension remove --name ml
```

Then you can install the `az ml` v2 extension as follows:

```bash
az extension add --name ml
```

More information at [Azure Machine Learning (ML) - Install and setup the CLI (v2)](https://learn.microsoft.com/en-us/azure/machine-learning/how-to-configure-cli?view=azureml-api-2&tabs=public).

## Azure Resource Group

An Azure Resource Group under the one you will create the Microsoft Foundry (classic) i.e., an Azure AI Foundry Hub-based project (note it will create an Azure AI Foundry resource as an Azure Machine Learning Workspace, but not the other way around, meaning that the Azure AI Foundry Hub will be listed as an Azure Machine Learning workspace, but leveraging the Microsoft Foundry capabilities for Gen AI), and the rest of the required resources. If you don't have one, you can create it as follows:

```bash
az group create --name huggingface-azure-rg --location eastus
```

Then, you can ensure that the resource group was created successfully by e.g. listing all the available resource groups that you have access to on your subscription:

```bash
az group list --output table
```

More information at [Manage Azure resource groups by using Azure CLI](https://learn.microsoft.com/en-us/azure/azure-resource-manager/management/manage-resource-groups-cli).

You can also create the Azure Resource Group [via the Azure Portal](https://learn.microsoft.com/en-us/azure/azure-resource-manager/management/manage-resource-groups-portal), or [via the Azure Resource Management Python SDK](https://learn.microsoft.com/en-us/azure/developer/python/sdk/examples/azure-sdk-example-resource-group?tabs=bash) (requires it to be installed as `pip install azure-mgmt-resource` in advance).

## Azure AI Foundry Hub-based project

An Azure AI Foundry Hub-based project (given that the new experience on Microsoft Foundry does not yet support Hugging Face models, but rather only the classic experience which is the same as saying Azure AI Foundry) under the aforementioned subscription and resource group. If you don't have one, you can create it as follows:

```bash
az ml workspace create \
    --kind hub \
    --name huggingface-azure-hub \
    --resource-group huggingface-azure-rg \
    --location eastus
```

Note that the main difference with an standard Azure Machine Learning Workspace is that the Microsoft Foundry (classic) i.e., Azure AI Foundry Hub, requires you to specify the `--kind hub`, removing it would create a standard Azure Machine Learning Workspace instead, so you wouldn't benefit from the features that the Microsoft Foundry brings. But, when you create an Azure AI Foundry Hub, you can still benefit from all the features that Azure Machine Learning brings, since Microsoft Foundry will still rely on Azure Machine Learning, but not the other way around.

Then, you can ensure that the workspace was created successfully by e.g. listing all the available workspaces that you have access to on your subscription:

```bash
az ml workspace list --filtered-kinds hub --query "[].{Name:name, Kind:kind}" --resource-group huggingface-azure-rg --output table
```

The `--filtered-kinds` argument has been recently included as of [Azure Machine Learning CLI 2.37.0](https://learn.microsoft.com/en-us/azure/machine-learning/azure-machine-learning-release-notes-cli-v2?view=azureml-api-2#azure-machine-learning-cli-v2-v-2370), meaning that you may need to upgrade `az ml` as `az extension update --name ml`.

Once the Azure AI Foundry Hub-based project is created, you need to create an Azure AI Foundry project linked to that Hub, to do so you first need to obtain the Azure AI Foundry Hub ID of the recently created Hub as follows (replace the resource names with yours):

```bash
az ml workspace show \
    --name huggingface-azure-hub \
    --resource-group huggingface-azure-rg \
    --query "id" \
    -o tsv
```

That command will provide the ID as follows `/subscriptions//resourceGroups//providers/Microsoft.MachineLearningServices/workspaces/huggingface-azure-hub`, meaning that you can also format it manually yourself with the appropriate replacements. Then you need to run the following command to create the Microsoft Foundry Project for that Hub as:

```bash
az ml workspace create \
    --kind project \
    --hub-id $(az ml workspace show --name huggingface-azure-hub --resource-group huggingface-azure-rg --query "id" -o tsv) \
    --name huggingface-azure-project \
    --resource-group huggingface-azure-rg \
    --location eastus
```

Finally, you can verify that it was correctly created with the following command:

```bash
az ml workspace list --filtered-kinds project --query "[].{Name:name, Kind:kind}" --resource-group huggingface-azure-rg --output table
```

More information at [How to create and manage an Azure AI Foundry Hub](https://learn.microsoft.com/en-us/azure/ai-foundry/how-to/create-azure-ai-resource?tabs=portal) and at [How to create a Hub using the Azure CLI](https://learn.microsoft.com/en-us/azure/ai-foundry/how-to/develop/create-hub-project-sdk?tabs=azurecli).

You can also create the Azure AI Foundry Hub [via the Azure Portal](https://learn.microsoft.com/en-us/azure/ai-foundry/how-to/create-secure-ai-hub), or [via the Azure Machine Learning Python SDK](https://learn.microsoft.com/en-us/azure/ai-foundry/how-to/develop/create-hub-project-sdk?tabs=python), among other options listed in [Manage AI Hub Resources](https://learn.microsoft.com/en-us/azure/ai-foundry/concepts/ai-resources).

