流运行管理#

作者:  头像 头像在 GitHub 上打开

先决条件 - 为了充分利用本教程,您需要具备:

  • Prompt flow 仓库的本地克隆

  • 支持 Jupyter Notebook 的 Python 环境(例如 Jupyter Lab 或 Visual Studio Code 的 Python 扩展)

  • 了解如何使用 Python 编程 :)

对机器学习有基本的了解会很有帮助,但不是强制性的。

学习目标 - 完成本教程后,您应该能够:

  • 通过 run.yaml 管理运行

  • 创建引用另一个运行输入的运行

  • 创建带连接覆盖的运行

动机 - 本指南将引导您了解本地运行管理功能。

0. 安装依赖包#

%pip install -r ../../requirements.txt

1. 创建必要的连接#

连接有助于安全地存储和管理与 LLM 和其他外部工具(例如 Azure 内容安全)交互所需的密钥或其他敏感凭据。

本笔记本将使用内部连接 open_ai_connection,如果之前没有添加,我们需要设置连接。创建后,它存储在本地数据库中,可以在任何流中使用。

遵循此说明准备您的 Azure OpenAI 资源,如果您没有 api_key,请获取一个。

import json
from promptflow.client import PFClient
from promptflow.connections import AzureOpenAIConnection, OpenAIConnection

# client can help manage your runs and connections.
pf = PFClient()
try:
    conn_name = "open_ai_connection"
    conn = pf.connections.get(name=conn_name)
    print("using existing connection")
except:
    # Follow https://learn.microsoft.com/en-us/azure/ai-services/openai/how-to/create-resource?pivots=web-portal to create an Azure OpenAI resource.
    connection = AzureOpenAIConnection(
        name=conn_name,
        api_key="<test_key>",
        api_base="<test_base>",
        api_type="azure",
        api_version="<test_version>",
    )

    # use this if you have an existing OpenAI account
    # connection = OpenAIConnection(
    #     name=conn_name,
    #     api_key="<user-input>",
    # )

    conn = pf.connections.create_or_update(connection)
    print("successfully created connection")

print(conn)

2. 使用 YAML 文件创建运行#

您可以将运行的配置保存在 YAML 文件中,以节省在 SDK/CLI 中重复提供它们的精力。在此步骤中,我们将使用 YAML 文件创建一个示例运行。

from promptflow.client import load_run

# load a run from YAML file
base_run = load_run(
    source="../../flows/standard/web-classification/run.yml",
    # override the default params in the YAML file
    params_override=[{"column_mapping": {"url": "${data.url}"}}],
)

# create the run
base_run = pf.runs.create_or_update(run=base_run)
details = pf.get_details(base_run)
details.head(10)

3 创建一个使用现有运行输入的流运行#

使用现有运行运行流时,您可以在列映射中引用其输入或输出。以下代码单元格展示了如何在列映射中引用运行的输入。

from promptflow.entities import Run

# directly create the run object
run = Run(
    # local flow file
    flow="../../flows/standard/web-classification",
    # run name
    run=base_run,
    column_mapping={
        # reference another run's inputs data column
        "url": "${run.inputs.url}",
    },
)

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

pf.runs.stream(base_run)

4. 创建带连接覆盖的流运行#

有时您想在提交流时切换流内的连接或部署名称。连接覆盖提供了一种简单的方法,无需更改原始 flow.dag.yaml 即可实现。在以下代码单元格中,我们将提交流 web-classification 并将其连接覆盖为 open_ai_connection。请确保您的本地环境中存在连接 open_ai_connection

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
    # you can replace connection to your local connections
    connections={
        "classify_with_llm": {"connection": "open_ai_connection"},
        "summarize_text_content": {"connection": "open_ai_connection"},
    },
)

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

pf.runs.stream(base_run)