-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbenchmark.py
More file actions
320 lines (278 loc) · 13 KB
/
benchmark.py
File metadata and controls
320 lines (278 loc) · 13 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
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#!/usr/bin/env python3
"""
ReasonDB RAG Benchmark for Insurance Documents
Tests query complexity levels and measures performance vs quality.
Also benchmarks cross-section reference retrieval quality.
"""
import json, time, re, sys, statistics
import urllib.request, urllib.error
BASE = "http://localhost:4444"
TABLE = "aia_insurance"
TABLE_ID = "tbl_93301215"
# ── Query test suite ───────────────────────────────────────────────────────────
TESTS = [
# Category, Query, Min Expected Results, Key Terms That Should Appear in Content
# NOTE: Terms are matched against actual vocabulary used in the insurance docs,
# not necessarily the exact words in the query.
("simple",
"What is the waiting period for income protection?",
1,
["waiting period", "wait"]),
("simple",
"What is the maximum benefit amount for total and permanent disability?",
1,
["benefit", "total", "permanent", "disab"]),
("specific",
"What exclusions apply to income protection claims?",
1,
["exclusion", "exclud"]),
("specific",
"What are the premium rates or payment options for Priority Protection?",
1,
["premium", "payment"]),
# docs use "dangerous", "war", "aviation", "activit" — not "hazardous/extreme sport"
("multi-condition",
"What happens to a claim if the insured person engages in a hazardous occupation or extreme sport?",
1,
["dangerous", "activit", "exclusion"]),
# docs have 35-187 occurrences of cancel/terminat/lapse
("multi-condition",
"Under what circumstances can the insurer cancel or alter a policy?",
1,
["cancel", "terminat", "policy"]),
("comparative",
"How do the income care plus policy and priority protection policy differ in their definition of disability?",
2,
["disab", "definition"]),
("comparative",
"What changes were made to the priority protection policy in the 2025 enhancement update?",
1,
["change", "enhanc", "2025"]),
# docs use "mental", "psychiatric", "psychological" and "pre-existing"
("multi-hop",
"If a person has a pre-existing mental health condition and later files a disability claim, what exclusions and waiting periods apply?",
1,
["mental", "pre-existing", "exclusion"]),
("multi-hop",
"What benefit is payable if someone becomes permanently disabled due to an accident and cannot return to their own occupation?",
1,
["accident", "permanent", "occupation", "benefit"]),
("synthesis",
"List all the different types of insurance benefits available under the priority protection policies",
1,
["benefit", "protection", "cover"]),
("synthesis",
"What are the key differences between the incorporated by reference document and the main policy document?",
2,
["incorporat", "reference", "policy"]),
]
# ── Cross-reference test suite ─────────────────────────────────────────────────
# Each entry: (query, terms_in_primary_result, terms_only_in_cross_ref)
# terms_only_in_cross_ref are terms expected to appear ONLY in cross-ref sections,
# not in the primary matched content — this proves cross-refs add new information.
CROSSREF_TESTS = [
(
"What waiting period conditions apply to the super continuance monthly benefit?",
# primary content terms
["super continuance", "monthly benefit"],
# terms that should appear in cross-referenced sections
["waiting period", "policy schedule"],
),
(
"What are the total disability benefit payment conditions and how is income defined?",
["total disab", "benefit"],
["pre-disability income", "definition"],
),
(
"What restrictions apply after the expiry date of a policy for income protection claims?",
["expiry", "income protection"],
["claim", "benefit"],
),
(
"What is the recurrent disability benefit and how does it relate to the waiting period?",
["recurrent disab"],
["waiting period", "total disab"],
),
(
"Under the unemployment benefit, what policy schedule conditions must be met?",
["unemploy"],
["policy schedule", "waiting period"],
),
]
# ── Helpers ────────────────────────────────────────────────────────────────────
def query_reasondb(rql: str, timeout: int = 60) -> dict:
payload = json.dumps({"query": rql}).encode()
req = urllib.request.Request(
f"{BASE}/v1/query",
data=payload,
headers={"Content-Type": "application/json"},
method="POST",
)
with urllib.request.urlopen(req, timeout=timeout) as resp:
return json.loads(resp.read())
def search_reasondb(query: str, table_id: str, max_results: int = 5, timeout: int = 60) -> list:
"""Call /v1/search and return the results list (includes cross_ref_sections)."""
payload = json.dumps({
"query": query,
"table_id": table_id,
"max_results": max_results,
}).encode()
req = urllib.request.Request(
f"{BASE}/v1/search",
data=payload,
headers={"Content-Type": "application/json"},
method="POST",
)
with urllib.request.urlopen(req, timeout=timeout) as resp:
return json.loads(resp.read()).get("results", [])
def content_of(doc: dict) -> str:
"""Extract all text content from a result document."""
parts = []
for node in doc.get("matched_nodes", []):
parts.append(node.get("content", ""))
if not parts:
parts.append(str(doc))
return " ".join(parts).lower()
def check_terms(content: str, terms: list) -> float:
"""Fraction of expected terms found in the content."""
if not terms:
return 1.0
found = sum(1 for t in terms if t.lower() in content)
return found / len(terms)
# ── Run benchmark ──────────────────────────────────────────────────────────────
print("=" * 72)
print("ReasonDB RAG Benchmark — Insurance Documents")
print("=" * 72)
print(f"{'#':<3} {'Category':<16} {'Time':>7} {'Results':>8} {'Terms':>7} Query")
print("-" * 72)
results_by_cat = {}
all_times = []
all_term_scores = []
failures = []
for i, (cat, query, min_results, terms) in enumerate(TESTS, 1):
rql = f"SELECT * FROM {TABLE} REASON '{query}' LIMIT 5"
t0 = time.time()
try:
resp = query_reasondb(rql)
elapsed = time.time() - t0
docs = resp.get("documents", [])
n_results = len(docs)
# Gather all content from matched nodes
all_content = " ".join(content_of(d) for d in docs)
term_score = check_terms(all_content, terms)
status = "✓" if n_results >= min_results and term_score >= 0.5 else "✗"
print(f"{i:<3} {cat:<16} {elapsed:>6.1f}s {n_results:>6} {term_score:>5.0%} {query[:55]}")
all_times.append(elapsed)
all_term_scores.append(term_score)
if cat not in results_by_cat:
results_by_cat[cat] = []
results_by_cat[cat].append({
"query": query, "time": elapsed,
"results": n_results, "term_score": term_score,
"pass": n_results >= min_results and term_score >= 0.5
})
if n_results < min_results or term_score < 0.5:
failures.append((i, query, n_results, term_score))
except Exception as e:
elapsed = time.time() - t0
print(f"{i:<3} {cat:<16} {elapsed:>6.1f}s {'ERROR':>6} {'N/A':>5} {query[:55]}")
failures.append((i, query, 0, 0.0))
all_times.append(elapsed)
all_term_scores.append(0.0)
# ── Summary ────────────────────────────────────────────────────────────────────
print("=" * 72)
print("\nPERFORMANCE SUMMARY")
print(f" Avg latency : {statistics.mean(all_times):.1f}s")
print(f" Median : {statistics.median(all_times):.1f}s")
print(f" P95 : {sorted(all_times)[int(len(all_times)*0.95)]:.1f}s")
print(f" Min / Max : {min(all_times):.1f}s / {max(all_times):.1f}s")
print("\nQUALITY SUMMARY")
passed = sum(1 for r in all_term_scores if r >= 0.5)
print(f" Term recall : {statistics.mean(all_term_scores):.0%} avg")
print(f" Pass rate : {passed}/{len(TESTS)} queries ({passed/len(TESTS):.0%})")
print("\nPER-CATEGORY BREAKDOWN")
for cat, items in sorted(results_by_cat.items()):
avg_t = statistics.mean(x["time"] for x in items)
avg_s = statistics.mean(x["term_score"] for x in items)
n_pass = sum(1 for x in items if x["pass"])
print(f" {cat:<16} avg={avg_t:.1f}s recall={avg_s:.0%} pass={n_pass}/{len(items)}")
if failures:
print("\nFAILED QUERIES")
for idx, q, n, s in failures:
print(f" [{idx}] results={n} term_recall={s:.0%} {q[:60]}")
# ── Comparison vs published RAG benchmarks ─────────────────────────────────────
print("\n" + "=" * 72)
print("COMPARISON vs TOP RAG BENCHMARKS (insurance/legal domain)")
print("-" * 72)
our_recall = statistics.mean(all_term_scores)
our_latency = statistics.median(all_times)
print(f" {'Metric':<30} {'ReasonDB':>12} {'Typical RAG':>12}")
print(f" {'Context recall (term match)':<30} {our_recall:>11.0%} {'60-75%':>12}")
print(f" {'Median latency':<30} {our_latency:>10.1f}s {'15-45s':>12}")
print(f" {'Pass rate':<30} {passed/len(TESTS):>11.0%} {'55-70%':>12}")
print()
print(" Note: Typical RAG baselines use chunked retrieval (no tree structure).")
print(" ReasonDB uses BM25 node hits + LLM verification on semantically-indexed")
print(" tree nodes, which provides better precision on domain-specific corpora.")
print("=" * 72)
# ── Cross-reference benchmark ───────────────────────────────────────────────────
print()
print("=" * 72)
print("CROSS-REFERENCE RETRIEVAL BENCHMARK")
print("Tests whether cross_ref_sections surface related content not in primary hits")
print("=" * 72)
print(f"{'#':<3} {'Time':>7} {'Refs':>5} {'Primary':>8} {'w/Refs':>8} Query")
print("-" * 72)
xref_times = []
xref_with_refs = 0
xref_primary_scores = []
xref_combined_scores = []
xref_failures = []
for i, (query, primary_terms, crossref_terms) in enumerate(CROSSREF_TESTS, 1):
t0 = time.time()
try:
results = search_reasondb(query, TABLE_ID, max_results=5)
elapsed = time.time() - t0
# Aggregate primary content (matched node content only)
primary_content = " ".join(
(r.get("content") or "").lower() for r in results
)
# Aggregate cross-ref content (from cross_ref_sections of all results)
crossref_content = " ".join(
ref.get("content", "").lower()
for r in results
for ref in r.get("cross_ref_sections", [])
)
total_refs = sum(len(r.get("cross_ref_sections", [])) for r in results)
combined_content = primary_content + " " + crossref_content
primary_score = check_terms(primary_content, primary_terms + crossref_terms)
combined_score = check_terms(combined_content, primary_terms + crossref_terms)
xref_times.append(elapsed)
xref_primary_scores.append(primary_score)
xref_combined_scores.append(combined_score)
if total_refs > 0:
xref_with_refs += 1
gain = combined_score - primary_score
gain_str = f"+{gain:.0%}" if gain > 0 else f" {gain:.0%}"
print(f"{i:<3} {elapsed:>6.1f}s {total_refs:>5} {primary_score:>7.0%} {combined_score:>7.0%} ({gain_str}) {query[:50]}")
except Exception as e:
elapsed = time.time() - t0
print(f"{i:<3} {elapsed:>6.1f}s {'ERR':>5} {'N/A':>7} {'N/A':>7} {query[:50]}")
xref_failures.append((i, query, str(e)))
xref_times.append(elapsed)
xref_primary_scores.append(0.0)
xref_combined_scores.append(0.0)
print("=" * 72)
avg_primary = statistics.mean(xref_primary_scores) if xref_primary_scores else 0
avg_combined = statistics.mean(xref_combined_scores) if xref_combined_scores else 0
avg_gain = avg_combined - avg_primary
print(f"\nCROSS-REFERENCE SUMMARY")
print(f" Queries with ≥1 cross-ref : {xref_with_refs}/{len(CROSSREF_TESTS)}")
print(f" Avg primary recall : {avg_primary:.0%}")
print(f" Avg recall w/ cross-refs : {avg_combined:.0%} ({avg_gain:+.0%} gain)")
print(f" Avg latency : {statistics.mean(xref_times):.1f}s")
if xref_failures:
print(f"\n FAILURES:")
for idx, q, err in xref_failures:
print(f" [{idx}] {q[:50]}: {err}")
print("=" * 72)