主题
如何在一个评估器中返回多个得分
有时自定义评估器或汇总评估器需要一次返回多个指标。例如,当 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 },
],
};
}生成的实验中,每一行都会展示这些得分。
