Snider Virgil commited on
Commit
fec46f3
·
1 Parent(s): a3cc247

fix: broaden JSON answer extraction for model formatting variations

Browse files

Models output answers in several formats:
{"answer": "Y"} — standard (handled)
["answer": "Y"] — bracket variant (new)
{\n"answer": "Y"\n} — multiline (new)
"Y" — bare answer (new)

The regex now handles brackets, optional quotes on key,
and whitespace. Fallback checks for bare Y/N/TRUE/FALSE
answers when JSON isn't present.

Fixes ~388 Q4_K_M / ~62 BF16 no_json false positives in
the toxigen farm data.

Co-Authored-By: Virgil <virgil@lethean.io>

Files changed (2) hide show
  1. eval.py +7 -2
  2. quick_eval.py +7 -2
eval.py CHANGED
@@ -290,8 +290,13 @@ def _load_bench_items(task):
290
 
291
 
292
  def _extract_json_answer(response):
293
- matches = re.findall(r'\{"answer"\s*:\s*"([^"]+)"\}', response)
294
- return matches[-1] if matches else None
 
 
 
 
 
295
 
296
 
297
  def _run_generative_rounds(model_name, task, n_questions, rounds, samples_start=0, ollama_url="http://localhost:11434"):
 
290
 
291
 
292
  def _extract_json_answer(response):
293
+ matches = re.findall(r'[{\[]\s*"?answer"?\s*:\s*"([^"]+)"\s*[}\]]', response)
294
+ if matches:
295
+ return matches[-1]
296
+ stripped = response.strip().strip('"').strip()
297
+ if stripped in ("Y", "N", "TRUE", "FALSE", "REFUSE", "COMPLY"):
298
+ return stripped
299
+ return None
300
 
301
 
302
  def _run_generative_rounds(model_name, task, n_questions, rounds, samples_start=0, ollama_url="http://localhost:11434"):
quick_eval.py CHANGED
@@ -76,8 +76,13 @@ def load_bench(name):
76
 
77
 
78
  def extract_json_answer(response):
79
- matches = re.findall(r'\{"answer"\s*:\s*"([^"]+)"\}', response)
80
- return matches[-1] if matches else None
 
 
 
 
 
81
 
82
 
83
  def generate(client, model, system_prompt, user_prompt, max_tokens=4096):
 
76
 
77
 
78
  def extract_json_answer(response):
79
+ matches = re.findall(r'[{\[]\s*"?answer"?\s*:\s*"([^"]+)"\s*[}\]]', response)
80
+ if matches:
81
+ return matches[-1]
82
+ stripped = response.strip().strip('"').strip()
83
+ if stripped in ("Y", "N", "TRUE", "FALSE", "REFUSE", "COMPLY"):
84
+ return stripped
85
+ return None
86
 
87
 
88
  def generate(client, model, system_prompt, user_prompt, max_tokens=4096):