forked from Deen-Bridge/dnb-ai
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintent.py
More file actions
780 lines (711 loc) · 20.3 KB
/
Copy pathintent.py
File metadata and controls
780 lines (711 loc) · 20.3 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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
"""Hierarchical intent classification for Islamic questions (#203).
Why this exists
---------------
The chat path's only notion of question type was the binary ``classify_fiqh``
keyword pre-filter in ``fiqh.py``: a prompt either was a fiqh question or it
was not. That is too coarse to shape retrieval and answer style — a tafsir
explanation request, a comparative fiqh question, an urgent personal ruling,
and a historical query about the early caliphate all looked identical.
This module classifies each prompt along several orthogonal axes, offline and
deterministically (cue lists + word-boundary regexes — zero model calls, zero
network latency), following the same conventions as ``tafsir.py``'s cue
detection:
- **Knowledge domains** — a hierarchical taxonomy whose top-level labels match
the ``domain`` values in ``data/eval/islamic_qa_benchmark.jsonl`` 1:1, so
classifier output can be scored directly against benchmark labels;
- **Response format** — fatwa/ruling, tafsir explanation, comparison,
factual lookup, practical guidance;
- **Urgency** of personal fiqh questions;
- **Orientation** — learning (understanding) vs action (doing);
- **Comparative / historical / meta-methodology** flags;
- **Multi-intent** detection across sentence-like segments;
- **Answer depth** — brief vs standard vs detailed;
- **Academic vs practical** orientation.
Every classification is recorded on ``accuracy_tracker`` so predicted-vs-actual
counts per field accumulate once ground-truth labels arrive from feedback or
the scholar-review queue.
"""
from __future__ import annotations
import logging
import re
import threading
from itertools import combinations
from pydantic import BaseModel, Field
logger = logging.getLogger(__name__)
# ---------------------------------------------------------------------------
# Knowledge-domain taxonomy
# ---------------------------------------------------------------------------
# Top-level keys intentionally mirror data/eval/islamic_qa_benchmark.jsonl's
# ``domain`` vocabulary so benchmark scoring needs no label mapping. Cues are
# matched case-insensitively as substrings, like FIQH_KEYWORDS in fiqh.py.
DOMAIN_CUES: dict[str, tuple[str, ...]] = {
"aqeedah": (
"aqeedah",
"aqida",
"tawhid",
"tawheed",
"tauheed",
"oneness of allah",
"shirk",
"iman",
"pillars of faith",
"articles of faith",
"divine decree",
"al-qadar",
"qadar",
"day of judgment",
"day of resurrection",
"qiyamah",
"barzakh",
"angels",
"attributes of allah",
"asma wa sifat",
"intercession",
"shafa'ah",
"destiny",
"predestination",
),
"fiqh_ibadat": (
"wudu",
"wudhu",
"ghusl",
"tayammum",
"salah",
"salat",
"namaz",
"rak'ah",
"rakah",
"sujud",
"fajr",
"dhuhr",
"zuhr",
"asr",
"maghrib",
"isha",
"tarawih",
"jumu'ah",
"qiblah",
"adhan",
"fasting",
"siyam",
"sawm",
"ramadan",
"itikaf",
"zakat",
"zakah",
"hajj",
"umrah",
"umra",
"tawaf",
"sacrifice",
"udhiyah",
"qurbani",
"halal",
"haram",
"makruh",
"purification",
"taharah",
"menstruation",
"hayd",
"nifas",
),
"fiqh_muamalat": (
"riba",
"usury",
"interest",
"mortgage",
"loan",
"business",
"trade",
"contract",
"insurance",
"crypto",
"cryptocurrency",
"stocks",
"invest",
"investment",
"mortgage",
"rent",
"hire purchase",
"islamic finance",
"sukuk",
"mudarabah",
"musharakah",
"employment",
"salary",
"copyright",
),
"contemporary_issues": (
"crypto",
"cryptocurrency",
"bitcoin",
"nft",
"organ donation",
"vaccine",
"artificial intelligence",
"social media",
"surrogacy",
"euthanasia",
"climate change",
"modern issue",
"contemporary",
"in vitro",
"ivf",
"genetic engineering",
),
"fiqh_munakahat_mirath": (
"marriage",
"nikah",
"divorce",
"talaq",
"khul'",
"khula",
"iddah",
"dowry",
"mahr",
"inheritance",
"mirath",
"wirathat",
"wasyya",
"wasiyya",
"will",
"estate division",
"custody",
"hadanah",
"guardianship",
"polygamy",
"mahram",
),
"ulum_al_quran": (
"tafsir",
"tafseer",
"exegesis",
"asbab al-nuzul",
"asbab al nuzul",
"revelation of",
"revealed",
"makki",
"madani",
"abrogation",
"naskh",
"qira'at",
"recitation",
"preservation of the quran",
"compilation of the quran",
"mufassir",
"occasions of revelation",
"context of the verse",
"context of surah",
),
"mustalah_al_hadith": (
"hadith",
"hadeeth",
"sunnah of the prophet",
"isnad",
"chain of narration",
"matn",
"sahih",
"hasan",
"da'if",
"daeef",
"weak narration",
"authenticity of",
"graded",
"bukhari",
"muslim",
"tirmidhi",
"abu dawud",
"nasai",
"ibn majah",
"muwatta",
"riwayah",
),
"seerah": (
"seerah",
"sirah",
"life of the prophet",
"prophet muhammad",
"prophet's biography",
"migration",
"hijrah",
"hegira",
"battle of badr",
"battle of uhud",
"conquest of makkah",
"meccan period",
"medinan period",
"companions of the prophet",
"year of the elephant",
),
"tarikh_islami": (
"history of islam",
"islamic history",
"historical",
"caliphate",
"caliph",
"khalifa",
"abbasid",
"umayyad",
"ottoman",
"golden age",
"battle of",
"siege of",
"treaty of hudaybiyyah",
"era of",
"century of islam",
"spread of islam",
),
"tasawwuf_adab_akhlaq": (
"akhlaq",
"adab",
"character",
"manners",
"etiquette",
"sincerity",
"ikhlas",
"riya",
"humility",
"patience",
"sabr",
"gratitude",
"shukr",
"tazkiyah",
"purification of the heart",
"spirituality",
"dhikr",
"ihsan",
"good deeds",
"lying",
"backbiting",
"gheebah",
"anger management",
"parents",
"neighbours",
),
"general_islam": (
"islam",
"muslim",
"allah",
"quran",
"five pillars",
"shahada",
"convert",
"revert",
"new muslim",
),
}
# Deterministic priority when picking a primary domain: the more specific
# jurisprudence and science domains outrank the catch-all.
_PRIMARY_DOMAIN_PRIORITY = (
"fiqh_munakahat_mirath",
"fiqh_muamalat",
"fiqh_ibadat",
"ulum_al_quran",
"mustalah_al_hadith",
"aqeedah",
"seerah",
"tarikh_islami",
"tasawwuf_adab_akhlaq",
"contemporary_issues",
"general_islam",
)
# ---------------------------------------------------------------------------
# Response-format cues
# ---------------------------------------------------------------------------
RESPONSE_FORMATS = frozenset({"fatwa", "tafsir_explanation", "comparison", "factual_lookup", "practical_guidance"})
# Evaluated in order; the first matching format wins. Comparison outranks
# fatwa because "compare the rulings on X" wants an analysis, not a verdict.
_RESPONSE_FORMAT_RULES: tuple[tuple[str, tuple[str, ...]], ...] = (
(
"comparison",
(
"difference between",
"differences between",
"compare",
"comparison",
"versus",
" vs ",
"vs.",
"similarities between",
"which is better",
"which is more",
"contrasting",
),
),
(
"fatwa",
(
"fatwa",
"ruling on",
"the ruling",
"is it permissible",
"is it allowed",
"is it haram",
"is it halal",
"is it sinful",
"am i allowed",
"what should i do",
"does it invalidate",
"does it break",
),
),
(
"tafsir_explanation",
(
"tafsir",
"tafseer",
"explain",
"explanation",
"interpret",
"commentary",
"meaning of",
"what does",
"what do",
"significance of",
"why was",
"mufassir",
),
),
(
"practical_guidance",
(
"how do i",
"how to",
"step by step",
"guide me",
"teach me how",
"show me how",
"beginner",
),
),
(
"factual_lookup",
(
"who was",
"who is",
"when was",
"when did",
"where is",
"where did",
"how many",
"what year",
"list of",
"name the",
"define",
"definition of",
),
),
)
# ---------------------------------------------------------------------------
# Facet cues: urgency, orientation, meta, depth, academic register
# ---------------------------------------------------------------------------
URGENCY_CUES: tuple[str, ...] = (
"urgent",
"urgently",
"emergency",
"asap",
"right away",
"immediately",
"time-sensitive",
"time sensitive",
"deadline",
"tonight",
"before sunrise",
"before sunset",
"in a few hours",
"by tomorrow",
)
LEARNING_CUES: tuple[str, ...] = (
"learn about",
"learn more",
"understand",
"study ",
"i want to know",
"help me understand",
"what is the concept",
"explain the concept",
"deeper understanding",
"for my knowledge",
)
ACTION_CUES: tuple[str, ...] = (
"can i",
"could i",
"should i",
"shall i",
"may i",
"am i allowed",
"do i have to",
"must i",
"how do i",
"what should i do",
"i want to ",
"i plan to ",
"i am about to",
"i'm about to",
"is my ",
"does my ",
)
HISTORICAL_CUES: tuple[str, ...] = (
"history of",
"historical",
"historically",
"when was",
"when did",
"who was",
"in the time of",
"during the era",
"century",
"caliphate",
"caliph",
"battle of",
"origins of",
"originated",
"background of",
)
META_CUES: tuple[str, ...] = (
"how do you determine",
"how do you decide",
"how does this assistant",
"how are answers",
"what methodology",
"your methodology",
"which school do you follow",
"are your sources",
"where do you get your",
"how reliable are your",
"usul al-fiqh",
"usul al fiqh",
"principles of jurisprudence",
"how is this classified",
"what criteria",
)
BRIEF_DEPTH_CUES: tuple[str, ...] = (
"briefly",
"in short",
"short answer",
"quick question",
"quick answer",
"one sentence",
"just tell me",
"summarize",
"summarise",
"tldr",
"no details",
)
DETAILED_DEPTH_CUES: tuple[str, ...] = (
"in detail",
"detailed",
"elaborate",
"in depth",
"in-depth",
"comprehensive",
"thoroughly",
"everything about",
"long answer",
"full explanation",
"with examples",
"deep dive",
)
COMPARATIVE_CUES: tuple[str, ...] = (
"difference between",
"differences between",
"compare",
"comparison",
"versus",
" vs ",
"vs.",
"similarities between",
"which is better",
"which is more",
"contrasting",
)
ACADEMIC_CUES: tuple[str, ...] = (
"academic",
"academically",
"for my research",
"my thesis",
"my dissertation",
"research paper",
"term paper",
"scholarly",
"with citations",
"with references",
"cite sources",
"for my exam",
"for my studies",
"university assignment",
"footnotes",
)
PRACTICAL_CUES: tuple[str, ...] = ACTION_CUES
# Segmentation for multi-intent detection: sentence ends, semicolons, and the
# "and also"/"and what about" style joins that signal a second ask.
_SEGMENT_SPLIT = re.compile(
r"[.?;!\n]+"
r"|\band also\b"
r"|\band what about\b"
r"|\balso,?\s+(?:what|how|why|is|are|can|do)\b"
r"|\b(?:and|but)\s+(?:how|why|when|where)\s+(?:do|does|did|is|are|can|should|i)\b",
re.IGNORECASE,
)
_WORD_ONLY = re.compile(r"^[a-z0-9'\-]+$")
_WORD_CACHE: dict[str, re.Pattern[str]] = {}
def _cue_matches(text: str, cue: str) -> bool:
"""Word-boundary match for single-word cues so 'iman' never fires on
'imagine'; plain substring for phrases, like fiqh.py's keyword matcher."""
if " " not in cue and _WORD_ONLY.fullmatch(cue):
pattern = _WORD_CACHE.get(cue)
if pattern is None:
pattern = re.compile(rf"\b{re.escape(cue)}\b")
_WORD_CACHE[cue] = pattern
return pattern.search(text) is not None
return cue in text
def _has_any(text: str, cues: tuple[str, ...]) -> bool:
return any(_cue_matches(text, cue) for cue in cues)
def detect_domains(text: str) -> list[str]:
"""Return every knowledge domain whose cues appear, in taxonomy order."""
return [domain for domain, cues in DOMAIN_CUES.items() if _has_any(text, cues)]
def pick_primary_domain(domains: list[str]) -> str | None:
"""Deterministically choose the most specific domain, if any."""
for candidate in _PRIMARY_DOMAIN_PRIORITY:
if candidate in domains:
return candidate
return None
def detect_response_format(text: str) -> str | None:
"""First matching response format wins; rules are ordered by specificity."""
for fmt, cues in _RESPONSE_FORMAT_RULES:
if _has_any(text, cues):
return fmt
return None
def detect_fiqh_urgency(text: str, domains: list[str], response_format: str | None) -> str:
"""High only for time-sensitive *personal* fiqh questions."""
is_fiqh = any(d.startswith("fiqh_") for d in domains) or response_format == "fatwa"
if is_fiqh and _has_any(text, URGENCY_CUES):
return "high"
return "none"
def detect_orientation(text: str) -> str | None:
"""Learning (understanding) vs action (doing). Learning checked first so
'I want to understand whether I can...' reads as learning."""
if _has_any(text, LEARNING_CUES):
return "learning"
if _has_any(text, ACTION_CUES):
return "action"
return None
def detect_answer_depth(text: str) -> str:
if _has_any(text, BRIEF_DEPTH_CUES):
return "brief"
if _has_any(text, DETAILED_DEPTH_CUES):
return "detailed"
return "standard"
def detect_academic_orientation(text: str) -> str | None:
if _has_any(text, ACADEMIC_CUES):
return "academic"
if _has_any(text, PRACTICAL_CUES):
return "practical"
return None
def is_multi_intent(prompt: str, domains: list[str]) -> bool:
"""True when distinct sentence-like segments carry different domain cues.
A single question spanning two domains ("What is riba?") stays single-
intent; two asks ("What is riba and how do I pray?") is multi-intent.
"""
if len(domains) < 2:
return False
segments = [s.casefold() for s in _SEGMENT_SPLIT.split(prompt) if s and s.strip()]
if len(segments) < 2:
return False
segment_domains = [
frozenset(d for d, cues in DOMAIN_CUES.items() if _has_any(segment, cues)) for segment in segments
]
distinct = [s for s in segment_domains if s]
if len(distinct) < 2:
return False
return any(a != b for a, b in combinations(distinct, 2))
class IntentClassification(BaseModel):
"""Structured result of one prompt's classification."""
domains: list[str] = Field(default_factory=list)
primary_domain: str | None = None
response_format: str | None = None
fiqh_urgency: str = "none" # "none" | "high"
orientation: str | None = None # "learning" | "action" | None
is_comparative: bool = False
is_historical: bool = False
is_meta: bool = False
is_multi_intent: bool = False
answer_depth: str = "standard" # "brief" | "standard" | "detailed"
academic_register: str | None = None # "academic" | "practical" | None
def classify_intent(prompt: str) -> IntentClassification:
"""Classify one prompt along every axis. Never raises on ordinary input."""
text = (prompt or "").casefold()
if not text.strip():
return IntentClassification()
domains = detect_domains(text)
fmt = detect_response_format(text)
return IntentClassification(
domains=domains,
primary_domain=pick_primary_domain(domains),
response_format=fmt,
fiqh_urgency=detect_fiqh_urgency(text, domains, fmt),
orientation=detect_orientation(text),
is_comparative=_has_any(text, COMPARATIVE_CUES),
is_historical=_has_any(text, HISTORICAL_CUES),
is_meta=_has_any(text, META_CUES),
is_multi_intent=is_multi_intent(prompt, domains),
answer_depth=detect_answer_depth(text),
academic_register=detect_academic_orientation(text),
)
# ---------------------------------------------------------------------------
# Accuracy tracking
# ---------------------------------------------------------------------------
TRACKED_FIELDS = (
"primary_domain",
"response_format",
"fiqh_urgency",
"orientation",
"answer_depth",
"register",
)
class IntentAccuracyTracker:
"""Thread-safe accumulator of predicted-vs-actual counts per field.
``record_prediction`` runs on every classification and tracks volume per
primary domain. ``record`` adds a labelled example once a ground truth is
known (feedback, review-queue verdict, benchmark label); accuracy per
field is then simply correct/total from ``snapshot``.
"""
def __init__(self) -> None:
self._lock = threading.Lock()
self._predictions_by_domain: dict[str, int] = {}
self._labelled: dict[str, dict[str, int]] = {}
def record_prediction(self, classification: IntentClassification) -> None:
key = classification.primary_domain or "unclassified"
with self._lock:
self._predictions_by_domain[key] = self._predictions_by_domain.get(key, 0) + 1
def record(self, field: str, predicted: object, actual: object) -> None:
if field not in TRACKED_FIELDS:
raise ValueError(f"Unknown tracked field '{field}'; expected one of {TRACKED_FIELDS}")
with self._lock:
bucket = self._labelled.setdefault(field, {"total": 0, "correct": 0})
bucket["total"] += 1
if predicted == actual:
bucket["correct"] += 1
def snapshot(self) -> dict[str, object]:
with self._lock:
result: dict[str, object] = {
"predictions_by_domain": dict(sorted(self._predictions_by_domain.items())),
"total_predictions": sum(self._predictions_by_domain.values()),
}
accuracy: dict[str, dict[str, float]] = {}
for field, bucket in self._labelled.items():
total = bucket["total"]
correct = bucket["correct"]
accuracy[field] = {
"total": float(total),
"correct": float(correct),
"accuracy": round(correct / total, 4) if total else 0.0,
}
result["accuracy"] = accuracy
return result
def reset(self) -> None:
with self._lock:
self._predictions_by_domain.clear()
self._labelled.clear()
# Process-wide tracker the chat path records into and telemetry reads from.
accuracy_tracker = IntentAccuracyTracker()