> ## 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 UI](https://smith.langchain.com) 中的代码评估器允许您直接在界面中使用 Python 或 TypeScript 代码编写自定义评估逻辑。与使用模型来评估输出的 [LLM 作为评判者](/langsmith/llm-as-a-judge) 评估器不同，代码评估器使用您定义的确定性逻辑。

<Note>
  要使用 SDK 以编程方式定义代码评估器，请参阅 [如何定义代码评估器 (SDK)](/langsmith/code-evaluator-sdk)。
</Note>

## 步骤 1. 创建评估器

1. 从 [LangSmith UI](https://smith.langchain.com) 中的以下页面之一创建评估器：
   * 在 Playground 中或从数据集中：选择 **+ Evaluator** 按钮。
   * 选择 **Add rules**，配置您的规则并选择 **Apply evaluator**。
2. 为您的评估器指定一个清晰的名称，描述其衡量的内容（例如，“精确匹配”）。
3. 从评估器类型选项中选择 **Create code evaluator**。

## 步骤 2. 编写评估器代码

<Note>
  **自定义代码评估器限制。**

  **允许的库**：您可以导入所有标准库函数，以及以下公共包：

  ```
  numpy (v2.2.2): "numpy"
  pandas (v1.5.2): "pandas"
  jsonschema (v4.21.1): "jsonschema"
  scipy (v1.14.1): "scipy"
  sklearn (v1.26.4): "scikit-learn"
  ```

  **网络访问**：您无法从自定义代码评估器访问互联网。
</Note>

在 **Add Custom Code Evaluator** 页面中，使用 Python 或 TypeScript 定义您的评估逻辑。

您的评估器函数必须命名为 `perform_eval`，并且应该：

1. 接受 `run` 和 `example` 参数。
2. 通过 `run['inputs']`、`run['outputs']` 和 `example['outputs']` 访问数据。
3. 返回一个字典，其中每个键是一个指标名称，每个值是该指标的分数。每个键代表您想要返回的一条反馈。例如，`{"correctness": 1, "silliness": 0}` 将为该运行创建两条反馈。

### 函数签名

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
def perform_eval(run, example):
    # 访问数据
    inputs = run['inputs']
    outputs = run['outputs']
    reference_outputs = example['outputs']  # 可选：参考/预期输出

    # 在此编写您的评估逻辑
    score = ...

    # 返回包含指标名称的字典
    return {"metric_name": score}
```

### 示例：精确匹配评估器

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
def perform_eval(run, example):
    """检查答案是否与预期答案完全匹配。"""
    actual = run['outputs']['answer']
    expected = example['outputs']['answer']

    is_correct = actual == expected
    return {"exact_match": is_correct}
```

### 示例：基于输入的评估器

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
def perform_eval(run, example):
    """检查输入文本是否包含有毒语言。"""
    text = run['inputs'].get('text', '').lower()
    toxic_words = ["idiot", "stupid", "hate", "awful"]

    is_toxic = any(word in text for word in toxic_words)
    return {"is_toxic": is_toxic}
```

## 步骤 3. 测试并保存

1. 在示例数据上测试您的评估器，确保其按预期工作
2. 点击 **Save** 以使评估器可供使用

## 使用您的代码评估器

创建后，您可以使用您的代码评估器：

* 从 [Playground](/langsmith/prompt-engineering-concepts#playground) 运行评估时
* 作为数据集的一部分，以[自动对实验运行评估](/langsmith/bind-evaluator-to-dataset)

## 相关内容

* [LLM 作为评判者评估器 (UI)](/langsmith/llm-as-a-judge)：使用 LLM 评估输出
* [组合评估器](/langsmith/composite-evaluators-ui)：组合多个评估器分数

***

<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/code-evaluator-ui.mdx) 或 [提交问题](https://github.com/langchain-ai/docs/issues/new/choose)。
  </Callout>
</div>
