> ## 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 同时支持分类指标和数值指标，您在编写自定义评估器时可以返回其中任意一种。

要使评估器结果被记录为数值指标，必须返回以下形式：

* （仅限 Python）`int`、`float` 或 `bool` 类型
* 形如 `{"key": "metric_name", "score": int | float | bool}` 的字典

要使评估器结果被记录为分类指标，必须返回以下形式：

* （仅限 Python）`str` 类型
* 形如 `{"key": "metric_name", "value": str | int | float | bool}` 的字典

以下是一些示例：

* Python：需要 `langsmith>=0.2.0`
* TypeScript：`langsmith@0.1.32` 及更高版本支持多分数返回

<CodeGroup>
  ```python Python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  def numerical_metric(inputs: dict, outputs: dict, reference_outputs: dict) -> float:
      # 评估逻辑...
      return 0.8
      # 等效于
      # return {"score": 0.8}
      # 或
      # return {"key": "numerical_metric", "score": 0.8}

  def categorical_metric(inputs: dict, outputs: dict, reference_outputs: dict) -> str:
      # 评估逻辑...
      return "english"
      # 等效于
      # return {"key": "categorical_metric", "score": "english"}
      # 或
      # return {"score": "english"}
  ```

  ```typescript TypeScript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  import type { Run, Example } from "langsmith/schemas";

  function numericalMetric(run: Run, example: Example) {
    // 您的评估逻辑在此处
    return { key: "numerical_metric", score: 0.8};
  }

  function categoricalMetric(run: Run, example: Example) {
    // 您的评估逻辑在此处
    return { key: "categorical_metric", value: "english"};
  }
  ```
</CodeGroup>

## 相关内容

* [在一个评估器中返回多个指标](/langsmith/multiple-scores)

***

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