test_retrieval_logging.py 3.37 KB
from types import SimpleNamespace

from src.rag.retrieval.logs import _candidate_log_items, _chunk_log_items
from src.rag.retrieval.retriever import Candidate


def test_candidate_log_items_include_source_scores_and_dynamic_labels():
    candidate = Candidate(
        child_chunk_id="child-1",
        parent_chunk_id="parent-1",
        file_id="file-1",
        retrieval_type="direct",
        hybrid_score=0.87654,
        fusion_score=0.12345,
        payload={
            "source_name": "source.pdf",
            "chunk_index": 12,
            "page_start": 3,
            "page_end": 4,
            "section_title": "features",
            # 业务标签按 taxonomy 声明动态展开,不再写死 brand/category 两个键
            "brand": "brand-a",
            "business_category": "vehicle",
            "content_kind": "text",
        },
    )

    items = _candidate_log_items([candidate], limit=5)

    assert items == [
        {
            "rank": 1,
            "type": "direct",
            "hybrid": 0.8765,
            "fusion": 0.1235,
            "child": "child-1",
            "parent": "parent-1",
            "file_id": "file-1",
            "source": "source.pdf",
            "chunk_index": 12,
            "page": "3-4",
            "section": "features",
            "kind": "text",
            "brand": "brand-a",
            "business_category": "vehicle",
        }
    ]


def test_chunk_log_items_include_scores_location_and_preview():
    chunk = SimpleNamespace(
        chunk_id="parent-1",
        child_chunk_id="child-1",
        file_id="file-1",
        source_name="source.pdf",
        chunk_index=12,
        page_start=3,
        page_end=3,
        row_start=None,
        row_end=None,
        title_path="features > cockpit",
        section_title="features",
        content_kind="text",
        retrieval_type="direct",
        vector_score=0.45678,
        final_score=0.45678,
        rerank_score=0.81234,
        # 业务标签只在 metadata_json 里——brand_series/business_category 两个 SQL 列已删除
        metadata_json={"brand": "brand-a", "business_category": "vehicle"},
        content="line one\nline two",
    )

    items = _chunk_log_items([chunk], limit=5, include_preview=True)

    assert items == [
        {
            "rank": 1,
            "chunk_id": "parent-1",
            "child": "child-1",
            "file_id": "file-1",
            "source": "source.pdf",
            "chunk_index": 12,
            "page": "3",
            "title": "features > cockpit",
            "section": "features",
            "kind": "text",
            "brand": "brand-a",
            "business_category": "vehicle",
            "type": "direct",
            "vector_score": 0.4568,
            "final_score": 0.4568,
            "rerank_score": 0.8123,
            "chars": 17,
            "preview": "line one line two",
        }
    ]


def test_chunk_log_items_mark_passed_against_rerank_threshold():
    """传入阈值时标注每条是否过阈值——在阈值过滤前记录,看得到被丢弃的片段。"""
    kept = SimpleNamespace(chunk_id="c-keep", rerank_score=0.30, content="x", metadata_json={})
    dropped = SimpleNamespace(chunk_id="c-drop", rerank_score=0.05, content="y", metadata_json={})

    items = _chunk_log_items([kept, dropped], limit=5, rerank_threshold=0.15)

    assert items[0]["passed"] is True
    assert items[1]["passed"] is False