如何返回分类指标与数值指标
LangSmith 支持分类指标和数值指标,您可以在编写自定义评估器时返回任一类型。
要将评估器结果记录为数值指标,必须将其返回为
- (仅限 Python)
int
、float
或bool
- 形式为
{"key": "metric_name", "score": int | float | bool}
的字典
要将评估器结果记录为分类指标,必须将其返回为
- (仅限 Python)
str
- 形式为
{"key": "metric_name", "value": str | int | float | bool}
的字典
以下是一些示例
- Python
- TypeScript
需要 langsmith>=0.2.0
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"}
langsmith@0.1.32
及更高版本提供对多个分数的支持
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"};
}