使用LangChain应用程序进行追踪#
Prompt flow 提供的追踪能力建立在 OpenTelemetry 之上,为您提供对 LLM 应用程序的完整可观测性。 在 OpenTelemetry 生态系统中,已经有丰富的 OpenTelemetry instrumentation packages 可用。
在这个例子中,我们将演示如何使用opentelemetry-instrumentation-langchain包,该包由Traceloop提供,用于对LangChain应用进行仪表化。
学习目标 - 完成本教程后,您应该能够:
追踪
LangChain
应用程序并在提示流中可视化应用程序的追踪。
需求#
要运行此笔记本示例,请安装所需的依赖项:
%%capture --no-stderr
%pip install -r ./requirements.txt
开始使用 promptflow 追踪 LangChain#
使用promptflow.start_trace
开始跟踪,点击打印的URL查看跟踪界面。
from promptflow.tracing import start_trace
# start a trace session, and print a url for user to check trace
start_trace()
默认情况下,opentelemetry-instrumentation-langchain
工具会将提示、完成和嵌入记录到跨度属性中。这使您能够清楚地了解您的LLM应用程序是如何工作的,并且可以轻松调试和评估输出的质量。
# enable langchain instrumentation
from opentelemetry.instrumentation.langchain import LangchainInstrumentor
instrumentor = LangchainInstrumentor()
if not instrumentor.is_instrumented_by_opentelemetry:
instrumentor.instrument()
运行一个简单的LangChain#
以下是一个针对AzureOpenAI资源的示例。请使用.env
文件配置您的API_KEY
,参见../.env.example
。
import os
from langchain.chat_models import AzureChatOpenAI
from langchain.prompts.chat import ChatPromptTemplate
from langchain.chains import LLMChain
from dotenv import load_dotenv
if "AZURE_OPENAI_API_KEY" not in os.environ:
# load environment variables from .env file
load_dotenv()
llm = AzureChatOpenAI(
deployment_name=os.environ["CHAT_DEPLOYMENT_NAME"],
openai_api_key=os.environ["AZURE_OPENAI_API_KEY"],
azure_endpoint=os.environ["AZURE_OPENAI_ENDPOINT"],
openai_api_type="azure",
openai_api_version="2023-07-01-preview",
temperature=0,
)
prompt = ChatPromptTemplate.from_messages(
[
("system", "You are world class technical documentation writer."),
("user", "{input}"),
]
)
chain = LLMChain(llm=llm, prompt=prompt, output_key="metrics")
chain({"input": "What is ChatGPT?"})
您现在应该能够在promptflow UI中看到链的痕迹。在跟踪UI的URL上检查带有start_trace
的单元格。
下一步#
到目前为止,您已经成功地使用提示流在您的应用程序中追踪了LLM调用。
你可以查看更多示例:
Trace your flow: 使用 promptflow @trace 结构化地追踪您的应用程序,并通过批量运行对其进行评估。