Prompty 入门#

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

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

  • 使用 Prompty 编写 LLM 应用程序并可视化应用程序的跟踪。

  • 对多行数据进行 Prompty 批处理运行。

0. 安装依赖包#

%%capture --no-stderr
%pip install promptflow-core

1. 执行 Prompty#

Prompty 是一个扩展名为 .prompty 的文件,用于开发提示模板。Prompty 资产是一个带有修改后的前置内容的 markdown 文件。前置内容采用 yaml 格式,包含许多元数据字段,这些字段定义了模型配置和 Prompty 的预期输入。

with open("basic.prompty") as fin:
    print(fin.read())

注意:在运行以下单元格之前,请通过创建一个 .env 文件来配置所需的环境变量 AZURE_OPENAI_API_KEYAZURE_OPENAI_ENDPOINT。请参考 ../.env.example 作为模板。

import os
from dotenv import load_dotenv

if "AZURE_OPENAI_API_KEY" not in os.environ:
    # load environment variables from .env file
    load_dotenv()
from promptflow.core import Prompty

# load prompty as a flow
f = Prompty.load(source="basic.prompty")

# execute the flow as function
result = f(question="What is the capital of France?")
result

您可以使用 AzureOpenAIModelConfigurationOpenAIModelConfiguration 覆盖配置。

from promptflow.core import AzureOpenAIModelConfiguration, OpenAIModelConfiguration

# override configuration with AzureOpenAIModelConfiguration
configuration = AzureOpenAIModelConfiguration(
    # azure_endpoint="${env:AZURE_OPENAI_ENDPOINT}",  # Use ${env:<ENV_NAME>} to surround the environment variable name.
    # api_key="${env:AZURE_OPENAI_API_KEY}",
    azure_deployment="gpt-4o",
)

# override configuration with OpenAIModelConfiguration
# configuration = OpenAIModelConfiguration(
#     base_url="${env:OPENAI_BASE_URL}",
#     api_key="${env:OPENAI_API_KEY}",
#     model="gpt-3.5-turbo"
# )

override_model = {"configuration": configuration, "parameters": {"max_tokens": 512}}

# load prompty as a flow
f = Prompty.load(source="basic.prompty", model=override_model)

# execute the flow as function
result = f(question="What is the capital of France?")
result

使用 start_trace 可视化追踪#

from promptflow.tracing import start_trace

# start a trace session, and print a url for user to check trace
start_trace()

重新运行以下单元格将在跟踪 UI 中收集跟踪。

# rerun the function, which will be recorded in the trace
question = "What is the capital of Japan?"
ground_truth = "Tokyo"
result = f(question=question)
result

评估结果#

注意:eval 流返回一个 json_object

# load prompty as a flow
eval_flow = Prompty.load("../eval-basic/eval.prompty")
# execute the flow as function
result = eval_flow(question=question, ground_truth=ground_truth, answer=result)
result

2. 使用多行数据进行批处理运行#

%%capture --no-stderr
# batch run requires promptflow-devkit package
%pip install promptflow-devkit
from promptflow.client import PFClient

pf = PFClient()
flow = "./basic.prompty"  # path to the prompty file
data = "./data.jsonl"  # path to the data file

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

3. 评估您的流#

然后您可以使用评估方法来评估您的流。评估方法也是流,通常使用 LLM 断言生成的输出符合某些预期。

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

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

eval_prompty = "../eval-basic/eval.prompty"

eval_run = pf.run(
    flow=eval_prompty,
    data="./data.jsonl",  # path to the data file
    run=base_run,  # specify base_run as the run you want to evaluate
    column_mapping={
        "question": "${data.question}",
        "answer": "${run.outputs.output}",  # TODO refine this mapping
        "ground_truth": "${data.ground_truth}",
    },
    stream=True,
)
details = pf.get_details(eval_run)
details.head(10)
# visualize run using ui
pf.visualize([base_run, eval_run])

后续步骤#

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

您可以查看更多示例

  • 基本聊天:演示如何创建一个聊天机器人,该机器人可以记住以前的交互并使用对话历史来生成下一条消息。