From fea544d5495f3d93dce061e8d9c4248f6c8ea60c Mon Sep 17 00:00:00 2001 From: Adam Treat Date: Tue, 24 Sep 2024 15:12:11 -0400 Subject: [PATCH] Break the weighted average formula out for readability. Signed-off-by: Adam Treat --- gpt4all-chat/src/database.cpp | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/gpt4all-chat/src/database.cpp b/gpt4all-chat/src/database.cpp index 0d4cc5ba9a20..8322be7c07c9 100644 --- a/gpt4all-chat/src/database.cpp +++ b/gpt4all-chat/src/database.cpp @@ -2270,11 +2270,15 @@ QList Database::reciprocalRankFusion(const std::vector &query, const const int bEmbeddingRank = embeddingRanks.value(b, embeddingResults.size() + 1); Q_ASSERT(embeddingRanks.contains(b)); - const float aRRFScore = bmWeight * (1.0f / (fusion_k + aBm25Rank)) + (1.f - bmWeight) * (1.0f / (fusion_k + aEmbeddingRank)); - const float bRRFScore = bmWeight * (1.0f / (fusion_k + bBm25Rank)) + (1.f - bmWeight) * (1.0f / (fusion_k + bEmbeddingRank)); + const float aBm25Score = 1.0f / (fusion_k + aBm25Rank); + const float bBm25Score = 1.0f / (fusion_k + bBm25Rank); + const float aEmbeddingScore = 1.0f / (fusion_k + aEmbeddingRank); + const float bEmbeddingScore = 1.0f / (fusion_k + bEmbeddingRank); + const float aWeightedScore = bmWeight * aBm25Score + (1.f - bmWeight) * aEmbeddingScore; + const float bWeightedScore = bmWeight * bBm25Score + (1.f - bmWeight) * bEmbeddingScore; // Higher RRF score means better ranking, so we use greater than for sorting - return aRRFScore > bRRFScore; + return aWeightedScore > bWeightedScore; } );