initial commit: retrieval + agent artifacts

This commit is contained in:
Danil Poliakov
2026-10-28 20:44:22 +07:00
commit cf04972e80
24 changed files with 980 additions and 0 deletions
+14
View File
@@ -0,0 +1,14 @@
from raglab.chunking import chunk_markdown
def test_chunking_keeps_heading_trail():
text = "# Title\n\nIntro\n\n## Section\n\nBody text."
chunks = chunk_markdown("doc", text)
assert all(chunk.doc_id == "doc" for chunk in chunks)
assert any("Section" in chunk.title for chunk in chunks)
def test_long_text_is_split_with_overlap():
chunks = chunk_markdown("doc", "# T\n\n" + "word " * 800, target=200, overlap=40)
assert len(chunks) > 1
assert all(len(chunk.text) <= 260 for chunk in chunks)
+17
View File
@@ -0,0 +1,17 @@
from raglab.search import Hit, rrf_fuse
def hit(chunk_id: str) -> Hit:
return Hit(id=chunk_id, doc_id=chunk_id.split(":")[0], title="", text="", score=0.0, source="test")
def test_rrf_prefers_documents_found_by_both_lists():
dense = [hit("a:1"), hit("b:1")]
lexical = [hit("c:1"), hit("a:1")]
fused = rrf_fuse([dense, lexical], limit=3)
assert [item.id for item in fused][0] == "a:1"
def test_rrf_respects_limit():
fused = rrf_fuse([[hit(f"a:{i}") for i in range(5)]], limit=2)
assert len(fused) == 2
+18
View File
@@ -0,0 +1,18 @@
from raglab.metrics import mrr_at_k, ndcg_at_k, recall_at_k
def test_recall_counts_distinct_relevant_docs():
assert recall_at_k(["a", "b", "c"], {"b", "z"}, 3) == 0.5
assert recall_at_k(["a"], {"z"}, 5) == 0.0
def test_mrr_uses_first_relevant_position():
assert mrr_at_k(["x", "a"], {"a"}, 10) == 0.5
assert mrr_at_k(["x"], {"a"}, 10) == 0.0
def test_ndcg_orders_grades():
perfect = ndcg_at_k(["a", "b"], {"a": 2, "b": 1}, 10)
reversed_ = ndcg_at_k(["b", "a"], {"a": 2, "b": 1}, 10)
assert perfect == 1.0
assert reversed_ < perfect