forked from Ikalus1988/MisakaNet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_cross_encoder_rerank.py
More file actions
63 lines (49 loc) 路 2.16 KB
/
Copy pathtest_cross_encoder_rerank.py
File metadata and controls
63 lines (49 loc) 路 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
"""Tests for cross-encoder reranking (Issue #312)."""
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
from misakanet.search.engine import _rank_docs_impl, CachedDoc
from pathlib import Path as P
def _make_doc(title, content="", domain="", status="published"):
d = CachedDoc(
filename=title.replace(" ", "_") + ".md",
filepath=P(f"lessons/{title.replace(' ', '_')}.md"),
content=content,
title=title,
domain=domain,
status=status,
)
d.mtime = 0.0
return d
def test_rerank_flag_accepted():
"""_rank_docs_impl should accept rerank parameter without error."""
docs = [
_make_doc("pip install timeout fix", "pip install network timeout error"),
_make_doc("SSL certificate error", "SSL certificate verification failed"),
]
# Should not raise even without sentence-transformers
result = _rank_docs_impl("timeout", docs, rerank=True)
assert len(result) == 2
def test_rerank_graceful_fallback():
"""Without sentence-transformers, should return same results as BM25."""
docs = [
_make_doc("pip install timeout fix", "pip install network timeout error"),
_make_doc("SSL certificate error", "SSL certificate verification failed"),
_make_doc("proxy configuration", "HTTP proxy setup guide"),
]
result_normal = _rank_docs_impl("timeout", docs, rerank=False)
result_rerank = _rank_docs_impl("timeout", docs, rerank=True)
# Without sentence-transformers installed, rerank should fallback to BM25
# Results should be in same order (or very close)
assert len(result_normal) == len(result_rerank)
# Top result should be the same
assert result_normal[0][1].title == result_rerank[0][1].title
def test_rerank_empty_docs():
"""Empty docs should return empty results."""
result = _rank_docs_impl("timeout", [], rerank=True)
assert result == []
def test_rerank_single_doc():
"""Single doc should work fine."""
docs = [_make_doc("pip install timeout fix", "pip install network timeout error")]
result = _rank_docs_impl("timeout", docs, rerank=True)
assert len(result) == 1