mongodb-rag-core
Version:
Common elements used by MongoDB Chatbot Framework components.
32 lines • 1.4 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.f1AtK = void 0;
const assertKIsValid_1 = require("./assertKIsValid");
const precisionAtK_1 = require("./precisionAtK");
const recallAtK_1 = require("./recallAtK");
/**
Calculate F1@K.
The F1-score is the harmonic mean of Precision@K and Recall@K.
It provides a balance between precision and recall.
@param relevantItems - List of relevant items
@param retrievedItems - List of retrieved items
@param matchFunc - Function to compare items for equality
@param k - Value of k (top-k results to consider)
@returns F1@k
*/
function f1AtK(relevantItems, retrievedItems, matchFunc, k = 5) {
(0, assertKIsValid_1.assertKIsValid)(k);
// Calculate precision@k using the existing function
const precision = (0, precisionAtK_1.precisionAtK)(relevantItems, retrievedItems, matchFunc, k);
// Calculate recall@k using the existing function
const recall = (0, recallAtK_1.recallAtK)(relevantItems, retrievedItems, matchFunc, k);
// If both precision and recall are 0, F1 is undefined (avoid division by zero)
if (precision === 0 && recall === 0) {
return 0;
}
// Calculate the F1-score as the harmonic mean of precision and recall
const f1 = (2 * (precision * recall)) / (precision + recall);
return f1;
}
exports.f1AtK = f1AtK;
//# sourceMappingURL=f1AtK.js.map