MrNK2107 commited on
Commit
3bbbd8a
·
1 Parent(s): b936742

fix(guardrails): use regex for unicode-aware Devanagari tokenization

Browse files
Files changed (1) hide show
  1. app/guardrails.py +6 -2
app/guardrails.py CHANGED
@@ -1,9 +1,12 @@
1
  import re
 
2
  from typing import List, Optional, Tuple
3
 
4
  from app.config import settings
5
  from app.schemas import RetrievedContext
6
 
 
 
7
 
8
  UNSAFE_PATTERNS = [
9
  r"\bmake a bomb\b",
@@ -88,8 +91,8 @@ def grounding_check(answer: str, contexts: List[RetrievedContext]) -> bool:
88
  context_text = " ".join(c.text.lower() for c in contexts)
89
 
90
  answer_tokens = {
91
- t for t in re.findall(r"\w+", answer.lower())
92
- if len(t) > 3
93
  }
94
 
95
  if not answer_tokens:
@@ -99,3 +102,4 @@ def grounding_check(answer: str, contexts: List[RetrievedContext]) -> bool:
99
  ratio = supported / max(len(answer_tokens), 1)
100
 
101
  return ratio >= 0.40
 
 
1
  import re
2
+ import regex
3
  from typing import List, Optional, Tuple
4
 
5
  from app.config import settings
6
  from app.schemas import RetrievedContext
7
 
8
+ _WORD_PATTERN = regex.compile(r"[\p{L}\p{M}\p{N}]+", flags=regex.UNICODE)
9
+
10
 
11
  UNSAFE_PATTERNS = [
12
  r"\bmake a bomb\b",
 
91
  context_text = " ".join(c.text.lower() for c in contexts)
92
 
93
  answer_tokens = {
94
+ t for t in _WORD_PATTERN.findall(answer.lower())
95
+ if len(t) >= 2
96
  }
97
 
98
  if not answer_tokens:
 
102
  ratio = supported / max(len(answer_tokens), 1)
103
 
104
  return ratio >= 0.40
105
+