Skip to main content

可观测性快速入门

本教程将通过向您展示如何将应用程序追踪到 LangSmith,帮助您快速启动并运行我们的可观测性 SDK。

如果您已经熟悉可观测性 SDK,或者您有兴趣追踪不仅仅是大语言模型调用的内容,您可以直接跳转到下一步骤部分, 或查看操操作指南

追踪 LangChain 或 LangGraph 应用程序

如果您正在使用 LangChainLangGraph,这两者均可与 LangSmith 无缝集成, 您可以阅读关于使用 LangChain 进行追踪或使用 LangGraph 进行追踪的指南来开始使用。

1. 安装依赖项

pip install -U langsmith openai

2. 创建一个API密钥

要创建API密钥,请前往 LangSmith 设置页面。然后点击 创建API密钥。

3. 设置你的环境

export LANGSMITH_TRACING=true
export LANGSMITH_API_KEY="<your-langsmith-api-key>"
# The example uses OpenAI, but it's not necessary if your code uses another LLM provider
export OPENAI_API_KEY="<your-openai-api-key>"

4. 定义你的应用

我们将为本教程中的一个简单的 RAG 应用程序添加监控功能,但如果您愿意,也可以使用您自己的代码——只需确保它包含一次大语言模型(LLM)调用即可!

应用代码
from openai import OpenAI

openai_client = OpenAI()

# This is the retriever we will use in RAG
# This is mocked out, but it could be anything we want
def retriever(query: str):
results = ["Harrison worked at Kensho"]
return results

# This is the end-to-end RAG chain.
# It does a retrieval step then calls OpenAI
def rag(question):
docs = retriever(question)
system_message = """Answer the users question using only the provided information below:

{docs}""".format(docs="\n".join(docs))

return openai_client.chat.completions.create(
messages=[
{"role": "system", "content": system_message},
{"role": "user", "content": question},
],
model="gpt-4o-mini",
)

5. 跟踪 OpenAI 调用

您可能想要追踪的第一件事是所有的 OpenAI 调用。LangSmith 通过 wrap_openai(Python)或 wrapOpenAI(TypeScript)包装器使这项工作变得简单。 您只需修改代码,使用被包装的客户端,而不是直接使用 OpenAI 客户端即可。

from openai import OpenAI
from langsmith.wrappers import wrap_openai

openai_client = wrap_openai(OpenAI())

# This is the retriever we will use in RAG
# This is mocked out, but it could be anything we want
def retriever(query: str):
results = ["Harrison worked at Kensho"]
return results

# This is the end-to-end RAG chain.
# It does a retrieval step then calls OpenAI
def rag(question):
docs = retriever(question)
system_message = """Answer the users question using only the provided information below:

{docs}""".format(docs="\n".join(docs))

return openai_client.chat.completions.create(
messages=[
{"role": "system", "content": system_message},
{"role": "user", "content": question},
],
model="gpt-4o-mini",
)

现在当你如下调用你的应用程序时:

rag("where did harrison work")

这将在 LangSmith 的默认追踪项目中生成仅包含 OpenAI 调用的追踪记录。其外观应类似于 这样

6. 跟踪整个应用程序

您还可以使用 [traceable] 装饰器(PythonTypeScript)来追踪您的整个应用程序,而不仅仅是大语言模型(LLM)的调用。

from openai import OpenAI
from langsmith import traceable
from langsmith.wrappers import wrap_openai

openai_client = wrap_openai(OpenAI())

def retriever(query: str):
results = ["Harrison worked at Kensho"]
return results

@traceable
def rag(question):
docs = retriever(question)
system_message = """Answer the users question using only the provided information below:

{docs}""".format(docs="\n".join(docs))

return openai_client.chat.completions.create(
messages=[
{"role": "system", "content": system_message},
{"role": "user", "content": question},
],
model="gpt-4o-mini",
)

现在如果你以下列方式调用你的应用程序:

rag("where did harrison work")

这将生成整个管道的跟踪(OpenAI 调用作为子运行)——其外观应类似于 这个

下一步

恭喜!如果你已经做到了这一步,你已经在成为 LangSmith 可观测性专家的道路上迈出了坚实的步伐。 以下是一些你可能想要继续探索的主题:

或者您可以访问操操作指南页面,了解使用LangSmith可观测性所能完成的所有功能。

如果您更喜欢视频教程,请查看LangSmith 入门课程中的跟踪基础视频


此页面有帮助吗?


您可以留下详细的反馈 在 GitHub 上