> ## Documentation Index
> Fetch the complete documentation index at: https://langchain-zh.cn/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="用户表示……"
      )

  # 为子运行提供反馈
  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: "评论内容",
      }
  );
  ```
</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) 为进行中的运行记录反馈。关于如何获取进行中运行的运行 ID，请参阅[在追踪函数内访问当前运行（跨度）](/langsmith/access-current-span)。

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

***

<div className="source-links">
  <Callout icon="edit">
    [Edit this page on GitHub](https://github.com/langchain-ai/docs/edit/main/src/i18n\zh-CN\langsmith\attach-user-feedback.mdx) or [file an issue](https://github.com/langchain-ai/docs/issues/new/choose).
  </Callout>

  <Callout icon="terminal-2">
    [Connect these docs](/use-these-docs) to Claude, VSCode, and more via MCP for real-time answers.
  </Callout>
</div>
