Updated on 2024-04-30 GMT+08:00

Examples

There are three scenarios:

  • Deploying a real-time service
  • Modifying a real-time service
  • Getting the inference address from the service deployment phase

Deploying a Real-Time Service

import modelarts.workflow as wf
# Use ServiceStep to define a service deployment phase and specify a model for service deployment.

# Define model name parameters.
model_name = wf.Placeholder(name="placeholder_name", placeholder_type=wf.PlaceholderType.STR)

service_step = wf.steps.ServiceStep(
    name="service_step", # Name of the service deployment phase. The name contains a maximum of 64 characters, including only letters, digits, underscores (_), and hyphens (-). It must start with a letter and must be unique in a workflow.
    title="Deploying a service",  # Title
    inputs=wf.steps.ServiceInput(name="si_service_ph", data=wf.data.ServiceInputPlaceholder(name="si_placeholder1", 
                                                                                  # Restrictions on the model name: Only the model name specified here can be used in the running state; use the same model name as model_name of the model registration phase.
                                                                                  model_name=model_name)),# ServiceStep inputs
    outputs=wf.steps.ServiceOutput(name="service_output") # ServiceStep outputs
)

workflow = wf.Workflow(
    name="service-step-demo",
    desc="this is a demo workflow",
    steps=[service_step]
)

Modifying a Real-Time Service

Scenario: When you use a new model version to update an existing service, ensure that the name of the new model version is the same as that of the deployed service.

import modelarts.workflow as wf
# Use ServiceStep to define a service deployment phase and specify a model for service update.

# Define model name parameters.
model_name = wf.Placeholder(name="placeholder_name", placeholder_type=wf.PlaceholderType.STR)

# Define a service object.
service = wf.data.ServiceUpdatePlaceholder(name="placeholder_name")

service_step  = wf.steps.ServiceStep(
    name="service_step", # Name of the service deployment phase. The name contains a maximum of 64 characters, including only letters, digits, underscores (_), and hyphens (-). It must start with a letter and must be unique in a workflow.
    title="Service update", # Title
    inputs=[wf.steps.ServiceInput(name="si2", data=wf.data.ServiceInputPlaceholder(name="si_placeholder2", 
                                                                                  # Restrictions on the model name: Only the model name specified here can be used in the running state.
                                                                                  model_name=model_name)),
           wf.steps.ServiceInput(name="si_service_data", data=service) # ServiceStep inputs are configured when the workflow is running. You can also use wf.data.ServiceData(service_id="fake_service") for the data field.
    ], # ServiceStep inputs
    outputs=wf.steps.ServiceOutput(name="service_output") # ServiceStep outputs
)

workflow = wf.Workflow(
    name="service-step-demo",
    desc="this is a demo workflow",
    steps=[service_step]
)

Getting the Inference Address From the Service Deployment Phase

The service deployment phase supports the output of the inference address. You can use the get_output_variable("access_address") method to obtain the output and use it in subsequent phases.

  • For services deployed in the public resource pool, you can use access_address to obtain the inference address registered on the public network from the output.
  • For services deployed in a dedicated resource pool, you can get the internal inference address from the output using cluster_inner_access_address, in addition to the public inference address. The internal address can only be accessed by other inference services.
    import modelarts.workflow as wf
    
    # Define model name parameters.
    sub_model_name = wf.Placeholder(name="si_placeholder1", placeholder_type=wf.PlaceholderType.STR)
    
    sub_service_step = wf.steps.ServiceStep(
        name="sub_service_step", # Name of the service deployment phase. The name contains a maximum of 64 characters, including only letters, digits, underscores (_), and hyphens (-). It must start with a letter and must be unique in a workflow.
        title="Subservice",  # Title
        inputs=wf.steps.ServiceInput(
            name="si_service_ph",
            data=wf.data.ServiceInputPlaceholder(name="si_placeholder1", model_name=sub_model_name)
        ),# ServiceStep inputs
        outputs=wf.steps.ServiceOutput(name="service_output") # ServiceStep outputs
    )
    
    
    main_model_name = wf.Placeholder(name="si_placeholder2", placeholder_type=wf.PlaceholderType.STR)
    
    # Obtain the inference address output by the subservice and transfer the address to the main service through envs.
    main_service_config = wf.steps.ServiceConfig(
        infer_type="real-time", 
        envs={"infer_address": sub_service_step.outputs["service_output"].get_output_variable("access_address")} # Obtain the inference address output by the subservice and transfer the address to the main service through envs.
    )
    
    main_service_step = wf.steps.ServiceStep(
        name="main_service_step", # Name of the service deployment phase. The name contains a maximum of 64 characters, including only letters, digits, underscores (_), and hyphens (-). It must start with a letter and must be unique in a workflow.
        title="Main service",  # Title
        inputs=wf.steps.ServiceInput(
            name="si_service_ph",
            data=wf.data.ServiceInputPlaceholder(name="si_placeholder2", model_name=main_model_name)
        ),# ServiceStep inputs
        outputs=wf.steps.ServiceOutput(name="service_output", service_config=main_service_config), # ServiceStep outputs
        depend_steps=sub_service_step
    )
    
    workflow = wf.Workflow(
        name="service-step-demo",
        desc="this is a demo workflow",
        steps=[sub_service_step, main_service_step]
    )