Skip to main content

可观测性快速入门

本教程将向您展示如何 将您的应用程序跟踪到 LangSmith。

如果您已经熟悉可观测性 SDK,或者对跟踪不仅仅是 LLM 调用,您可以跳至后续步骤部分, 或查看操作指南

跟踪 LangChain 或 LangGraph 应用程序

如果您使用的是 LangChainLangGraph,它们都与 LangSmith 无缝集成, 您可以通过阅读 使用 LangChain 进行跟踪 或使用 LangGraph 进行跟踪的指南来开始使用。

1. 安装依赖项

pip install -U langsmith openai

2. 创建 API 密钥

要创建 API 密钥,请前往 LangSmith 设置页面。然后单击 Create API Key(创建 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) 包装器。 您所要做的就是修改代码以使用包装的客户端,而不是使用OpenAIclient 直接。

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 上留下详细的反馈。