LangSmith 使您可以轻松附加 反馈 to 跟踪。此反馈可来自用户、标注员、自动评估器等,这对于监控和评估应用程序至关重要。
本页详细说明如何使用 SDK记录反馈。如需了解反馈对象的结构,请参阅 反馈数据格式.
使用 create_feedback() / createFeedback
以下示例创建一个包含两个子运行的跟踪,然后针对根运行和其中一个子运行记录反馈。TypeScript 代码片段显示等效的 createFeedback 调用形式,假设 runId 已从您的应用程序中可用。
from langsmith import Client, trace, traceable
@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,
)
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。
从客户端应用程序收集反馈
如果您需要从浏览器或其他客户端环境收集反馈而不暴露您的 API 密钥,请使用 **预签名反馈令牌**。这些会生成一个限定于特定运行和反馈键的 URL,客户端可以直接调用。
请参阅 使用预签名 URL 收集反馈 获取完整指南。
要了解有关如何根据各种属性(包括用户反馈)筛选跟踪的更多信息,请参阅 筛选跟踪.