File size: 4,636 Bytes
6064cea 06407c6 5f6d20c 06407c6 6064cea 5f6d20c 6064cea 5f6d20c 6064cea 06407c6 6064cea 5f6d20c 6064cea 06407c6 6064cea 06407c6 5f6d20c 6064cea 5f6d20c 6064cea 5f6d20c 6064cea 5f6d20c 6064cea 5f6d20c 6064cea 5f6d20c 6064cea 06407c6 5f6d20c 6064cea 5f6d20c 6064cea 06407c6 5f6d20c 6064cea 5f6d20c 6064cea 06407c6 6064cea 5f6d20c 06407c6 5f6d20c | 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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 | from __future__ import annotations
from sentence_transformers import CrossEncoder
class LegalReranker:
def __init__(
self,
model_name: str = "BAAI/bge-reranker-large"
):
self.model = CrossEncoder(
model_name,
max_length=512
)
# =====================================================
# BUILD RERANK QUERY
# =====================================================
def build_rerank_query(
self,
query: str,
analysis=None
) -> str:
if analysis is None:
return query
parts = [
f"User Query: {query}"
]
if getattr(
analysis,
"offence",
None
):
parts.append(
f"Legal Offence: {analysis.offence}"
)
if getattr(
analysis,
"intent",
None
):
parts.append(
f"Intent: {analysis.intent}"
)
return "\n".join(parts)
# =====================================================
# LEGAL BOOSTING
# =====================================================
def legal_boost(
self,
score: float,
text: str,
analysis=None
) -> float:
if analysis is None:
return score
text = text.lower()
boost = 0.0
offence = getattr(
analysis,
"offence",
None
)
intent = getattr(
analysis,
"intent",
None
)
# offence keyword present
if offence:
if offence.lower() in text:
boost += 0.15
# punishment intent
if intent == "punishment":
if "shall be punished" in text:
boost += 0.25
if "punishment" in text:
boost += 0.10
if "imprisonment" in text:
boost += 0.05
# evidence intent
elif intent == "evidence":
if "evidence" in text:
boost += 0.15
# procedure intent
elif intent == "procedure":
if "procedure" in text:
boost += 0.15
return score + boost
# =====================================================
# RERANK
# =====================================================
def rerank(
self,
query: str,
points,
top_k: int = 5,
analysis=None
):
if not points:
return []
rerank_query = self.build_rerank_query(
query=query,
analysis=analysis
)
pairs = []
point_texts = []
for point in points:
payload = point.payload
text = (
payload.get(
"enriched_text"
)
or payload.get(
"text",
""
)
)
point_texts.append(text)
pairs.append(
(
rerank_query,
text
)
)
scores = self.model.predict(
pairs,
show_progress_bar=False
)
ranked = []
for point, score, text in zip(
points,
scores,
point_texts
):
final_score = self.legal_boost(
score=float(score),
text=text,
analysis=analysis
)
ranked.append(
{
"point": point,
"rerank_score": float(score),
"final_score": float(final_score)
}
)
ranked.sort(
key=lambda x: x["final_score"],
reverse=True
)
results = [
item["point"]
for item in ranked[:top_k]
]
# =============================
# DEBUG LOGGING
# =============================
print("\n" + "=" * 80)
print("RERANK RESULTS")
print("=" * 80)
for rank, item in enumerate(
ranked[:top_k],
start=1
):
point = item["point"]
print(
f"{rank}. "
f"{point.payload.get('chunk_id')} "
f"| rerank={item['rerank_score']:.4f} "
f"| final={item['final_score']:.4f}"
)
print("=" * 80)
return results |