<jupyter_start><jupyter_text>Using MLflow REST with Azure MLThe MLflow REST API allows you to create, list, and get experiments and runs, and log parameters, metrics, and artifacts. The API is hosted under the `/api/2.0/mlflow` route on for Azure ML MLflow tracking URL.<jupyter_code>import requests<jupyter_output><empty_output><jupyter_text>Getting an authorization tokenAs with any Azure service, you need an authentication token to access the MLflow REST API. The following example shows how to get an authentication token using a service principal. You will need the tenant ID, client ID and client secret associated with the service principal. To create a service principal you can follow the steps at [Use the portal to create an Azure AD application and service principal that can access resources](https://docs.microsoft.com/en-us/azure/active-directory/develop/howto-create-service-principal-portal).<jupyter_code>tenant_id = "<>"
client_id = "<>"
client_secret = "<>"
grant_type = "client_credentials"
resource_scope = "https://management.azure.com"<jupyter_output><empty_output><jupyter_text>The request URL is as follows:<jupyter_code>mso_url = f"https://login.microsoftonline.com/{tenant_id}/oauth2/token?"<jupyter_output><empty_output><jupyter_text>The service principal information is included inside the body:<jupyter_code>mso_body = f"grant_type={grant_type}&client_id={client_id}&client_secret={client_secret}&resource={resource_scope}"<jupyter_output><empty_output><jupyter_text>Making the request and getting the token back:<jupyter_code>auth_response = requests.post(url=mso_url, data=mso_body)
token_data = auth_response.json()<jupyter_output><empty_output><jupyter_text>> Token is returned in the field `access_token`.  Querying the MLflow APIYou can get the MLflow API from the Azure portal, or you can construct it manually as follows:<jupyter_code>subscription_id = "<>"
resource_group = "<>"
workspace_name = "<>"
location = "eastus"
azure_ml_mlflow_base = f"https://{location}.api.azureml.ms/mlflow/v1.0/subscriptions/{subscription_id}/resourceGroups/{resource_group}/providers/Microsoft.MachineLearningServices/workspaces/{workspace_name}"
mlflow_api_path = "api/2.0/mlflow"<jupyter_output><empty_output><jupyter_text>> You can get this URL following this steps: Navigate to Azure ML Studio and select the workspace you are working on > Click on the name of the workspace at the upper right corner of the page. > Click “View all properties in Azure Portal” on the pane popup. > Copy the MLflow tracking URI value from the properties section. Then replace the protocol from `azureml` to `https`. Construct the request headers including the authentication token you just got:<jupyter_code>headers = {
    "Content-Type": "application/json",
    "Authorization": f"{token_data['token_type']} {token_data['access_token']}",
}<jupyter_output><empty_output><jupyter_text>Construct the request URL as indicated at https://mlflow.org/docs/latest/rest-api.html. For example, the following URL list all the experiments in the workspace:<jupyter_code>response = requests.get(
    f"{azure_ml_mlflow_base}/{mlflow_api_path}/experiments/list", headers=headers
)<jupyter_output><empty_output><jupyter_text>In the following example, we are retrieving the experiments and parsing the results into a Pandas DataFrame for visualization:<jupyter_code>data = response.json()
import pandas as pd
pd.DataFrame(data["experiments"])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09486735870818916, 'fraction_numerical': 0.002018454440599769, 'mean_word_length': 6.993087557603687, 'pattern_counts': {'":': 2, '<': 43, '<?xml version=': 0, '>': 48, 'https://': 5, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '383081', 'license': 'MIT', 'n_tokens_mistral': 1025, 'n_tokens_neox': 1007, 'n_words': 313, 'path': 'notebooks/using-mlflow/using-rest-api/using_mlflow_rest_api.ipynb', 'repo_name': 'jplummer01/azureml-examples', 'size': '3468'}