基于类的弹性流程聊天#

作者:  Open on GitHub Open on GitHubOpen on GitHub

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

  • 使用基于类的灵活流程编写LLM应用程序。

  • 使用 AzureOpenAIConnection 作为类的初始化参数。

  • 将应用程序转换为流程,并针对多行数据进行批处理运行。

  • 使用基于类的流程来评估主流程,并学习如何进行聚合。

0. 安装依赖包#

%%capture --no-stderr
%pip install -r ./requirements.txt

1. 使用promptflow跟踪您的应用程序#

假设我们已经有一个Python程序,它利用了promptflow内置的aoai工具。

with open("flow.py") as fin:
    print(fin.read())

创建必要的连接#

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

上述提示内部使用了连接 open_ai_connection,如果之前没有添加过,我们需要设置这个连接。创建后,它会被存储在本地数据库中,并可以在任何流程中使用。

按照此说明准备您的Azure OpenAI资源,并获取您的api_key(如果您还没有)。

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="<your_AOAI_key>",
        api_base="<your_AOAI_endpoint>",
        api_type="azure",
    )

    # 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)
from promptflow.core import AzureOpenAIModelConfiguration

# create the model config to be used in below flow calls
config = AzureOpenAIModelConfiguration(
    connection="open_ai_connection", azure_deployment="gpt-4o"
)

使用start_trace可视化跟踪#

注意我们在my_llm_tool函数中添加了@trace,重新运行下面的单元格将在trace UI中收集一个跟踪。

from flow import ChatFlow
from promptflow.tracing import start_trace

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

# create a chatFlow obj with connection
chat_flow = ChatFlow(config)
# run the flow as function, which will be recorded in the trace
result = chat_flow(question="What is ChatGPT? Please explain with consise statement")
result

评估结果#

%load_ext autoreload
%autoreload 2

import paths  # add the code_quality module to the path
from check_list import EvalFlow

eval_flow = EvalFlow(config)
# evaluate answer agains a set of statement
eval_result = eval_flow(
    answer=result,
    statements={
        "correctness": "It contains a detailed explanation of ChatGPT.",
        "consise": "It is a consise statement.",
    },
)
eval_result

2. 以流的方式批量运行函数,处理多行数据#

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

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=chat_flow,
    data=data,
    column_mapping={
        "question": "${data.question}",
        "chat_history": "${data.chat_history}",
    },
    stream=True,
)
details = pf.get_details(base_run)
details.head(10)

3. 评估你的流程#

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

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

base_run 是我们在上述步骤2中完成的批量运行,用于以“data.jsonl”作为输入的web分类流程。

eval_run = pf.run(
    flow=eval_flow,
    data="./data.jsonl",  # path to the data file
    run=base_run,  # specify base_run as the run you want to evaluate
    column_mapping={
        "answer": "${run.outputs.output}",
        "statements": "${data.statements}",
    },
    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])

下一步#

到目前为止,你已经成功运行了你的聊天流程并对其进行了评估。这太棒了!

你可以查看更多示例:

  • Stream Chat: 演示如何创建一个在流模式下运行的聊天机器人。