DAG 流入门#

作者:  头像在 GitHub 上打开

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

  • Prompt Flow 存储库的本地克隆

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

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

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

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

  • 运行你的第一个 Prompt Flow 示例

  • 运行你的第一次评估

本教程中使用的示例是web-classification流,它将 URL 分类到几个预定义的类别中。分类是传统的机器学习任务,此示例说明了如何使用 GPT 和提示执行分类。

0. 安装依赖包#

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

1. 创建必要的连接#

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

在本笔记本中,我们将使用web-classification流,其中使用了连接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. 运行 web-classification 流#

web-classification是一个演示 LLM 多分类的流。给定一个 URL,它将通过少量示例、简单的摘要和分类提示将 URL 分类到其中一个网页类别中。

设置流路径#

flow = "../../flows/standard/web-classification"  # path to the flow directory

快速测试#

# Test flow
flow_inputs = {
    "url": "https://play.google.com/store/apps/details?id=com.twitter.android",
}
flow_result = pf.test(flow=flow, inputs=flow_inputs)
print(f"Flow result: {flow_result}")
# Test single node in the flow
node_name = "fetch_text_content_from_url"
node_inputs = {
    "url": "https://play.google.com/store/apps/details?id=com.twitter.android"
}
flow_result = pf.test(flow=flow, inputs=node_inputs, node=node_name)
print(f"Node result: {flow_result}")

流作为函数#

我们还实现了一个语法糖,你可以像 Python 函数一样使用流,并能够覆盖连接、输入和其他运行时配置。有关更多详细信息,请参阅此处

from promptflow.client import load_flow

flow_func = load_flow(flow)
flow_result = flow_func(**flow_inputs)
print(f"Flow function result: {flow_result}")

使用数据文件(包含多行测试数据)进行批量运行#

data = "../../flows/standard/web-classification/data.jsonl"  # path to the data file

# create run with default variant
base_run = pf.run(
    flow=flow,
    data=data,
    stream=True,
    column_mapping={
        "url": "${data.url}",
    },
)
details = pf.get_details(base_run)
details.head(10)

3. 评估您的流#

然后,你可以使用评估方法来评估你的流。评估方法也是流,它们使用 Python 或 LLM 等来计算准确性、相关性分数等指标。

在本笔记本中,我们使用classification-accuracy-eval流进行评估。这是一个说明如何评估分类系统性能的流。它涉及将每个预测与真实值进行比较并分配“正确”或“不正确”等级,并汇总结果以生成准确性等指标,这反映了系统在分类数据方面的表现。

对之前的批量运行进行评估#

base_run 是我们在上面第 2 步中完成的批量运行,用于 web-classification 流,输入为“data.jsonl”。

eval_flow = "../../flows/evaluation/eval-classification-accuracy"

eval_run = pf.run(
    flow=eval_flow,
    data="../../flows/standard/web-classification/data.jsonl",  # path to the data file
    run=base_run,  # specify base_run as the run you want to evaluate
    column_mapping={
        "groundtruth": "${data.answer}",
        "prediction": "${run.outputs.category}",
    },  # map the url field from the data to the url input of the flow
    stream=True,
)
details = pf.get_details(eval_run)
details.head(10)
metrics = pf.get_metrics(eval_run)
print(json.dumps(metrics, indent=4))
pf.visualize([base_run, eval_run])

现在您已经成功运行了您的第一个提示流并对其进行了评估。太棒了!

你可以查看web-classification流和classification-accuracy流以获取更多详细信息,并开始构建自己的流。

或者你可以继续学习更高级的主题:尝试变体。

另一次带有变体的批量运行#

Prompt Flow 中的变体允许你使用 LLM 进行实验。你可以设置指向不同提示的 Prompt/LLM 节点的变体,或使用不同的 LLM 参数,例如温度。

在此示例中,web-classification的节点summarize_text_content有两个变体:variant_0variant_1。它们之间的区别在于输入参数。

变体 0

- inputs:
    - deployment_name: gpt-35-turbo
    - max_tokens: '128'
    - temperature: '0.2'
    - text: ${fetch_text_content_from_url.output}

变体 1

- inputs:
    - deployment_name: gpt-35-turbo
    - max_tokens: '256'
    - temperature: '0.3'
    - text: ${fetch_text_content_from_url.output}

你可以在flow.dag.yaml处查看完整的流定义

# use the variant1 of the summarize_text_content node.
variant_run = pf.run(
    flow=flow,
    data=data,
    variant="${summarize_text_content.variant_1}",  # here we specify node "summarize_text_content" to use variant 1 version.
    column_mapping={
        "url": "${data.url}",
    },
    stream=True,
)
details = pf.get_details(variant_run)
details.head(10)

对变体运行进行评估#

这样以后我们就可以比较指标,看看哪个效果更好。

eval_flow = "../../flows/evaluation/eval-classification-accuracy"

eval_run_variant = pf.run(
    flow=eval_flow,
    data="../../flows/standard/web-classification/data.jsonl",  # path to the data file
    run=variant_run,  # use run as the variant
    column_mapping={
        "groundtruth": "${data.answer}",
        "prediction": "${run.outputs.category}",
    },  # map the url field from the data to the url input of the flow
    stream=True,
)
details = pf.get_details(eval_run_variant)
details.head(10)
metrics = pf.get_metrics(eval_run_variant)
print(json.dumps(metrics, indent=4))
pf.visualize([eval_run, eval_run_variant])

后续步骤#

了解更多关于

  • 管理连接:如何管理用于访问外部服务(包括 LLM)的端点/机密信息。

  • 与 PDF 聊天:通过端到端教程了解如何使用 Prompt Flow 开发聊天应用程序。

  • 部署 http 端点:如何将流部署为本地 http 端点。

  • Azure AI 中的 Prompt Flow:在 Azure AI 中运行和评估流,你可以在其中更好地与团队协作。