DAG流程入门#

作者:  Open on GitHubOpen on GitHub

前提条件 - 要充分利用本教程,您需要:

  • 提示流仓库的本地克隆

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

  • 知道如何使用Python编程 :)

对机器学习的基本理解可能是有益的,但不是强制性的。

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

  • 运行您的第一个提示流示例

  • 运行您的第一次评估

本教程中使用的示例是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 是一个展示使用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中完成的批量运行,用于以“data.jsonl”作为输入的web分类流程。

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流程以获取更多详细信息,并开始构建您自己的流程。

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

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

Variant 在提示流中是为了允许您对LLMs进行实验。您可以设置一个指向不同提示的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])

下一步#

了解更多:

  • Manage connections: 如何管理端点/密钥信息以访问包括LLMs在内的外部服务。

  • Chat with PDF: 通过一个端到端的教程,学习如何使用prompt flow开发一个聊天应用程序。

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

  • Prompt flow in Azure AI: 在Azure AI中运行和评估流程,您可以更好地与团队协作。