> ## Documentation Index
> Fetch the complete documentation index at: https://cndoc-langchain.site/llms.txt
> Use this file to discover all available pages before exploring further.

# 使用 SDK 记录用户反馈

<Tip>
  **核心概念**

  * [关于跟踪与反馈的概念指南](/langsmith/observability-concepts)
  * [关于反馈数据格式的参考指南](/langsmith/feedback-data-format)
</Tip>

LangSmith 使得将反馈附加到跟踪变得非常简单。
这些反馈可以来自用户、标注员、自动评估器等，对于监控和评估应用程序至关重要。

## 使用 `create_feedback()` / `createFeedback`

这里我们将介绍如何使用 SDK 记录反馈。

<Info>
  **子运行**
  您可以将用户反馈附加到跟踪的任何子运行，而不仅仅是跟踪（根运行）本身。
  这对于评估 LLM 应用程序的特定步骤非常有用，例如 RAG 管道的检索步骤或生成步骤。
</Info>

<Tip>
  **非阻塞创建（仅限 Python）**
  如果您向 [`create_feedback()`](https://reference.langchain.com/python/langsmith/client/Client/create_feedback) 传递 `trace_id=`，Python 客户端将自动在后台创建反馈。
  这对于低延迟环境至关重要，您希望确保应用程序不会因反馈创建而被阻塞。
</Tip>

<CodeGroup>
  ```python Python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  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

      # 为跟踪（也称为根运行）提供反馈
      client.create_feedback(
          key="user_feedback",
          score=1,
          trace_id=trace_id,
          comment="the user said that ..."
      )

  # 为子运行提供反馈
  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= 是可选的，但建议提供以启用批量和后台反馈处理。
      trace_id=trace_id,
  )
  ```

  ```typescript TypeScript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  import { Client } from "langsmith";
  const client = new Client();

      // ... 运行您的应用程序并获取 run_id...
      // 此信息可以是面向用户的反馈表单的结果

  await client.createFeedback(
      runId,
      "feedback-key",
      {
          score: 1.0,
          comment: "comment",
      }
  );
  ```
</CodeGroup>

您甚至可以使用 [`create_feedback()`](https://reference.langchain.com/python/langsmith/client/Client/create_feedback) / [`createFeedback`](https://reference.langchain.com/javascript/classes/langsmith.client.Client.html#createfeedback) 为正在进行的运行记录反馈。请参阅[在跟踪的函数内访问当前运行（跨度）](/langsmith/access-current-span)以了解如何获取正在进行的运行的运行 ID。

## 从客户端应用程序收集反馈

如果您需要从浏览器或其他客户端环境收集反馈，同时又不想暴露您的 API 密钥，请使用**预签名反馈令牌**。这些令牌会生成一个 URL，该 URL 的范围限定为特定的运行和反馈键，客户端可以直接调用。

完整指南请参阅[使用预签名 URL 收集反馈](/langsmith/presigned-feedback-tokens)。

要了解更多关于如何根据各种属性（包括用户反馈）过滤跟踪的信息，请参阅[过滤跟踪](/langsmith/filter-traces-in-application)。

***

<div className="source-links">
  <Callout icon="terminal-2">
    [将这些文档连接](/use-these-docs)到 Claude、VSCode 等，通过 MCP 获取实时答案。
  </Callout>

  <Callout icon="edit">
    [在 GitHub 上编辑此页面](https://github.com/langchain-ai/docs/edit/main/src/langsmith/attach-user-feedback.mdx) 或 [提交问题](https://github.com/langchain-ai/docs/issues/new/choose)。
  </Callout>
</div>
