Azure Machine Learning Component SDK

0.9.18.post2 · active · verified Thu Apr 16

The `azure-ml-component` library is a low-level Python SDK providing core functionalities and base classes for defining and managing components within Azure Machine Learning. It is primarily an internal dependency of the higher-level `azure-ai-ml` SDK, which is the recommended package for most end-user interactions. This package is actively maintained with frequent updates, currently at version 0.9.18.post2, usually released in sync with the `azure-ai-ml` ecosystem.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates defining a basic command component using the `azure.ai.ml` SDK, which is the recommended way to interact with Azure Machine Learning components. Although `azure-ml-component` provides underlying classes, direct usage is uncommon. This example defines a component that takes a string input and writes a processed string to an output file. To actually register and run this component, an `MLClient` connected to an Azure ML Workspace would be required (commented out for quickstart runnability).

import os
from azure.ai.ml import command, Input, Output
from azure.ai.ml.entities import CommandComponent

# Define a simple command component using the recommended azure.ai.ml SDK
# This component takes an input string and produces an output string.

# For this example, we'll define a simple Python script as the command source
component_script_content = """
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--input_data", type=str)
parser.add_argument("--output_data", type=str)
args = parser.parse_args()

print(f"Received input: {args.input_data}")

with open(args.output_data, "w") as f:
    f.write(f"Processed: {args.input_data.upper()}")
print(f"Wrote output to {args.output_data}")
"""

# Create a temporary directory and save the script
import tempfile
import shutil

with tempfile.TemporaryDirectory() as temp_dir:
    script_path = os.path.join(temp_dir, "process.py")
    with open(script_path, "w") as f:
        f.write(component_script_content)

    # Define the command component
    hello_world_component = command(
        name="hello_world_component",
        display_name="Hello World Component Example",
        description="A simple component that processes input text.",
        inputs={
            "input_data": Input(type="string", description="Input string for processing")
        },
        outputs={
            "output_data": Output(type="uri_file", description="Processed output string")
        },
        command=f"python {{inputs.script_path}}/process.py --input_data {{inputs.input_data}} --output_data {{outputs.output_data}}",
        environment=dict(
            conda_file=os.path.join(temp_dir, "conda_env.yml"),
            image="mcr.microsoft.com/azureml/openmpi4.1.0-ubuntu20.04:latest"
        ),
        code=temp_dir
    )

    print("\n--- Component Definition ---")
    print(hello_world_component.as_dict())

    # To actually run this, you would need an MLClient connected to an Azure ML workspace.
    # from azure.ai.ml import MLClient
    # from azure.identity import DefaultAzureCredential
    # ml_client = MLClient(
    #     DefaultAzureCredential(), 
    #     subscription_id=os.environ.get("AZURE_SUBSCRIPTION_ID", ""),
    #     resource_group_name=os.environ.get("AZURE_RESOURCE_GROUP", ""),
    #     workspace_name=os.environ.get("AZURE_WORKSPACE_NAME", "")
    # )
    # registered_component = ml_client.components.create_or_update(hello_world_component)
    # print(f"Component registered with name: {registered_component.name}, version: {registered_component.version}")

view raw JSON →