feat(corroboration): halacha matcher + cosine threshold (INV-COR3, X11)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-05-31 18:57:47 +00:00
parent 09eec6a906
commit dbc176ae66
4 changed files with 31 additions and 0 deletions

View File

@@ -25,6 +25,14 @@ def _coerce_treatment(raw: dict) -> str:
return t if t in _VALID_TREATMENT else "mentioned"
def accept_match(best: tuple[str, float] | None, floor: float = config.HALACHA_CORROBORATION_MATCH_FLOOR) -> str | None:
"""Return the halacha_id iff similarity clears the floor (INV-COR3)."""
if not best:
return None
halacha_id, sim = best
return halacha_id if sim >= floor else None
_TREATMENT_PROMPT = """אתה משפטן בכיר. נתון ציטוט של פסק/החלטה קודמים בתוך החלטה מאוחרת.
סווג כיצד ההחלטה המאוחרת **מטפלת** בתקדים המצוטט, לפי אחת מהקטגוריות:
- followed — אימצה והחילה את ההלכה.

View File

@@ -3386,6 +3386,18 @@ async def update_halacha(
return dict(row) if row else None
async def nearest_halacha_for_vector(case_law_id: UUID, vec: list[float]) -> tuple[str, float] | None:
"""Best-matching halacha of `case_law_id` for a context embedding (cosine)."""
pool = await get_pool()
row = await pool.fetchrow(
"SELECT id::text AS id, 1 - (embedding <=> $2) AS sim "
"FROM halachot WHERE case_law_id = $1 AND embedding IS NOT NULL "
"ORDER BY embedding <=> $2 LIMIT 1",
case_law_id, vec,
)
return (row["id"], float(row["sim"])) if row else None
async def search_precedent_library_semantic(
query_embedding: list[float],
practice_area: str = "",