跳到主要内容

Observability 快速入门

本教程将通过演示如何将你的应用程序追踪到 LangSmith,帮助你快速开始使用我们的 observability SDK。

如果你已经熟悉 observability 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. 定义你的应用

在本教程中,我们将instrument一个简单的 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) wrappers 使这变得容易。你所要做的就是修改你的代码以使用 wrapped client,而不是直接使用 OpenAI client。

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")

这将生成仅包含整个 pipeline 的追踪(OpenAI 调用作为子运行)- 它应该看起来像这样

后续步骤

恭喜你!如果你已经走到这一步,那么你离成为 LangSmith observability 专家已经不远了。以下是你可能想要接下来探索的一些主题

或者你可以访问操作指南页面,了解关于你可以使用 LangSmith observability 做的一切。

如果你更喜欢视频教程,请查看 LangSmith 入门课程中的Tracing Basics 视频


此页面是否对您有帮助?


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