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

# 如何定义代码评估器

[LangSmith UI](https://smith.langchain.com) 中的代码评估器允许您直接在界面中使用 Python 或 TypeScript 代码编写自定义评估逻辑。与使用模型来评估输出的 [LLM 作为评判者](/langsmith/llm-as-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-judge)：使用 LLM 评估输出。
* [复合评估器](/langsmith/composite-evaluators-ui)：组合多个评估器分数。

***

<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\code-evaluator-ui.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>
