From f1c5df1541823c4177b9c2efd8de56eb3e309dd3 Mon Sep 17 00:00:00 2001 From: Pinapelz Date: Sun, 26 Apr 2026 21:38:13 -0700 Subject: fix: for hallucination calculation switch to n-gram approach --- server.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/server.py b/server.py index cea2c8a..01c348e 100644 --- a/server.py +++ b/server.py @@ -203,8 +203,10 @@ def is_hallucination(text: str) -> bool: ngrams = [" ".join(clean[i : i + n]) for i in range(len(clean) - n + 1)] top, count = Counter(ngrams).most_common(1)[0] if count >= 3: - print(f"🔴 Hallucination (\'{top}\' x{count}): {text[:60]!r}") - return True + tokens_covered = count * n + if tokens_covered / max(1, len(clean)) > 0.35: + print(f"🔴 Hallucination (\'{top}\' x{count}, covers {tokens_covered}/{len(clean)} tokens): {text[:60]!r}") + return True top, count = Counter(clean).most_common(1)[0] if count >= 4 and count / len(clean) > 0.40: print(f"🔴 Hallucination (\'{top}\' x{count}, {count/len(clean):.0%}): {text[:60]!r}") -- cgit v1.2.3