使用异步灵活流进行流式聊天#
学习目标 - 完成本教程后,您应该能够
使用基于类的灵活流编写 LLM 应用程序。
使用 AzureOpenAIModelConfiguration 作为类初始化参数。
使用 prompty 进行流式补全。
将应用程序转换为异步流并对多行数据进行批量运行。
使用基于类的流评估主流程,并学习如何进行聚合。
0. 安装依赖包#
%%capture --no-stderr
%pip install -r ./requirements.txt
1. 使用 promptflow 追踪您的应用程序#
假设我们已经有一个利用 prompty 的 Python 程序。
with open("flow.py") as fin:
print(fin.read())
当在输出格式为文本的提示的参数中配置 stream=true
时,promptflow sdk 将返回一个生成器类型,其项是每个块的内容。
参考 OpenAI 文档,了解如何使用纯 Python 代码实现:如何流式补全
with open("chat.prompty") as fin:
print(fin.read())
创建必要的连接#
连接有助于安全地存储和管理与 LLM 和其他外部工具(例如 Azure 内容安全)交互所需的密钥或其他敏感凭据。
上述 prompty 内部使用连接 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)
使用 start_trace 可视化追踪#
请注意,我们在 my_llm_tool
函数中添加了 @trace
,重新运行下面的单元格将在追踪 UI 中收集追踪。
from promptflow.tracing import start_trace
from promptflow.core import AzureOpenAIModelConfiguration
from flow import ChatFlow
# create a chatFlow obj with connection
config = AzureOpenAIModelConfiguration(
connection="open_ai_connection", azure_deployment="gpt-4o"
)
chat_flow = ChatFlow(config)
# start a trace session, and print a url for user to check trace
start_trace()
# run the flow as function, which will be recorded in the trace
result = chat_flow(question="What is ChatGPT? Please explain with detailed statement")
# note the type is async generator object as we enabled stream in prompty
result
import asyncio
# print result in stream manner
async for output in result:
print(output, end="")
await asyncio.sleep(0.01)
result = chat_flow(question="What is ChatGPT? Please explain with consise statement")
answer = ""
async for output in result:
answer += output
answer
评估结果#
%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=answer,
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 步中完成的批量运行,用于 web-classification 流,输入为“data.jsonl”。
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])
后续步骤#
至此,您已成功运行聊天流并对其进行了评估。太棒了!
您可以查看更多示例
流式聊天:演示如何创建以流模式运行的聊天机器人。