mcpField
A
azure-ai-ml-py

azure-ai-ml-py

github.com/sickn33云与 DevOps48.7k76 次安装今天更新
代码仓库

关于 azure-ai-ml-py

面向 Python 开发者的 Azure Machine Learning SDK v2 客户端库,用于管理工作区、作业、模型、数据和计算资源。它通过 MLClient 提供资源操作、作业监控和管道构建接口。

主要功能

  1. 01管理工作区与数据资产
  2. 02注册和查询模型
  3. 03创建及管理计算资源
  4. 04提交并监控作业
  5. 05构建多步骤管道
  6. 06管理环境与数据存储

适用场景

  1. 01列出或创建 Azure ML 工作区
  2. 02注册数据集和模型版本
  3. 03提交训练作业并查看日志
  4. 04编排多步骤机器学习管道

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/sickn33/agentic-awesome-skills --skill azure-ai-ml-py

安装后在 Claude Code 中输入/azure-ai-ml-py确认已加载。

详细介绍

用于 Python 的 Azure Machine Learning SDK v2

用于管理 Azure ML 资源的客户端库:工作区、作业、模型、数据和计算资源。

范围与执行

在创建或更新资源,以及提交计算任务或作业之前,确认订阅、租户、工作区、区域和预算。示例仅为片段:其中不包含训练组件、源文件和环境定义。请选择当前受支持且经过审核的 SDK 和环境版本;历史映像和 @latest 示例无法保证可复现性。使用最小权限身份,不要打印私有作业数据。

安装

pip install azure-ai-ml

环境变量

AZURE_SUBSCRIPTION_ID=<your-subscription-id>
AZURE_RESOURCE_GROUP=<your-resource-group>
AZURE_ML_WORKSPACE_NAME=<your-workspace-name>

身份验证

import os
from azure.ai.ml import MLClient
from azure.identity import DefaultAzureCredential

ml_client = MLClient(
    credential=DefaultAzureCredential(),
    subscription_id=os.environ["AZURE_SUBSCRIPTION_ID"],
    resource_group_name=os.environ["AZURE_RESOURCE_GROUP"],
    workspace_name=os.environ["AZURE_ML_WORKSPACE_NAME"]
)
从配置文件读取
import os
from azure.ai.ml import MLClient
from azure.identity import DefaultAzureCredential

# Uses config.json in current directory or parent
ml_client = MLClient.from_config(
    credential=DefaultAzureCredential()
)

工作区管理

创建工作区
from azure.ai.ml.entities import Workspace

ws = Workspace(
    name="my-workspace",
    location="eastus",
    display_name="My Workspace",
    description="ML workspace for experiments",
    tags={"purpose": "demo"}
)

ml_client.workspaces.begin_create(ws).result()
列出工作区
for ws in ml_client.workspaces.list():
    print(f"{ws.name}: {ws.location}")

数据资产

注册数据
from azure.ai.ml.entities import Data
from azure.ai.ml.constants import AssetTypes

# Register a file
my_data = Data(
    name="my-dataset",
    version="1",
    path="azureml://datastores/workspaceblobstore/paths/data/train.csv",
    type=AssetTypes.URI_FILE,
    description="Training data"
)

ml_client.data.create_or_update(my_data)
注册文件夹
my_data = Data(
    name="my-folder-dataset",
    version="1",
    path="azureml://datastores/workspaceblobstore/paths/data/",
    type=AssetTypes.URI_FOLDER
)

ml_client.data.create_or_update(my_data)

模型注册表

注册模型
from azure.ai.ml.entities import Model
from azure.ai.ml.constants import AssetTypes

model = Model(
    name="my-model",
    version="1",
    path="./model/",
    type=AssetTypes.CUSTOM_MODEL,
    description="My trained model"
)

ml_client.models.create_or_update(model)
列出模型
for model in ml_client.models.list(name="my-model"):
    print(f"{model.name} v{model.version}")

计算资源

创建计算群集
from azure.ai.ml.entities import AmlCompute

cluster = AmlCompute(
    name="cpu-cluster",
    type="amlcompute",
    size="Standard_DS3_v2",
    min_instances=0,
    max_instances=4,
    idle_time_before_scale_down=120
)

ml_client.compute.begin_create_or_update(cluster).result()
列出计算资源
for compute in ml_client.compute.list():
    print(f"{compute.name}: {compute.type}")

作业

命令作业
from azure.ai.ml import command, Input

job = command(
    code="./src",
    command="python train.py --data ${{inputs.data}} --lr ${{inputs.learning_rate}}",
    inputs={
        "data": Input(type="uri_folder", path="azureml:my-dataset:1"),
        "learning_rate": 0.01
    },
    environment="AzureML-sklearn-1.0-ubuntu20.04-py38-cpu@latest",
    compute="cpu-cluster",
    display_name="training-job"
)

returned_job = ml_client.jobs.create_or_update(job)
print(f"Job URL: {returned_job.studio_url}")
监控作业
ml_client.jobs.stream(returned_job.name)

管道

from azure.ai.ml import dsl, Input, Output

@dsl.pipeline(
    compute="cpu-cluster",
    description="Training pipeline"
)
def training_pipeline(data_input):
    prep_step = prep_component(data=data_input)
    train_step = train_component(
        data=prep_step.outputs.output_data,
        learning_rate=0.01
    )
    return {"model": train_step.outputs.model}

pipeline = training_pipeline(
    data_input=Input(type="uri_folder", path="azureml:my-dataset:1")
)

pipeline_job = ml_client.jobs.create_or_update(pipeline)

环境

创建自定义环境
from azure.ai.ml.entities import Environment

env = Environment(
    name="my-env",
    version="1",
    image="mcr.microsoft.com/azureml/openmpi4.1.0-ubuntu20.04",
    conda_file="./environment.yml"
)

ml_client.environments.create_or_update(env)

数据存储

列出数据存储
for ds in ml_client.datastores.list():
    print(f"{ds.name}: {ds.type}")
获取默认数据存储
default_ds = ml_client.datastores.get_default()
print(f"Default: {default_ds.name}")

MLClient 操作

属性操作
workspacescreate, get, list, delete
jobscreate_or_update, get, list, stream, cancel
modelscreate_or_update, get, list, archive
datacreate_or_update, get, list
computebegin_create_or_update, get, list, delete
environmentscreate_or_update, get, list
datastorescreate_or_update, get, list, get_default
componentscreate_or_update, get, list

最佳实践

  1. 使用版本控制管理数据、模型和环境
  2. 配置空闲时缩容以降低计算成本
  3. 使用环境确保训练可复现
  4. 流式传输作业日志以监控进度
  5. 在训练作业成功后注册模型
  6. 使用管道处理多步骤工作流
  7. 为资源添加标记,以便组织资源和跟踪成本

适用场景

当需要执行概述中描述的工作流或操作时,可使用此技能。

限制

  • 仅当任务明确符合上述范围时,才使用此技能。
  • 不要将输出视为环境特定验证、测试或专家审核的替代品。
  • 如果缺少必需的输入、权限、安全边界或成功标准,请暂停并请求澄清。

评论

支持 Markdown