在 Azure 中开始使用灵活流#
学习目标 - 完成本教程后,您应该能够
使用笔记本编写 LLM 应用程序并可视化应用程序的跟踪。
将应用程序转换为流并针对多行数据进行批处理运行。
0. 安装依赖包#
%%capture --no-stderr
%pip install -r ./requirements-azure.txt
1. 连接到工作区#
配置凭据#
我们使用 DefaultAzureCredential
访问工作区。DefaultAzureCredential
应该能够处理大多数 Azure SDK 身份验证场景。
如果此方法不适用于您,请参考其他凭据:配置凭据示例,azure-identity 参考文档。
from azure.identity import DefaultAzureCredential, InteractiveBrowserCredential
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 does not work
credential = InteractiveBrowserCredential()
连接到工作区#
我们使用配置文件连接到工作区。Azure ML 工作区应配置有计算集群。查看此笔记本以了解如何配置工作区
from promptflow.azure import PFClient
# Connect to the workspace
pf = PFClient.from_config(credential=credential)
创建必要的连接#
连接有助于安全地存储和管理与 LLM 和其他外部工具(例如 Azure 内容安全)交互所需的密钥或其他敏感凭据。
在本笔记本中,我们将使用 basic
和 eval-code-quality
灵活流,它们使用连接 open_ai_connection
。如果之前没有添加过,我们需要设置连接。
要准备您的 Azure OpenAI 资源,请按照这些说明操作,如果您没有 api_key
,请获取它。
转到工作区门户,单击 Prompt flow
-> Connections
-> Create
,然后按照说明创建您自己的连接。了解更多关于连接的信息。
2. 使用多行数据以流的形式批处理运行函数。#
创建一个 flow.flex.yaml
文件来定义一个流,其入口点指向我们定义的 Python 函数。
# Show the flow.flex.yaml content
with open("flow.flex.yaml") as fin:
print(fin.read())
使用数据文件(包含多行测试数据)进行批量运行#
flow = "." # Path to the flow directory
data = "./data.jsonl" # Path to the data file
# Create a run with the flow and data
base_run = pf.run(
flow=flow,
data=data,
column_mapping={
"text": "${data.text}",
},
environment_variables={
"AZURE_OPENAI_API_KEY": "${open_ai_connection.api_key}",
"AZURE_OPENAI_ENDPOINT": "${open_ai_connection.api_base}",
},
stream=True,
)
details = pf.get_details(base_run)
details.head(10)
3. 评估您的流#
然后您可以使用评估方法来评估您的流。评估方法也是流,通常使用 LLM 来验证生成的输出是否与预期输出匹配。
设置带连接的模型配置#
在 Azure 中使用 Promptflow 时,使用连接名称创建模型配置对象。在运行流时,模型配置将连接到云托管的 Promptflow 实例。
from promptflow.core import AzureOpenAIModelConfiguration
model_config = AzureOpenAIModelConfiguration(
connection="open_ai_connection",
azure_deployment="gpt-4o",
)
评估之前的批处理运行#
base_run 是我们在上面第 2 步中完成的批处理运行,用于使用“data.jsonl”作为输入的网络分类流。评估将获取该 base_run 的输出,并使用 LLM 将它们与您期望的输出进行比较,然后可视化结果。
eval_flow = "../eval-code-quality/flow.flex.yaml"
eval_run = pf.run(
flow=eval_flow,
init={"model_config": model_config},
data="./data.jsonl", # path to the data file
run=base_run, # specify the base_run as the run you want to evaluate
column_mapping={
"code": "${run.outputs.output}",
},
stream=True,
)
details = pf.get_details(eval_run)
details.head(10)
import json
metrics = pf.get_metrics(eval_run)
print(json.dumps(metrics, indent=4))
pf.visualize([base_run, eval_run])
后续步骤#
您已成功运行您的第一个灵活流并对其进行了评估。太棒了!
您可以查看更多示例
基本聊天:演示如何创建一个聊天机器人,它可以记住以前的交互并使用对话历史记录生成下一条消息。