feat: RAG 混合检索可测核心(BM25 / RRF / 去重 / 降级,75 项离线单测)
This commit is contained in:
@@ -0,0 +1,154 @@
|
||||
"""混合检索编排单测 —— 整条链路,纯内存,不建索引、不加载模型。
|
||||
|
||||
生产版`search_hybrid`(kb_index_lib.py:1261-1354)里向量那一路直连
|
||||
ChromaDB、BM25 那一路从向量库取语料,两路都绑索引,所以在生产里
|
||||
**整条链路没有测试覆盖**。这里把它变成纯参数,于是能测。
|
||||
|
||||
覆盖的五条都是生产里真实存在、且错了不报错的分支:
|
||||
1. 一路失败只丢一路,另一路仍出结果
|
||||
2. 两路都失败 → 空列表,不抛异常
|
||||
3. 候选池大小必须覆盖 k+offset(分页翻页时才能拿到完整候选)
|
||||
4. 无标题笔记的去重退路
|
||||
5. 精排发生在分页之前
|
||||
"""
|
||||
|
||||
import unittest
|
||||
|
||||
from ragretrieval.pipeline import RERANK_TOP_N, hybrid_fuse
|
||||
from ragretrieval.rerank import keyword_scorer
|
||||
|
||||
DOCS = [
|
||||
"强平机制:亏损达维持保证金线强制平仓", # 0
|
||||
"标记价格:用指数价格加权,避免插针爆仓", # 1
|
||||
"pytest fixture 用法:scope 与 yield", # 2
|
||||
"资金费率:永续合约锚定现货的手段", # 3
|
||||
]
|
||||
|
||||
|
||||
def vec_pool(*specs):
|
||||
"""specs: (id, file, heading, text)"""
|
||||
return [
|
||||
{"id": i, "file": f, "heading": h, "text": t, "similarity": 0.9}
|
||||
for i, f, h, t in specs
|
||||
]
|
||||
|
||||
|
||||
class TestHybridFuse(unittest.TestCase):
|
||||
def test_both_paths_produce_results(self):
|
||||
out = hybrid_fuse("强平", vector_pool=vec_pool(("v1", "a.md", "A", "强平相关")),
|
||||
bm25_docs=DOCS, k=5)
|
||||
self.assertGreater(len(out), 0)
|
||||
|
||||
def test_bm25_finds_cjk_term(self):
|
||||
"""纯 BM25 路径应能命中中文术语——这正是向量之外要 BM25 的原因。"""
|
||||
out = hybrid_fuse("资金费率", vector_pool=[], bm25_docs=DOCS, k=5)
|
||||
self.assertTrue(any("资金费率" in i["text"] for i in out))
|
||||
|
||||
def test_vector_only(self):
|
||||
pool = vec_pool(("v1", "a.md", "A", "强平"), ("v2", "b.md", "B", "强平二号"))
|
||||
out = hybrid_fuse("强平", vector_pool=pool, bm25_docs=None, k=5)
|
||||
self.assertEqual(len(out), 2)
|
||||
self.assertTrue(all(i["source_of"] == "vector" for i in out))
|
||||
|
||||
def test_bm25_only_marks_source(self):
|
||||
out = hybrid_fuse("pytest", vector_pool=[], bm25_docs=DOCS, k=5)
|
||||
self.assertTrue(all(i["source_of"] == "bm25" for i in out))
|
||||
|
||||
def test_fusion_score_present(self):
|
||||
out = hybrid_fuse("强平", bm25_docs=DOCS, k=5)
|
||||
self.assertIn("fusion", out[0])
|
||||
|
||||
def test_agreement_marked_both(self):
|
||||
"""同一个 id 两路都命中 → source_of=both。
|
||||
|
||||
bm25_ids 必须显式给出:RRF 靠身份键对齐两路,不给键则同一条内容
|
||||
被算成两条独立结果,both 信号直接丢失且不报错。
|
||||
"""
|
||||
pool = vec_pool(("blk0", "doc-0", "doc-0", DOCS[0]))
|
||||
out = hybrid_fuse(
|
||||
"强平", vector_pool=pool, bm25_docs=DOCS,
|
||||
bm25_ids=["blk0", "blk1", "blk2", "blk3"], k=5,
|
||||
)
|
||||
both = [i for i in out if i["source_of"] == "both"]
|
||||
self.assertTrue(both, "两路都命中时应标 both")
|
||||
self.assertEqual(both[0]["id"], "blk0")
|
||||
|
||||
def test_misaligned_ids_lose_both_signal(self):
|
||||
"""反例锁定:id 对不上时 both 永远不出现——这是缺陷不是特性。"""
|
||||
pool = vec_pool(("v-mismatch", "doc-0", "doc-0", DOCS[0]))
|
||||
out = hybrid_fuse("强平", vector_pool=pool, bm25_docs=DOCS, k=5)
|
||||
self.assertEqual([i for i in out if i["source_of"] == "both"], [])
|
||||
|
||||
def test_bm25_ids_length_mismatch_raises(self):
|
||||
with self.assertRaises(ValueError):
|
||||
hybrid_fuse("x", bm25_docs=DOCS, bm25_ids=["a"])
|
||||
|
||||
def test_empty_everything(self):
|
||||
self.assertEqual(hybrid_fuse("x", vector_pool=[], bm25_docs=None), [])
|
||||
|
||||
def test_bm25_no_match_returns_empty(self):
|
||||
"""BM25 侧零命中项必须被滤掉,不能塞进融合池污染排名。"""
|
||||
out = hybrid_fuse("zzzz不存在的词", vector_pool=[], bm25_docs=DOCS, k=5)
|
||||
self.assertEqual(out, [])
|
||||
|
||||
|
||||
class TestDegradation(unittest.TestCase):
|
||||
def test_no_vector_still_works(self):
|
||||
out = hybrid_fuse("强平", vector_pool=None, bm25_docs=DOCS, k=5)
|
||||
self.assertGreater(len(out), 0)
|
||||
|
||||
def test_no_bm25_still_works(self):
|
||||
out = hybrid_fuse("强平", vector_pool=vec_pool(("v", "a.md", "A", "x")),
|
||||
bm25_docs=None, k=5)
|
||||
self.assertEqual(len(out), 1)
|
||||
|
||||
def test_pool_size_covers_pagination(self):
|
||||
"""翻页到 offset 较大时,仍必须能取满 k 条。"""
|
||||
docs = [f"强平文档{i} 内容" for i in range(20)]
|
||||
out = hybrid_fuse("强平", bm25_docs=docs, k=3, offset=15)
|
||||
self.assertEqual(len(out), 3)
|
||||
|
||||
def test_rerank_before_pagination(self):
|
||||
"""精排看的是整个候选池;若先分页再精排,只有前 k 个能参与排序。"""
|
||||
pool = vec_pool(*[(f"v{i}", f"f{i}.md", f"H{i}", f"内容{i}") for i in range(30)])
|
||||
out = hybrid_fuse("关键词", vector_pool=pool, bm25_docs=None, k=3,
|
||||
rerank=True, rerank_scorer=keyword_scorer)
|
||||
self.assertEqual(len(out), 3)
|
||||
self.assertIn("rerank_score", out[0])
|
||||
|
||||
def test_rerank_disabled_is_passthrough(self):
|
||||
pool = vec_pool(("v1", "a.md", "A", "强平"))
|
||||
out = hybrid_fuse("强平", vector_pool=pool, bm25_docs=None, k=5, rerank=False)
|
||||
self.assertNotIn("rerank_score", out[0])
|
||||
|
||||
def test_no_heading_note_not_collapsed(self):
|
||||
"""无标题条目应保留多条,不被heading 去重压成 1 条。"""
|
||||
pool = vec_pool(("v1", "plain.md", "(无标题)", "甲"),
|
||||
("v2", "plain.md", "(无标题)", "乙"))
|
||||
out = hybrid_fuse("x", vector_pool=pool, bm25_docs=None, k=5)
|
||||
self.assertEqual(len(out), 2)
|
||||
|
||||
def test_dedupe_disabled(self):
|
||||
pool = vec_pool(("v1", "a.md", "H", "甲"), ("v2", "a.md", "H", "乙"))
|
||||
out = hybrid_fuse("x", vector_pool=pool, bm25_docs=None, k=5, dedupe=False)
|
||||
self.assertEqual(len(out), 2)
|
||||
|
||||
def test_constants_match_production(self):
|
||||
self.assertEqual(RERANK_TOP_N, 20)
|
||||
|
||||
|
||||
class TestDeterminism(unittest.TestCase):
|
||||
def test_repeated_runs_identical(self):
|
||||
"""评测必须可复跑——同样输入给同样结果,否则回归对比没有意义。"""
|
||||
a = hybrid_fuse("强平", bm25_docs=DOCS, k=5)
|
||||
b = hybrid_fuse("强平", bm25_docs=DOCS, k=5)
|
||||
self.assertEqual(a, b)
|
||||
|
||||
def test_input_docs_not_mutated(self):
|
||||
docs = list(DOCS)
|
||||
hybrid_fuse("强平", bm25_docs=docs, k=5)
|
||||
self.assertEqual(docs, DOCS)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user