<jupyter_start><jupyter_text>Flow Run Management in Azure**Requirements** - In order to benefit from this tutorial, you will need:- An Azure account with an active subscription - [Create an account for free](https://azure.microsoft.com/free/?WT.mc_id=A261C142F)- An Azure ML workspace - [Configure workspace](../../configuration.ipynb)- A python environment- Installed prompt flow SDK**Learning Objectives** - By the end of this tutorial, you should be able to:- create run with remote data- create run which references another runs inputs- manage runs via run.yaml- create run with connection override- create run with custom runtime**Motivations** - This guide will walk you through cloud run management abilities.  0. Install dependent packages<jupyter_code>%pip install -r ../../requirements.txt<jupyter_output><empty_output><jupyter_text>1. Connect to Azure Machine Learning WorkspaceThe [workspace](https://docs.microsoft.com/en-us/azure/machine-learning/concept-workspace) is the top-level resource for Azure Machine Learning, providing a centralized place to work with all the artifacts you create when you use Azure Machine Learning. In this section we will connect to the workspace in which the job will be run. 1.1 Import the required libraries<jupyter_code>from azure.identity import DefaultAzureCredential, InteractiveBrowserCredential
from azure.ai.ml.entities import Data
from azure.core.exceptions import ResourceNotFoundError

from promptflow.azure import PFClient
from promptflow.entities import Run<jupyter_output><empty_output><jupyter_text>1.2 Configure credentialWe are using `DefaultAzureCredential` to get access to workspace. `DefaultAzureCredential` should be capable of handling most Azure SDK authentication scenarios. Reference for more available credentials if it does not work for you: [configure credential example](../../configuration.ipynb), [azure-identity reference doc](https://docs.microsoft.com/en-us/python/api/azure-identity/azure.identity?view=azure-python).<jupyter_code>try:
    credential = DefaultAzureCredential()
    # Check if given credential can get token successfully.
    credential.get_token("https://management.azure.com/.default")
except Exception as ex:
    # Fall back to InteractiveBrowserCredential in case DefaultAzureCredential not work
    credential = InteractiveBrowserCredential()<jupyter_output><empty_output><jupyter_text>1.3 Get a handle to the workspaceWe use config file to connect to a workspace. The Azure ML workspace should be configured with computer cluster. [Check this notebook for configure a workspace](../../configuration.ipynb)<jupyter_code># Get a handle to workspace
pf = PFClient.from_config(credential=credential)<jupyter_output><empty_output><jupyter_text>1.4 Create necessary connectionsConnection helps securely store and manage secret keys or other sensitive credentials required for interacting with LLM and other external tools for example Azure Content Safety.In this notebook, we will use flow `web-classification` which uses connection `open_ai_connection` inside, we need to set up the connection if we haven't added it before.Prepare your Azure OpenAI resource follow this [instruction](https://learn.microsoft.com/en-us/azure/cognitive-services/openai/how-to/create-resource?pivots=web-portal) and get your `api_key` if you don't have one.Please go to [workspace portal](https://ml.azure.com/), click `Prompt flow` -> `Connections` -> `Create`, then follow the instruction to create your own connections. Learn more on [connections](https://learn.microsoft.com/en-us/azure/machine-learning/prompt-flow/concept-connections?view=azureml-api-2).  2. Create run with remote dataInstead of relying on local files, there may be situations where you want to reuse data that's already available in your workspace when submitting a flow.The following code cells show how to create flow run with remote data. 2.1 Create or update remote data<jupyter_code>data_name, data_version = "flow_run_test_data", "1"

try:
    data = pf.ml_client.data.get(name=data_name, version=data_version)
except ResourceNotFoundError:
    data = Data(
        name=data_name,
        version=data_version,
        path=f"../../flows/standard/web-classification/data.jsonl",
        type="uri_file",
    )
    data = pf.ml_client.data.create_or_update(data)<jupyter_output><empty_output><jupyter_text>2.2 Prepare remote data id<jupyter_code>data_id = f"azureml:{data.name}:{data.version}"
print(data_id)<jupyter_output><empty_output><jupyter_text>2.3 Create a flow run with remote dataYou can change instance type or idle time of the runtime or reset it to clean state. The following code cell shows how to do so.<jupyter_code># create run
run = Run(
    # local flow file
    flow="../../flows/standard/web-classification",
    # remote data
    data=data_id,
    # to customize runtime instance type and idle time, you can provide them in resources
    # resources={
    #     "instance_type": "STANDARD_DS11_V2",
    #     "idle_time_before_shutdown_minutes": 10
    # }
)

base_run = pf.runs.create_or_update(
    run=run,
    # to reset automatic runtime to clean state, set reset_runtime to True
    # reset_runtime=True,
)<jupyter_output><empty_output><jupyter_text>2.4 Stream the flow run to make sure it runs successfully<jupyter_code>pf.runs.stream(base_run)<jupyter_output><empty_output><jupyter_text>3 Create a flow run which uses an existing run's inputsWhen running a flow with an existing run, you can reference either it's inputs or outputs in column mapping.The following code cell show how to reference a run's inputs in column mapping.<jupyter_code>run = Run(
    # local flow file
    flow="../../flows/standard/web-classification",
    # run name
    run=run,
    column_mapping={
        # reference another run's input data columns
        "url": "${run.inputs.url}",
        "answer": "${run.inputs.answer}",
        "evidence": "${run.inputs.evidence}",
    },
)

base_run = pf.runs.create_or_update(
    run=run,
)

pf.runs.stream(base_run)<jupyter_output><empty_output><jupyter_text>4. Create a flow run with connection overrideSometime you want to switch connection or deployment name inside a flow when submitting it.Connection override provided an easy way to do it without changing original `flow.dag.yaml`.In the following code cell, we will submit flow `web-classification` and override it's connection `open_ai_connection` to `azure_open_ai_connection`. Please make sure the connection `azure_open_ai_connection` exists in your workspace.<jupyter_code>run = Run(
    # local flow file
    flow="../../flows/standard/web-classification",
    data="../../flows/standard/web-classification/data.jsonl",
    # override connection for node classify_with_llm & summarize_text_content
    connections={
        "classify_with_llm": {"connection": "azure_open_ai_connection"},
        "summarize_text_content": {"connection": "azure_open_ai_connection"},
    },
)

base_run = pf.runs.create_or_update(
    run=run,
)

pf.runs.stream(base_run)<jupyter_output><empty_output><jupyter_text>4 Create a flow run which uses custom runtimePrompt flow’s runtime provides the computing resources required for the application to run. You can follow this [instruction](https://learn.microsoft.com/en-us/azure/machine-learning/prompt-flow/how-to-create-manage-runtime?view=azureml-api-2) to create a runtime in workspace then use it to submit flow runs.The following code cell shows how to use your custom runtime.<jupyter_code># create run
run = Run(
    # local flow file
    flow="../../flows/standard/web-classification",
    # remote data
    data=data_id,
)

# comment below create operation as it requires a runtime with specific name
# which will break CI pipeline
# base_run = pf.runs.create_or_update(
#     run=run,
#     runtime = "<runtime-name>"  # TODO replace with your runtime name
# )<jupyter_output><empty_output>