开始使用弹性流#
学习目标 - 完成本教程后,您应该能够:
使用笔记本编写LLM应用程序并可视化应用程序的跟踪。
将应用程序转换为流程,并针对多行数据进行批处理运行。
0. 安装依赖包#
%%capture --no-stderr
%pip install -r ./requirements.txt
1. 使用promptflow跟踪您的应用程序#
假设我们已经有一个调用OpenAI API的python函数。
with open("llm.py") as fin:
print(fin.read())
注意:在运行下面的单元格之前,请通过创建一个.env文件来配置所需的环境变量AZURE_OPENAI_API_KEY和AZURE_OPENAI_ENDPOINT。请参考../.env.example作为模板。
# control the AOAI deployment (model) used in this example
deployment_name = "gpt-4o"
from llm import my_llm_tool
# pls configure `AZURE_OPENAI_API_KEY`, `AZURE_OPENAI_ENDPOINT` environment variables first
result = my_llm_tool(
prompt="Write a simple Hello, world! program that displays the greeting message when executed. Output code only.",
deployment_name=deployment_name,
)
result
使用start_trace可视化跟踪#
注意我们在my_llm_tool函数中添加了@trace,重新运行下面的单元格将在trace UI中收集一个跟踪。
from promptflow.tracing import start_trace
# start a trace session, and print a url for user to check trace
start_trace()
# rerun the function, which will be recorded in the trace
result = my_llm_tool(
prompt="Write a simple Hello, world! program that displays the greeting message when executed. Output code only.",
deployment_name=deployment_name,
)
result
现在,让我们添加另一层函数调用。在programmer.py中有一个名为write_simple_program的函数,它调用了一个名为load_prompt的新函数以及之前的my_llm_tool函数。
# show the programmer.py content
with open("programmer.py") as fin:
print(fin.read())
# call the flow entry function
from programmer import write_simple_program
result = write_simple_program("Java Hello, world!")
result
使用环境变量设置模型配置#
在本地使用时,使用环境变量创建一个模型配置对象。
import os
from dotenv import load_dotenv
from promptflow.core import AzureOpenAIModelConfiguration
if "AZURE_OPENAI_API_KEY" not in os.environ:
# load environment variables from .env file
load_dotenv()
if "AZURE_OPENAI_API_KEY" not in os.environ:
raise Exception("Please specify environment variables: AZURE_OPENAI_API_KEY")
model_config = AzureOpenAIModelConfiguration(
azure_endpoint=os.environ["AZURE_OPENAI_ENDPOINT"],
api_key=os.environ["AZURE_OPENAI_API_KEY"],
azure_deployment=deployment_name,
api_version="2023-07-01-preview",
)
评估结果#
%load_ext autoreload
%autoreload 2
import paths # add the code_quality module to the path
from code_quality import CodeEvaluator
evaluator = CodeEvaluator(model_config=model_config)
eval_result = evaluator(result)
eval_result
2. 以流的方式批量运行函数,处理多行数据#
创建一个flow.flex.yaml文件来定义一个流程,该流程的入口指向我们定义的python函数。
# show the flow.flex.yaml content
with open("flow.flex.yaml") as fin:
print(fin.read())
使用数据文件进行批量运行(包含多行测试数据)#
from promptflow.client import PFClient
pf = PFClient()
data = "./data.jsonl" # path to the data file
# create run with the flow function and data
base_run = pf.run(
flow=write_simple_program,
data=data,
column_mapping={
"text": "${data.text}",
},
stream=True,
)
details = pf.get_details(base_run)
details.head(10)
3. 评估你的流程#
然后你可以使用评估方法来评估你的流程。评估方法也是流程,通常使用LLM来断言生成的输出是否符合某些预期。
对之前的批量运行进行评估#
base_run 是我们在上述步骤2中完成的批量运行,用于以“data.jsonl”作为输入的web分类流程。
# we can also run flow pointing to yaml file
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 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])
下一步#
到目前为止,你已经成功运行了你的第一个提示流程,并对其进行了评估。这真是太棒了!
你可以查看更多示例:
Basic Chat: 演示如何创建一个能够记住之前交互并使用对话历史生成下一条消息的聊天机器人。