fix(llm): search returns each citation label once — the same FR paragraph indexed under two bib items (2019-16041, 2024-14828 each have two rule items) showed twice; keep the closest hit
Some checks failed
CI / lint (push) Successful in 46s
CI / notebooks-smoke (push) Successful in 1m46s
Deploy / notebooks (push) Has been skipped
Deploy / zotero (push) Has been skipped
Deploy / docs (push) Has been skipped
Deploy / api (push) Has been skipped
CI / test (push) Failing after 2m45s
Deploy / llm (push) Has been skipped
Deploy / mc (push) Has been skipped
Infra CI / zotero (push) Successful in 16s
Infra CI / docs (push) Successful in 1m49s
Infra CI / notebooks (push) Successful in 3m23s
Infra CI / api (push) Successful in 1m19s
Infra CI / mc (push) Failing after 40s
Infra CI / llm (push) Successful in 55s
Deploy / report (push) Successful in 15s
Harden / build-scan-report (push) Failing after 24m52s
Zotero Sync / zotero-sync (push) Successful in 1m48s
Notebooks Integration / notebooks-integration (push) Successful in 8m17s
LLM Golden / llm-golden (push) Successful in 4m3s
Renovate / renovate (push) Successful in 15s
Package Supply Chain / pkg-supply-chain (push) Successful in 1m14s
Some checks failed
CI / lint (push) Successful in 46s
CI / notebooks-smoke (push) Successful in 1m46s
Deploy / notebooks (push) Has been skipped
Deploy / zotero (push) Has been skipped
Deploy / docs (push) Has been skipped
Deploy / api (push) Has been skipped
CI / test (push) Failing after 2m45s
Deploy / llm (push) Has been skipped
Deploy / mc (push) Has been skipped
Infra CI / zotero (push) Successful in 16s
Infra CI / docs (push) Successful in 1m49s
Infra CI / notebooks (push) Successful in 3m23s
Infra CI / api (push) Successful in 1m19s
Infra CI / mc (push) Failing after 40s
Infra CI / llm (push) Successful in 55s
Deploy / report (push) Successful in 15s
Harden / build-scan-report (push) Failing after 24m52s
Zotero Sync / zotero-sync (push) Successful in 1m48s
Notebooks Integration / notebooks-integration (push) Successful in 8m17s
LLM Golden / llm-golden (push) Successful in 4m3s
Renovate / renovate (push) Successful in 15s
Package Supply Chain / pkg-supply-chain (push) Successful in 1m14s
This commit is contained in:
@@ -102,6 +102,23 @@ def _theme_ok(md: dict[str, Any], theme: str) -> bool:
|
||||
return theme in str(md.get("themes", "")).split(",")
|
||||
|
||||
|
||||
def dedupe_by_label(hits: list[dict]) -> list[dict]:
|
||||
"""One result per citation label, keeping the closest: the same FR
|
||||
paragraph can be indexed under two bib items that hold the same rule
|
||||
document, and the same chunk can be reached through two collections;
|
||||
a reader wants each cite once."""
|
||||
best: dict[str, dict] = {}
|
||||
for h in hits:
|
||||
key = h.get("label") or h.get("id") or ""
|
||||
if not key:
|
||||
best[id(h)] = h # type: ignore[index]
|
||||
continue
|
||||
cur = best.get(key)
|
||||
if cur is None or float(h.get("distance", 0)) < float(cur.get("distance", 0)):
|
||||
best[key] = h
|
||||
return list(best.values())
|
||||
|
||||
|
||||
def _hit_to_result(doc: Any, distance: float) -> dict:
|
||||
md = {k: str(v) for k, v in (doc.metadata or {}).items()}
|
||||
result = as_source(md, doc.page_content, _similarity(float(distance)))
|
||||
@@ -157,6 +174,7 @@ def search(
|
||||
if not _theme_ok(doc.metadata or {}, theme):
|
||||
continue
|
||||
hits.append(_hit_to_result(doc, distance))
|
||||
hits = dedupe_by_label(hits)
|
||||
hits.sort(key=lambda h: h["distance"])
|
||||
return hits[offset : offset + limit]
|
||||
|
||||
|
||||
@@ -406,3 +406,22 @@ class TestThemeFilter:
|
||||
"$and": [{"doctype": "manual"}, theme_or]
|
||||
}
|
||||
assert _store_filter({"theme": "telehealth"}) == theme_or
|
||||
|
||||
|
||||
class TestDedupeByLabel:
|
||||
def test_keeps_the_closest_per_label(self):
|
||||
from llm.search import dedupe_by_label
|
||||
|
||||
hits = [
|
||||
{"label": "89 FR 62002 ¶5", "distance": 0.30, "item_key": "A"},
|
||||
{"label": "89 FR 62002 ¶5", "distance": 0.25, "item_key": "B"},
|
||||
{"label": "IOM 100-02 ch.10 §30.1.1", "distance": 0.4},
|
||||
{"label": "", "id": "", "distance": 0.5},
|
||||
]
|
||||
out = dedupe_by_label(hits)
|
||||
assert [h.get("label") for h in out] == [
|
||||
"89 FR 62002 ¶5",
|
||||
"IOM 100-02 ch.10 §30.1.1",
|
||||
"",
|
||||
]
|
||||
assert out[0]["item_key"] == "B"
|
||||
|
||||
Reference in New Issue
Block a user