File size: 10,532 Bytes
b158684 a51b12a b158684 a51b12a b158684 a51b12a b158684 a51b12a b158684 a51b12a b158684 97969cb b158684 a51b12a b158684 a51b12a b158684 a51b12a b158684 a51b12a b158684 a51b12a b158684 a51b12a b158684 a51b12a b158684 97969cb b158684 a51b12a b158684 a51b12a b158684 97969cb b158684 a51b12a b158684 a51b12a b158684 a51b12a b158684 a51b12a b158684 a51b12a b158684 a51b12a b158684 a51b12a b158684 | 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 | import os
import re
import json
import base64
import threading
from pathlib import Path
from typing import Any
import pycountry
# Constants from demo.py
BASE_DIR = Path(".")
HF_TOKEN_PATH = BASE_DIR / "hf_token"
HF_TOKEN = HF_TOKEN_PATH.read_text(encoding="utf-8").strip() or None
if HF_TOKEN is not None:
from huggingface_hub import login
login(token=HF_TOKEN, add_to_git_credential=False)
HF_MODEL = os.environ.get("HF_MODEL", "google/gemma-4-E2B-it")
JAILBREAK_MODEL = os.environ.get("JAILBREAK_MODEL", "DerivedFunction1/xlmr-prompt-injection")
JAILBREAK_THRESHOLD = float(os.environ.get("JAILBREAK_THRESHOLD", "0.65"))
PROMPT_INJECTION_MODEL = os.environ.get(
"PROMPT_INJECTION_MODEL", "protectai/deberta-v3-base-prompt-injection-v2"
)
REFUSAL_LANGUAGE_MODEL = os.environ.get(
"REFUSAL_LANGUAGE_MODEL",
"polyglot-tagger/multilabel-language-identification",
)
SUPPORTED_GEMMA_LANGS = {
"EN", "ES", "FR", "DE", "IT", "PT", "NL",
"DA", "RU", "PL",
"ZH", "JA", "KO", "VI",
"HI", "BN", "TH", "ID", "MS", "MR", "TE", "TA", "GU", "PA",
"AR", "TR", "HE", "SW",
}
SUPPORTED_JAILBREAK_LANGS = {
"EN",
"AR",
"DE",
"ES",
"FR",
"HI",
"IT",
"JA",
"KO",
"NL",
"TH",
"ZH",
}
# Imports for model loading
from transformers import AutoProcessor, Gemma4ForConditionalGeneration, BitsAndBytesConfig, pipeline
# Model loading
print(f"Loading model: {HF_MODEL}")
_processor = AutoProcessor.from_pretrained(HF_MODEL, padding_side="left")
_bnb_config = BitsAndBytesConfig(
load_in_8bit=True,
# llm_int8_enable_fp32_cpu_offload=True,
)
_model = Gemma4ForConditionalGeneration.from_pretrained(
HF_MODEL,
# quantization_config=_bnb_config,
device_map="auto",
)
_GENERATION_CONFIG = {
"max_new_tokens": 8192,
"temperature": 1.2,
"do_sample": True,
"pad_token_id": _processor.tokenizer.eos_token_id,
}
print(f"Loading jailbreak detector: {JAILBREAK_MODEL}")
_jailbreak_pipe = pipeline("text-classification", model=JAILBREAK_MODEL)
print(f"Loading prompt injection detector: {PROMPT_INJECTION_MODEL}")
_prompt_injection_pipe = pipeline("text-classification", model=PROMPT_INJECTION_MODEL)
print(f"Loading refusal language detector: {REFUSAL_LANGUAGE_MODEL}")
_refusal_language_pipe = pipeline("text-classification", model=REFUSAL_LANGUAGE_MODEL)
# Tool call regex and markup stripping (from demo.py)
TOOL_CALL_RE = re.compile(
r"(?:<\|?tool_call\|?>|^)\s*"
r"(?:call:)?(?P<name>[a-zA-Z_][a-zA-Z0-9_\-\s]*?)\s*"
r"(?:\{|\()(?P<args>.*?)(?:\}|\))\s*"
r"(?P<close><\|?tool_call\|?>|<eos>|<end_of_turn>|<turn\|?>|</s>|$)",
re.DOTALL,
)
TOOL_CALL_MARKUP_RE = re.compile(
r"<\|?tool_call\|?>.*?(?:<\|?tool_call\|?>|<eos>|$)",
re.DOTALL,
)
TOOL_RESPONSE_RE = re.compile(
r"<\|?tool_response\|?>.*$",
re.DOTALL,
)
CLEANUP_RE = re.compile(
r"(<\|?turn\|?>|<eos>|</s>|\[REDIRECT\])",
re.DOTALL,
)
THOUGHT_BLOCK_RE = re.compile(
r"<\|?channel\|?>(?:thought\s*)?.*?(?:<channel\|>|$)",
re.DOTALL,
)
QUOTES_RE = re.compile(r"<\|\"\|>")
TOOL_RESPONSE_MARKERS_RE = re.compile(r"<\|?tool_response\|?>", re.DOTALL)
MALFORMED_TOOL_TAIL_RE = re.compile(r"(<\|?tool_call(?:\|)?$|<\|?$|<\|?\?$)")
def _strip_tool_call_markup(text: str) -> str:
cleaned = (text or "").replace("\r", "").strip()
if not cleaned:
return ""
cleaned = QUOTES_RE.sub('"', cleaned)
cleaned = THOUGHT_BLOCK_RE.sub("", cleaned)
cleaned = TOOL_CALL_MARKUP_RE.sub("", cleaned)
cleaned = TOOL_RESPONSE_RE.sub("", cleaned)
# Remove various special tokens and the REDIRECT token if present
cleaned = CLEANUP_RE.sub("", cleaned)
return cleaned.strip()
def _clean_tool_text(text: str) -> str:
cleaned = _strip_tool_call_markup(text)
if not cleaned:
return ""
cleaned = TOOL_RESPONSE_MARKERS_RE.sub("", cleaned)
return cleaned.strip()
def _strip_trailing_malformed_tool_tokens(text: str) -> str:
cleaned = (text or "").strip()
while cleaned:
if MALFORMED_TOOL_TAIL_RE.search(cleaned):
cleaned = cleaned[:-1].rstrip()
continue
break
return cleaned
def _clean_language_detector_text(text: str) -> str:
cleaned = []
for ch in str(text or ""):
if ch.isalpha() or ch.isspace():
cleaned.append(ch)
else:
cleaned.append(" ")
return " ".join("".join(cleaned).split())
def detect_jailbreak(text: str) -> dict:
"""Return detector metadata for a user message."""
result = _jailbreak_pipe(text, truncation=True, max_length=512)[0]
label = str(result.get("label", "")).lower()
score = float(result.get("score", 0.0))
unsafe_score = score if label == "unsafe" else (1.0 - score if label == "safe" else score)
return {
"score": unsafe_score,
"blocked": unsafe_score >= JAILBREAK_THRESHOLD,
"predicted_label": label,
}
def detect_prompt_injection(text: str) -> dict:
"""Return detector metadata for a user message using the prompt injection model."""
result = _prompt_injection_pipe(text, truncation=True, max_length=512)[0]
label = str(result.get("label", "")).lower()
score = float(result.get("score", 0.0))
# Assuming 'INJECTION' is the unsafe label for this model
unsafe_score = (
score if label.lower() == "injection" else (1.0 - score if label == "safe" else score)
)
return {
"score": unsafe_score,
"blocked": unsafe_score >= JAILBREAK_THRESHOLD, # Reusing JAILBREAK_THRESHOLD for consistency
"predicted_label": label,
}
def detect_refusal_language(text: str) -> str:
cleaned_text = _clean_language_detector_text(text)
result = _refusal_language_pipe(cleaned_text, truncation=True, max_length=512)[0]
label = str(result.get("label", "")).upper().strip()
normalized = _normalize_language_label(label)
if normalized in SUPPORTED_GEMMA_LANGS:
return normalized
return "EN"
def detect_preferred_language(text: str) -> str:
cleaned_text = _clean_language_detector_text(text)
result = _refusal_language_pipe(cleaned_text, truncation=True, max_length=512)[0]
label = str(result.get("label", "")).upper().strip()
normalized = _normalize_language_label(label)
return normalized or "EN"
def _normalize_language_label(label: str) -> str:
cleaned = str(label or "").strip()
if not cleaned:
return ""
upper = cleaned.upper()
if upper in SUPPORTED_GEMMA_LANGS:
return upper
lowered = cleaned.lower()
lang = pycountry.languages.get(alpha_2=lowered)
if lang is None and len(lowered) == 3:
lang = pycountry.languages.get(alpha_3=lowered)
if lang is None:
try:
lang = pycountry.languages.lookup(cleaned)
except LookupError:
lang = None
if lang is None:
return upper
alpha_2 = getattr(lang, "alpha_2", None)
if alpha_2:
return str(alpha_2).upper()
alpha_3 = getattr(lang, "alpha_3", None)
if alpha_3:
return str(alpha_3).upper()
return upper
def _sanitize_display_text(text: str, system_prompt: str | None = None) -> str:
cleaned = _clean_tool_text(text)
if not cleaned:
return ""
# New logic to handle [{'text': "...", 'type': 'text'}] format
try:
parsed_json = json.loads(cleaned)
if (
isinstance(parsed_json, list)
and len(parsed_json) > 0
and isinstance(parsed_json[0], dict)
and "text" in parsed_json[0]
):
return parsed_json[0]["text"].strip()
except json.JSONDecodeError:
pass # Not a JSON string, proceed with normal text processing
return cleaned.strip()
# These imports are needed for generate_response and generate_response_stream
# They are imported here to avoid circular dependencies with demo.py
from bob_resources import (
connect,
validate,
skip,
clarify_intent,
store_policy,
store_information,
store_app_website,
food_safety_endpoint,
legal_endpoint,
emergency_crisis,
apply_discount,
loyalty_program,
competitor_mentions,
take_order
)
def generate_response(
messages: list,
system_prompt: str,
enable_thinking: bool = False,
) -> str:
full = [{"role": "system", "content": system_prompt}] + messages
full.append({"role": "assistant", "content": ""})
inputs = _processor.apply_chat_template(
full,
tools=[connect, validate, skip, clarify_intent, store_policy,
store_information, store_app_website, food_safety_endpoint, legal_endpoint,
emergency_crisis, apply_discount, loyalty_program, competitor_mentions, take_order],
tokenize=True,
return_dict=True,
return_tensors="pt",
add_generation_prompt=True,
enable_thinking=enable_thinking,
).to(_model.device)
with __import__("torch").no_grad():
out = _model.generate( # pyright: ignore[reportAttributeAccessIssue]
**inputs,
**_GENERATION_CONFIG,
)
new_tokens = out[0][inputs["input_ids"].shape[1]:]
return _processor.decode(new_tokens, skip_special_tokens=True).strip()
def generate_response_stream(
messages: list,
system_prompt: str,
enable_thinking: bool = False,
):
full = [{"role": "system", "content": system_prompt}] + messages
inputs = _processor.apply_chat_template(
full,
tools=[connect, validate, skip, clarify_intent, store_policy,
store_information, store_app_website, food_safety_endpoint, legal_endpoint,
emergency_crisis, apply_discount, loyalty_program, competitor_mentions, take_order],
tokenize=True,
return_dict=True,
return_tensors="pt",
add_generation_prompt=True,
enable_thinking=enable_thinking,
).to(_model.device)
from transformers import TextIteratorStreamer
streamer = TextIteratorStreamer(_processor.tokenizer, skip_prompt=True, skip_special_tokens=False)
thread = threading.Thread(
target=_model.generate, # pyright: ignore[reportAttributeAccessIssue]
kwargs={
**inputs,
**_GENERATION_CONFIG,
"streamer": streamer,
},
daemon=True,
)
thread.start()
generated = ""
for chunk in streamer:
generated += chunk
yield chunk # Yield only the new delta chunk
thread.join()
|