> ## 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.

# 在实验上自动运行评估器

LangSmith 支持两种方式来对通过 SDK 创建的实验进行评分：

* **通过编程方式**，在代码中指定评估器（详情请参阅[如何评估 LLM 应用](/langsmith/evaluate-llm-application)）
* 通过在 UI 中**将评估器绑定到数据集**。这将自动在任何新创建的实验上运行评估器，此外还会运行您通过 SDK 设置的任何评估器。当您正在迭代应用程序（目标函数），并且有一套标准评估器希望在所有实验上运行时，这非常有用。

## 在数据集上配置评估器

1. 在 [LangSmith UI](https://smith.langchain.com) 中，选择一个数据集。
2. 点击 **Evaluators**（评估器）选项卡。
3. 点击 **+ Evaluator**（+ 评估器）以打开 **Add Evaluator**（添加评估器）面板。
4. 选择以下选项之一：
   * **从头创建**：构建一个新的 [LLM 作为评判者](/langsmith/llm-as-judge)、[代码](/langsmith/online-evaluations-code) 或 [复合](/langsmith/composite-evaluators-ui) 评估器，或选择 **From labeled data**（从标记数据）来创建一个与[人类反馈对齐](/langsmith/improve-judge-evaluator-feedback)的 LLM 作为评判者评估器。
   * **附加现有评估器**：选择您工作区中已有的评估器以重复使用。
   * **从模板创建**：从现成的评估器开始。

<Note>
  当您为数据集配置评估器时，它只会影响在评估器配置后创建的实验运行。它不会影响在评估器配置之前创建的实验运行的评估。
</Note>

## LLM 作为评判者的评估器

将评估器绑定到数据集的过程与在 Playground 中配置 LLM 作为评判者评估器的过程非常相似。请查看[在 Playground 中配置 LLM 作为评判者评估器](/langsmith/llm-as-judge?mode=ui)的说明。

## 自定义代码评估器

将代码评估器绑定到数据集的过程与在线评估中配置代码评估器的过程非常相似。请查看[配置代码评估器](/langsmith/online-evaluations-code)的说明。

在在线评估中配置代码评估器与将代码评估器绑定到数据集之间的唯一区别在于，自定义代码评估器可以引用作为数据集 `Example`（示例）一部分的输出。

对于绑定到数据集的自定义代码评估器，评估器函数接受两个参数：

* 一个 `Run`（运行）（[参考](/langsmith/run-data-format)）。这代表您实验中的新运行。例如，如果您通过 SDK 运行了一个实验，这将包含您正在测试的链或模型的输入/输出。
* 一个 `Example`（示例）（[参考](/langsmith/example-data-format)）。这代表您数据集中的参考示例，您正在测试的链或模型使用该示例。`Run` 和 `Example` 的 `inputs`（输入）应该相同。如果您的 `Example` 有参考 `outputs`（输出），那么您可以使用它来与运行的输出进行比较以进行评分。

下面的代码展示了一个简单的评估器函数示例，该函数检查输出是否与参考输出完全相等。

<CodeGroup>
  ```python Python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  import numpy as np

  def perform_eval(run, example):
      # run 是一个 Run 对象
      # example 是一个 Example 对象
      output = run['outputs']['output']
      ref_output = example['outputs']['outputs']
      output_match = np.array_equal(output, ref_output)

      return { "exact_match": output_match }
  ```

  ```javascript JavaScript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  function perform_eval(run, example) {
      // run 是一个 Run 对象
      // example 是一个 Example 对象
      const output = run.outputs.output;
      const refOutput = example.outputs.outputs;

      // 对数组/对象进行深度相等性检查
      const outputMatch = JSON.stringify(output) === JSON.stringify(refOutput);

      return { "exact_match": outputMatch };
  }
  ```
</CodeGroup>

## 后续步骤

* 在[实验选项卡](/langsmith/analyze-an-experiment)中分析您的实验结果
* 在[比较视图](/langsmith/compare-experiment-results)中比较您的实验结果

***

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

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