@arizeai/phoenix-evals
Version:
A library for running evaluations for AI use cases
47 lines • 2.26 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.createPrecisionRecallFScoreEvaluators = createPrecisionRecallFScoreEvaluators;
const classificationMetrics_1 = require("./classificationMetrics");
const createClassificationMetricEvaluator_1 = require("./createClassificationMetricEvaluator");
/**
* Wraps `computePrecisionRecallFScore` so repeated calls with the same
* `example` object (by reference) reuse the first computed result, instead
* of recomputing the full confusion matrix once per evaluator.
*/
function createCachedComputer() {
const cache = new WeakMap();
return (example, options) => {
const cached = cache.get(example);
if (cached) {
return cached;
}
const result = (0, classificationMetrics_1.computePrecisionRecallFScore)(example, options);
cache.set(example, result);
return result;
};
}
/**
* Creates matching precision, recall, and F-beta evaluators from a single set
* of options, so all three are computed with the same `average`, `beta`,
* `positiveLabel`, and `zeroDivision` settings. When the same `expected`/
* `output` example object is passed to all three evaluators, the underlying
* confusion matrix is only computed once and shared across them.
*
* @example
* ```typescript
* const { precision, recall, fScore } = createPrecisionRecallFScoreEvaluators({
* average: "weighted",
* });
* ```
*/
function createPrecisionRecallFScoreEvaluators(options = {}) {
const { beta = 1 } = options;
const suffix = (0, classificationMetrics_1.getAverageMetricNameSuffix)(options);
const compute = createCachedComputer();
return {
precision: (0, createClassificationMetricEvaluator_1.createClassificationMetricEvaluator)(`precision${suffix}`, "precision", options, compute),
recall: (0, createClassificationMetricEvaluator_1.createClassificationMetricEvaluator)(`recall${suffix}`, "recall", options, compute),
fScore: (0, createClassificationMetricEvaluator_1.createClassificationMetricEvaluator)(`${(0, classificationMetrics_1.formatBetaForMetricName)(beta)}${suffix}`, "fScore", options, compute),
};
}
//# sourceMappingURL=createPrecisionRecallFScoreEvaluators.js.map