test_retrieval_logging.py
3.37 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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