'How to get Model ID of the Latest Version registered in Azure Machine Learning Service Model Registry using az ml cli?

Using Azure Machine Learning CLI extension, how do we get the Model ID for the latest version of a Model (with known model name)?

To get the entire list of Model Details with a given name the command is

az ml model list --model-name [Model_Name] --resource-group [RGP_NAME] --subscription-id [SUB_ID] --workspace-name [WS_NAME]

Running this will give a list of all the models:

[
  {
    "createdTime": "2021-03-19T07:02:03.814172+00:00",
    "framework": "Custom",
    "frameworkVersion": null,
    "id": "model:2"
    "name": "model",
    "version": 3
  },
  {
    "createdTime": "2021-03-19T06:46:34.301054+00:00",
    "framework": "Custom",
    "frameworkVersion": null,
    "id": "model:2",
    "name": "model",
    "version": 2
  },
  {
    "createdTime": "2021-03-19T06:38:56.558385+00:00",
    "framework": "Custom",
    "frameworkVersion": null,
    "id": "model:1",
    "name": "model",
    "version": 1
  }
]

The Microsoft Documentation mentions, we can use a -l parameter to get the latest version details:

az ml model list --model-name [Model_Name] --resource-group [RGP_NAME] --subscription-id [SUB_ID] --workspace-name [WS_NAME] -l

However, running this gives the following error:

ERROR: UnrecognizedArgumentError: unrecognized arguments: -l

What is the syntax to use this -l flag?



Solution 1:[1]

If we wish to obtain the model-id for the latest model, instead of using az ml model list with -l flag, using az model show will return the details for the latest model. The syntax to get a string for model-id will be:

az ml model show --model-id $(TRN_MODEL_ID) --resource-group $(AML_TRN_RG) --subscription-id $(AML_TRN_SUB_ID) --workspace-name $(AML_TRN_WS) --query name -o tsv

Solution 2:[2]

I've just spent a fun few hours wrestling with this. Running something like

az ml model list -w your_workspace_name -g your_resource_group_name -l -n name_of_your_registered_model

will get you back some JSON that looks like this:

[
  {
    "createdTime": "2022-03-04T16:05:47.103407+00:00",
    "framework": "Custom",
    "frameworkVersion": null,
    "id": "name_of_your_registered_model:3",
    "name": "name_of_your_registered_model",
    "version": 3
  }
]

This is well and good for a human, but not much use to a machine. The azure cli supports something called JMESPath, which allows you to write a query against the result of a CLI command. Running

az ml model list -w your_workspace_name -g your_resource_group_name -l -n name_of_your_registered_model --query "[0].{id:id}" -o tsv

should get you back

"name_of_your_registered_model:3"

Which you can then use in an env var or whatever other use case you have.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Anirban Saha
Solution 2 Tom Clelford