Skip to content
LangSmith 文档版本 跟随上游 源快照 271b8082 更新于 2026-10-18

如何返回分类型指标与数值型指标

LangSmith 同时支持分类型与数值型指标,编写自定义评估器时两者都可以返回。

要让评估器结果被记录为数值型指标,必须按下列形式返回:

  • (仅 Python)一个 int、float 或 bool
  • 形如 {"key": "metric_name", "score": int | float | bool} 的 dict

要让评估器结果被记录为分类型指标,必须按下列形式返回:

  • (仅 Python)一个 str
  • 形如 {"key": "metric_name", "value": str | int | float | bool} 的 dict

下面是一些示例:

  • Python:需要 langsmith>=0.2.0
  • TypeScript:langsmith@0.1.32 及更高版本支持返回多个得分
python
def numerical_metric(inputs: dict, outputs: dict, reference_outputs: dict) -> float:
    # Evaluation logic...
    return 0.8
    # Equivalently
    # return {"score": 0.8}
    # Or
    # return {"key": "numerical_metric", "score": 0.8}

def categorical_metric(inputs: dict, outputs: dict, reference_outputs: dict) -> str:
    # Evaluation logic...
    return "english"
    # Equivalently
    # return {"key": "categorical_metric", "score": "english"}
    # Or
    # return {"score": "english"}
typescript
import type { Run, Example } from "langsmith/schemas";

function numericalMetric(run: Run, example: Example) {
  // Your evaluation logic here
  return { key: "numerical_metric", score: 0.8};
}

function categoricalMetric(run: Run, example: Example) {
  // Your evaluation logic here
  return { key: "categorical_metric", value: "english"};
}

相关


BytesRouter 是社区翻译站点,所有译文仅供参考,一切以官方英文原文为准。