如何在评估器中返回多个分数
有时,对于自定义评估器函数或摘要评估器函数来说,返回多个指标非常有用。例如,如果您有多个指标由 LLM 法官生成,那么通过进行一次 LLM 调用来生成多个指标,而不是进行多次 LLM 调用,可以节省时间和金钱。
要使用 Python SDK 返回多个分数,只需返回以下形式的字典/对象列表
[
# '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”键的对象,然后返回上述形式的列表
{results: [{ key: string, score: number }, ...]};
这些字典中的每一个都可以包含反馈字段中的任何或全部;请查看链接的文档以获取更多信息。
示例
- Python
- TypeScript
需要 langsmith>=0.2.0
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},
]
langsmith@0.1.32
及更高版本支持多个分数
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 },
],
};
}
结果实验中的行将显示每个分数。