可观测性快速入门
本教程将向您展示如何 将您的应用程序跟踪到 LangSmith。
如果您已经熟悉可观测性 SDK,或者对跟踪不仅仅是 LLM 调用,您可以跳至后续步骤部分, 或查看操作指南。
跟踪 LangChain 或 LangGraph 应用程序
1. 安装依赖项
- 蟒
- TypeScript (类型脚本)
pip install -U langsmith openai
yarn add 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 调用!
应用程序代码
- 蟒
- TypeScript (类型脚本)
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",
)
import { OpenAI } from "openai";
const openAIClient = new OpenAI();
// This is the retriever we will use in RAG
// This is mocked out, but it could be anything we want
async function retriever(query: string) {
return ["This is a document"];
}
// This is the end-to-end RAG chain.
// It does a retrieval step then calls OpenAI
async function rag(question: string) {
const docs = await retriever(question);
const systemMessage =
"Answer the users question using only the provided information below:\n\n" +
docs.join("\n");
return await openAIClient.chat.completions.create({
messages: [
{ role: "system", content: systemMessage },
{ role: "user", content: question },
],
model: "gpt-4o-mini",
});
}
5. 跟踪 OpenAI 调用
您可能要跟踪的第一件事是所有 OpenAI 调用。LangSmith 通过wrap_openai(Python) 或wrapOpenAI(TypeScript) 包装器。
您所要做的就是修改代码以使用包装的客户端,而不是使用OpenAIclient 直接。
- 蟒
- TypeScript (类型脚本)
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",
)
import { OpenAI } from "openai";
import { wrapOpenAI } from "langsmith/wrappers";
const openAIClient = wrapOpenAI(new OpenAI());
// This is the retriever we will use in RAG
// This is mocked out, but it could be anything we want
async function retriever(query: string) {
return ["This is a document"];
}
// This is the end-to-end RAG chain.
// It does a retrieval step then calls OpenAI
async function rag(question: string) {
const docs = await retriever(question);
const systemMessage =
"Answer the users question using only the provided information below:\n\n" +
docs.join("\n");
return await openAIClient.chat.completions.create({
messages: [
{ role: "system", content: systemMessage },
{ role: "user", content: question },
],
model: "gpt-4o-mini",
});
}
现在,当您按如下方式调用应用程序时:
rag("where did harrison work")
这将在 LangSmith 的默认跟踪项目中生成仅 OpenAI 调用的跟踪。它应该看起来像这样。

6. 跟踪整个应用程序
也可以使用 [traceable] 装饰器(Python 或 TypeScript)来跟踪整个应用程序,而不仅仅是 LLM 调用。
- 蟒
- TypeScript (类型脚本)
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",
)
import { OpenAI } from "openai";
import { traceable } from "langsmith/traceable";
import { wrapOpenAI } from "langsmith/wrappers";
const openAIClient = wrapOpenAI(new OpenAI());
async function retriever(query: string) {
return ["This is a document"];
}
const rag = traceable(async function rag(question: string) {
const docs = await retriever(question);
const systemMessage =
"Answer the users question using only the provided information below:\n\n" +
docs.join("\n");
return await openAIClient.chat.completions.create({
messages: [
{ role: "system", content: systemMessage },
{ role: "user", content: question },
],
model: "gpt-4o-mini",
});
});
现在,如果您按如下方式调用应用程序:
rag("where did harrison work")
这将生成整个管道的跟踪(将 OpenAI 调用作为子运行)- 它应该看起来像这样

后续步骤
祝贺!如果您已经做到了这一点,那么您已经顺利成为 LangSmith 的可观测性专家了。 以下是您接下来可能想要探索的一些主题:
或者,您可以访问操作指南页面,了解您可以使用 LangSmith 可观测性执行的所有作。
如果您更喜欢视频教程,请观看 LangSmith 简介课程中的跟踪基础知识视频。