Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Max BM25 score / BM25 normalization #267

Merged
merged 3 commits into from
Feb 8, 2025
Merged

Max BM25 score / BM25 normalization #267

merged 3 commits into from
Feb 8, 2025

Conversation

svilupp
Copy link
Owner

@svilupp svilupp commented Feb 8, 2025

  • Added BM25 normalization kwarg to RAGTools.jl to enable 0-1 query-specific normalization of BM25 scores for easier filtering and comparison. See ?RT.bm25 and ?RT.max_bm25_score for more information.

Note: this also enables easier reciprocal rank fusion between BM25 and cosine similarity.

Example

documents = [["this", "is", "a", "test"], ["this", "is", "another", "test"], ["foo", "bar", "baz"]]
dtm = document_term_matrix(documents)
query = ["this"]
scores = bm25(dtm, query)
scores_norm = bm25(dtm, query; normalize = true)

## Make it more accurate for your dataset/index
normalize_max_tf = 3 # assume max term frequency is 3 (what is likely for your dataset? depends on chunk size, preprocessing, etc.)
normalize_min_doc_rel_length = minimum([x for x in RT.doc_rel_length(dtm) if x > 0]) |> Float32
scores_norm = bm25(dtm, query; normalize = true, normalize_max_tf, normalize_min_doc_rel_length)

Copy link

codecov bot commented Feb 8, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 92.07%. Comparing base (9b783ec) to head (c221abe).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #267      +/-   ##
==========================================
+ Coverage   92.04%   92.07%   +0.03%     
==========================================
  Files          49       49              
  Lines        4826     4846      +20     
==========================================
+ Hits         4442     4462      +20     
  Misses        384      384              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@svilupp svilupp merged commit 04b70f7 into main Feb 8, 2025
7 checks passed
@svilupp svilupp deleted the max-bm25-score branch February 8, 2025 11:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant