使用 SDK 记录用户反馈
关键概念
LangSmith 使得将反馈附加到追踪变得容易。这些反馈可以来自用户、标注者、自动化评估器等,对于监控和评估应用程序至关重要。
使用 create_feedback() / createFeedback()
这里我们将逐步介绍如何使用 SDK 记录反馈。
子运行
您可以将用户反馈附加到追踪的任何子运行,而不仅仅是追踪(根运行)本身。这对于批评 LLM 应用程序的特定步骤非常有用,例如 RAG 管道的检索步骤或生成步骤。
非阻塞式创建 (仅限 Python)
如果您将 `trace_id=` 传递给 create_feedback(),Python 客户端将自动在后台创建反馈。这对于低延迟环境至关重要,在这种环境中,您希望确保应用程序不会因反馈创建而阻塞。
- Python
- TypeScript
要求 `langsmith >= 0.3.43`
from langsmith import trace, traceable, Client
@traceable
def foo(x):
return {"y": x * 2}
@traceable
def bar(y):
return {"z": y - 1}
client = Client()
inputs = {"x": 1}
with trace(name="foobar", inputs=inputs) as root_run:
result = foo(**inputs)
result = bar(**result)
root_run.outputs = result
trace_id = root_run.id
child_runs = root_run.child_runs
# Provide feedback for a trace (a.k.a. a root run)
client.create_feedback(
key="user_feedback",
score=1,
trace_id=trace_id,
comment="the user said that ..."
)
# Provide feedback for a child run
foo_run_id = [run for run in child_runs if run.name == "foo"][0].id
client.create_feedback(
key="correctness",
score=0,
run_id=foo_run_id,
# trace_id= is optional but recommended to enable batched and backgrounded
# feedback ingestion.
trace_id=trace_id,
)
import { Client } from "langsmith";
const client = new Client();
// ... Run your application and get the run_id...
// This information can be the result of a user-facing feedback form
await client.createFeedback(
runId,
"feedback-key",
{
score: 1.0,
comment: "comment",
}
);
您甚至可以使用 `create_feedback() / createFeedback()` 记录正在进行的运行的反馈。请参阅本指南,了解如何获取正在进行的运行的 ID。
要了解有关如何根据包括用户反馈在内的各种属性过滤追踪的更多信息,请参阅本指南。