fix(rag): tokenize CJK text so BM25 retrieval can score non-Latin queries - #146
Open
kuroudo-ai wants to merge 1 commit into
Open
fix(rag): tokenize CJK text so BM25 retrieval can score non-Latin queries#146kuroudo-ai wants to merge 1 commit into
kuroudo-ai wants to merge 1 commit into
Conversation
The retriever default token pattern assumes whitespace-delimited words. Japanese and Chinese are not delimited, so a whole phrase becomes a single token and a query token can never equal a document token: every chunk scores 0.0 while top_k hits are still returned, which surfaces unrelated chunks as if they were search results. Split CJK characters individually and keep the two-or-more-character rule for every other script, so Latin ranking and scores are unchanged.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
retrieve_parsed_documentsilently returns meaningless results for Japanese and Chinese queries. Every chunk scores0.0, buttop_khits are still returned, so unrelated chunks come back looking like search results. Nothing raises.On
master:This is the failure mode that matters most for a RAG pipeline: the retriever reports success, and an unrelated chunk is handed to the model as evidence.
Cause
BM25Retriever.from_defaultsdefaults totoken_pattern=r"(?u)\b\w\w+\b", which assumes whitespace-delimited words. Japanese and Chinese are not delimited, so a phrase such as国際標準化に関する動向becomes one token and the query token国際標準化never equals a document token.Fix
Pass a token pattern that splits CJK characters individually and leaves every other script on the existing two-or-more-character rule:
Because the alternation excludes CJK from the first branch only, Latin, Cyrillic and other alphabets tokenize exactly as before.
skip_stemming=Truewas considered and deliberately not used: it degrades English retrieval while adding no improvement for CJK.Verification
Added
ScriptAwareRetrievalTestscovering Japanese, Chinese and Latin. Removing the fix fails the Japanese and Chinese cases while the Latin case keeps passing — the tests fail for the reason they claim to.bash scripts/verify.shpasses: 429 passed, total coverage 84.78%.Measured retrieval quality on a mixed-language corpus:
国際標準化人材育成Latin-only corpus, four queries compared before/after: identical scores and identical ranking.
One caveat worth stating explicitly: on a mixed-language corpus, English scores do move (for example 1.056 → 2.068). That is not a regression in tokenization — CJK chunks now yield more tokens, which changes average document length and IDF for the whole corpus, and BM25 scores are relative to those statistics. Ranking is unaffected, and the Latin-only comparison above isolates the tokenizer itself.