可观测性快速入门
本教程将向您展示如何将应用程序追踪到 LangSmith,从而帮助您快速上手我们的可观测性 SDK。
如果您已经熟悉可观测性 SDK,或者不仅对 LLM 调用追踪感兴趣,您可以跳到下一步部分,或者查看操作指南。
如果您正在使用 LangChain 或 LangGraph(两者均与 LangSmith 无缝集成),您可以通过阅读LangChain 追踪指南或LangGraph 追踪指南来开始。
1. 安装依赖
- Python
- TypeScript
pip install -U langsmith openai
yarn add langsmith openai
2. 创建 API 密钥
要创建 API 密钥,请前往 LangSmith 设置页面。然后点击创建 API 密钥。
3. 设置环境
- Shell
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 调用即可!
应用程序代码
- Python
- 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) 包装器使其变得简单。您只需修改代码以使用包装后的客户端,而不是直接使用 OpenAI
客户端。
- Python
- 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 调用。
- Python
- 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 入门课程中的追踪基础视频。