流程运行管理#
前提条件 - 要充分利用本教程,您需要:
提示流仓库的本地克隆
一个支持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)