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

如何在一个评估器中返回多个得分

有时自定义评估器或汇总评估器需要一次返回多个指标。例如,当 LLM 裁判会产出多个指标时,用一次 LLM 调用生成全部指标,比发起多次 LLM 调用更省时间和成本。

要用 Python SDK 返回多个得分,只需返回一个列表,其中的字典/对象形如:

python
[
    # 'key' is the metric name
    # 'score' is the value of a numerical metric
    {"key": string, "score": number},
    # 'value' is the value of a categorical metric
    {"key": string, "value": string},
    ... # You may log as many as you wish
]

用 JS/TS SDK 时,返回一个带 'results' 键的对象,其值就是上述形式的列表

typescript
{results: [{ key: string, score: number }, ...]};

这些字典可以包含反馈字段中的任意几项或全部,更多信息见所链接的文档。

示例:

  • Python:需要 langsmith>=0.2.0
  • TypeScript:langsmith@0.1.32 及更高版本支持返回多个得分
python
def multiple_scores(outputs: dict, reference_outputs: dict) -> list[dict]:
    # Replace with real evaluation logic.
    precision = 0.8
    recall = 0.9
    f1 = 0.85
    return [
        {"key": "precision", "score": precision},
        {"key": "recall", "score": recall},
        {"key": "f1", "score": f1},
    ]
typescript
import type { Run, Example } from "langsmith/schemas";

function multipleScores(rootRun: Run, example: Example) {
  // Your evaluation logic here
  return {
      results: [
          { key: "precision", score: 0.8 },
          { key: "recall", score: 0.9 },
          { key: "f1", score: 0.85 },
      ],
  };
}

生成的实验中,每一行都会展示这些得分。

multiple_scores.png

相关


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