mongodb-rag-core
Version:
Common elements used by MongoDB Chatbot Framework components.
32 lines (30 loc) • 1.75 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.computeNormalizedLogarithmicQueryEfficiency = void 0;
const assert_1 = require("assert");
/**
Computes a normalized query efficiency score based on the logarithmic relationship
between documents examined and documents returned. The score ranges from 0 to 1, where:
- 1 means perfect efficiency (n_examined = n_returned)
- 0 means full scan with minimal results
- Score decreases logarithmically as examination overhead increases
- Formula: 1 - log((n_examined+eps2)/(n_returned+eps2)) / log((n_total+eps1)/(n_returned+eps2))
- Uses epsilon values for numerical stability and to handle zero-result queries
@returns A score between 0 and 1
*/
function computeNormalizedLogarithmicQueryEfficiency({ nReturned, nExamined, nTotal, }) {
(0, assert_1.strict)(nReturned >= 0 && nExamined >= 0 && nTotal > 0, "Document counts must be non-negative, and total must be > 0");
(0, assert_1.strict)(nExamined >= nReturned, "Documents examined must be >= documents returned");
(0, assert_1.strict)(nTotal >= nExamined, "Total documents must be >= documents examined");
// Epsilon values for numerical stability
const eps1 = 0.1; // Added to nTotal
const eps2 = 0.01; // Added to nReturned and nExamined
// Formula with epsilon smoothing
const efficiency = 1 -
Math.log((nExamined + eps2) / (nReturned + eps2)) /
Math.log((nTotal + eps1) / (nReturned + eps2));
// Clamp between 0 and 1
return Math.max(0, Math.min(1, efficiency));
}
exports.computeNormalizedLogarithmicQueryEfficiency = computeNormalizedLogarithmicQueryEfficiency;
//# sourceMappingURL=computeNormalizedLogarithmicQueryEfficiency.js.map