与 Prompty 聊天#
学习目标 - 完成本教程后,您应该能够
使用 Prompty 编写 LLM 应用程序并可视化应用程序的跟踪。
了解如何使用 Prompty 处理聊天对话
对多行数据进行批处理运行 Prompty。
0. 安装依赖包#
%%capture --no-stderr
%pip install promptflow-devkit
1. Prompty#
Prompty 是一个扩展名为 .prompty 的文件,用于开发提示模板。Prompty 资产是一个带有修改后的前置元数据的 markdown 文件。前置元数据采用 yaml 格式,包含许多元数据字段,这些字段定义了模型配置和 Prompty 的预期输入。
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)
将 Prompty 作为函数执行#
from promptflow.core import Prompty
# load prompty as a flow
f = Prompty.load("chat.prompty")
# execute the flow as function
question = "What is the capital of France?"
result = f(question=question)
result
您可以使用 AzureOpenAIModelConfiguration
和 OpenAIModelConfiguration
覆盖连接。
from promptflow.core import AzureOpenAIModelConfiguration, OpenAIModelConfiguration
# override configuration with created connection in AzureOpenAIModelConfiguration
configuration = AzureOpenAIModelConfiguration(
connection="open_ai_connection", azure_deployment="gpt-4o"
)
# override openai connection with OpenAIModelConfiguration
# configuration = OpenAIModelConfiguration(
# connection=connection,
# model="gpt-3.5-turbo"
# )
override_model = {
"configuration": configuration,
}
# load prompty as a flow
f = Prompty.load("chat.prompty", model=override_model)
# execute the flow as function
question = "What is the capital of France?"
result = f(question=question)
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
result = f(question=question)
result
评估结果#
在此示例中,我们将使用一个提示来确定聊天对话是否包含助手的道歉。
eval_prompty = "../eval-apology/apology.prompty"
with open(eval_prompty) as fin:
print(fin.read())
注意:评估流返回一个 json_object
。
# load prompty as a flow
eval_flow = Prompty.load(eval_prompty)
# execute the flow as function
result = eval_flow(question=question, answer=result, messages=[])
result
2. 多行数据批处理运行#
from promptflow.client import PFClient
flow = "chat.prompty" # path to the prompty file
data = "./data.jsonl" # path to the data file
# create run with the flow and data
pf = PFClient()
base_run = pf.run(
flow=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. 评估您的 Prompty#
然后您可以使用评估 Prompty 来评估您的 Prompty。
对之前的批量运行进行评估#
base_run 是我们在上面第 2 步中完成的批量运行,用于 web-classification 流,输入为“data.jsonl”。
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={
"messages": "${data.chat_history}",
"question": "${data.question}",
"answer": "${run.outputs.output}", # TODO refine this mapping
},
stream=True,
)
details = pf.get_details(eval_run)
details.head(10)
后续步骤#
现在您已经成功运行了您的第一个提示流并对其进行了评估。太棒了!
您可以查看更多 Prompty 示例。