-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_citations.py
More file actions
51 lines (37 loc) · 1.59 KB
/
Copy pathtest_citations.py
File metadata and controls
51 lines (37 loc) · 1.59 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
import uuid
from backend.app.rag.citations import (
build_sources,
extract_citations,
validate_citations,
)
from backend.app.rag.retrieval_models import RetrievedChunk
def make_chunk(index: int) -> RetrievedChunk:
return RetrievedChunk(
chunk_id=str(uuid.uuid4()),
document_id=str(uuid.uuid4()),
text=f"text {index}",
title=f"Title {index}",
section_title=f"Section {index}",
source_uri=f"source-{index}.md",
score=1.0 / index,
rank=index,
retrieval_mode="hybrid_rrf",
metadata={},
)
def test_extract_citations_returns_unique_numeric_ids() -> None:
assert extract_citations("Answer [1] and more [2], repeated [1].") == {1, 2}
def test_validate_citations_requires_at_least_one_valid_citation() -> None:
assert validate_citations("Answer [1].", num_sources=2) is True
assert validate_citations("Answer with no source.", num_sources=2) is False
def test_validate_citations_rejects_out_of_range_citations() -> None:
assert validate_citations("Answer [3].", num_sources=2) is False
assert validate_citations("Answer [0].", num_sources=2) is False
def test_build_sources_assigns_backend_controlled_source_ids() -> None:
chunks = [make_chunk(1), make_chunk(2)]
sources = build_sources(chunks)
assert [source.source_id for source in sources] == ["1", "2"]
assert sources[0].title == "Title 1"
assert sources[0].section == "Section 1"
assert sources[0].source_uri == "source-1.md"
assert sources[0].chunk_id == chunks[0].chunk_id
assert sources[0].score == chunks[0].score