Spaces:
Running on Zero
Running on Zero
Download tests/test_rerank.py from subh-007/allcited-api: direct link, hf CLI and curl.
- Browser
- Download file 2.86 kB
-
https://huggingface.co/spaces/subh-007/allcited-api/resolve/main/tests/test_rerank.py
- Command line
-
hf download hf://spaces/subh-007/allcited-api/tests/test_rerank.py
-
curl -L -o test_rerank.py https://huggingface.co/spaces/subh-007/allcited-api/resolve/main/tests/test_rerank.py
2.86 kB
| from pathlib import Path | |
| from app.pipeline.chunker import chunk_blocks | |
| from app.pipeline.indexes import DocumentIndex | |
| from app.pipeline.parsers.pdf import parse_pdf | |
| from app.pipeline.rerank import Reranker | |
| from app.pipeline.retrieve import hybrid_retrieve | |
| def test_reranker_flag_off_path(): | |
| """Verify that when disabled, the re-ranker safely returns the initial ranking order.""" | |
| fixture_path = Path(__file__).resolve().parent.parent.parent / "eval" / "fixtures" / "handbook.pdf" | |
| assert fixture_path.exists() | |
| blocks = parse_pdf(str(fixture_path), source_name="handbook.pdf") | |
| chunks = chunk_blocks(blocks, doc_id="handbook", target_tokens=250) | |
| doc_index = DocumentIndex() | |
| doc_index.build(chunks) | |
| candidates = hybrid_retrieve(doc_index, "sick leave doctor certificate", top_k=6) | |
| assert len(candidates) >= 2 | |
| reranker = Reranker() | |
| # Flag off: enabled=False | |
| results_off = reranker.rerank("sick leave doctor certificate", candidates, top_k=4, enabled=False) | |
| assert len(results_off) == min(4, len(candidates)) | |
| for i, res in enumerate(results_off): | |
| assert res.chunk_id == candidates[i].chunk.id | |
| assert res.original_rank == i + 1 | |
| assert res.rerank_rank == i + 1 | |
| def test_reranker_ranking_improvement_on_debug_questions(): | |
| """ | |
| Gate check: On the 5 debug questions, reranked top-4 changes >=1 ranking for the better. | |
| """ | |
| fixture_path = Path(__file__).resolve().parent.parent.parent / "eval" / "fixtures" / "handbook.pdf" | |
| assert fixture_path.exists() | |
| blocks = parse_pdf(str(fixture_path), source_name="handbook.pdf") | |
| chunks = chunk_blocks(blocks, doc_id="handbook", target_tokens=250) | |
| doc_index = DocumentIndex() | |
| doc_index.build(chunks) | |
| debug_questions = [ | |
| "What form must be submitted for sick leave longer than two days?", | |
| "How many days of annual leave do employees accrue per calendar year?", | |
| "What are the rules for carrying forward leave into the next year?", | |
| "What is rule 63.7.4 regarding leave encashment on resignation?", | |
| "What are the password security requirements under information technology?", | |
| ] | |
| reranker = Reranker() | |
| ranking_changed_count = 0 | |
| for q in debug_questions: | |
| candidates = hybrid_retrieve(doc_index, q, top_k=10) | |
| reranked = reranker.rerank(q, candidates, top_k=4, enabled=True) | |
| assert len(reranked) >= 1 | |
| # Check if any chunk moved up in rank | |
| for item in reranked: | |
| if item.rerank_rank < item.original_rank: | |
| ranking_changed_count += 1 | |
| break | |
| # Gate requires that across the 5 debug questions, >=1 ranking changes for the better | |
| assert ranking_changed_count >= 1, ( | |
| f"Expected at least 1 ranking to improve via cross-encoder, got {ranking_changed_count}" | |
| ) | |