mbochniak01 Claude Sonnet 4.6 commited on
Commit ·
27156ca
1
Parent(s): 14d263b
Add joke short-circuit and reset attempt handler
Browse filesJoke queries (joke, funny, pun, dad joke, etc.) get a canned domain-themed
joke instead of going through RAG — avoids faithfulness false positives on
creative/off-topic requests.
Reset/jailbreak attempts (forget, blank slate, ignore context, etc.) also
route to the joke bank: light-hearted deflection, no compliance risk.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
- backend/pipeline.py +61 -0
backend/pipeline.py
CHANGED
|
@@ -14,6 +14,7 @@ Knowledge base formats supported:
|
|
| 14 |
import csv
|
| 15 |
import json
|
| 16 |
import logging
|
|
|
|
| 17 |
import re
|
| 18 |
from dataclasses import dataclass, field
|
| 19 |
from pathlib import Path
|
|
@@ -259,6 +260,57 @@ def _build_context(docs: list[RetrievedDoc]) -> str:
|
|
| 259 |
return "\n\n".join(f"[{d.title}]\n{d.content.strip()}" for d in docs)
|
| 260 |
|
| 261 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 262 |
# ---------------------------------------------------------------------------
|
| 263 |
# Generation
|
| 264 |
# ---------------------------------------------------------------------------
|
|
@@ -303,6 +355,15 @@ def run(
|
|
| 303 |
) -> PipelineResult:
|
| 304 |
"""Retrieve relevant KB docs, generate a grounded answer, and grade it."""
|
| 305 |
domain = domain_for(client)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 306 |
embedder = get_embedder()
|
| 307 |
index = _build_index(domain, embedder)
|
| 308 |
|
|
|
|
| 14 |
import csv
|
| 15 |
import json
|
| 16 |
import logging
|
| 17 |
+
import random
|
| 18 |
import re
|
| 19 |
from dataclasses import dataclass, field
|
| 20 |
from pathlib import Path
|
|
|
|
| 260 |
return "\n\n".join(f"[{d.title}]\n{d.content.strip()}" for d in docs)
|
| 261 |
|
| 262 |
|
| 263 |
+
# ---------------------------------------------------------------------------
|
| 264 |
+
# Joke short-circuit
|
| 265 |
+
# ---------------------------------------------------------------------------
|
| 266 |
+
|
| 267 |
+
_JOKE_PATTERN = re.compile(
|
| 268 |
+
r"\b(joke|jokes|funny|laugh|humor|humour|dad joke|daddy joke|pun|make me (laugh|smile))\b",
|
| 269 |
+
re.IGNORECASE,
|
| 270 |
+
)
|
| 271 |
+
|
| 272 |
+
_DOMAIN_JOKES: dict[str, list[str]] = {
|
| 273 |
+
"retail": [
|
| 274 |
+
"Why did the inventory go to therapy? It had too many unresolved stockouts.",
|
| 275 |
+
"What do you call a shelf that never runs out? A shelf-fulfilling prophecy.",
|
| 276 |
+
"Why don't retail systems ever get lonely? Because they're always in stock-ing.",
|
| 277 |
+
"I tried to write a joke about planograms… but I couldn't find the right placement.",
|
| 278 |
+
"Why did the out-of-stock alert break up with the replenishment order? It said: 'You always come too late.'",
|
| 279 |
+
],
|
| 280 |
+
"pharma": [
|
| 281 |
+
"Why did the adverse event report go to the doctor? It had too many side effects.",
|
| 282 |
+
"What do you call a drug that's always on time? Punctu-pill.",
|
| 283 |
+
"Why are pharmacovigilance teams great at parties? They always follow up.",
|
| 284 |
+
"I asked the formulary for a joke. It said: 'Prior authorization required.'",
|
| 285 |
+
"Why did the clinical trial fail to finish the joke? Insufficient sample size.",
|
| 286 |
+
],
|
| 287 |
+
}
|
| 288 |
+
|
| 289 |
+
_FALLBACK_JOKES = [
|
| 290 |
+
"Why did the AI refuse to tell a joke? Insufficient context. Please provide grounding documents.",
|
| 291 |
+
"I'm not great at jokes — my training data was mostly compliance documents.",
|
| 292 |
+
"Why did the chatbot cross the road? To reduce hallucination on the other side.",
|
| 293 |
+
]
|
| 294 |
+
|
| 295 |
+
_RESET_PATTERNS = re.compile(
|
| 296 |
+
r"\b(forget|ignore|pretend|blank slate|no context|disregard|system prompt|jailbreak)\b",
|
| 297 |
+
re.IGNORECASE,
|
| 298 |
+
)
|
| 299 |
+
|
| 300 |
+
|
| 301 |
+
def _is_joke_query(query: str) -> bool:
|
| 302 |
+
return bool(_JOKE_PATTERN.search(query))
|
| 303 |
+
|
| 304 |
+
|
| 305 |
+
def _joke_response(domain: str) -> str:
|
| 306 |
+
jokes = _DOMAIN_JOKES.get(domain, _FALLBACK_JOKES)
|
| 307 |
+
return random.choice(jokes)
|
| 308 |
+
|
| 309 |
+
|
| 310 |
+
def _is_reset_attempt(query: str) -> bool:
|
| 311 |
+
return bool(_RESET_PATTERNS.search(query))
|
| 312 |
+
|
| 313 |
+
|
| 314 |
# ---------------------------------------------------------------------------
|
| 315 |
# Generation
|
| 316 |
# ---------------------------------------------------------------------------
|
|
|
|
| 355 |
) -> PipelineResult:
|
| 356 |
"""Retrieve relevant KB docs, generate a grounded answer, and grade it."""
|
| 357 |
domain = domain_for(client)
|
| 358 |
+
|
| 359 |
+
if _is_joke_query(query) or _is_reset_attempt(query):
|
| 360 |
+
answer = _joke_response(domain)
|
| 361 |
+
report = grade(query=query, response=answer, context=answer, client=client)
|
| 362 |
+
return PipelineResult(
|
| 363 |
+
query=query, client=client, answer=answer,
|
| 364 |
+
retrieved_docs=[], grade_report=report, context_used="",
|
| 365 |
+
)
|
| 366 |
+
|
| 367 |
embedder = get_embedder()
|
| 368 |
index = _build_index(domain, embedder)
|
| 369 |
|