Spaces:
Running
Running
Fix: detect and strip Nemotron reasoning narration (model talking to itself) from AI output
Browse files
agents.py
CHANGED
|
@@ -57,6 +57,77 @@ def start_background_load() -> None:
|
|
| 57 |
pass
|
| 58 |
|
| 59 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
def clean_text(text: str) -> str:
|
| 61 |
"""Aggressively strip model cruft: think blocks, AI prefixes, markdown, noise."""
|
| 62 |
if not text or not text.strip():
|
|
@@ -76,6 +147,9 @@ def clean_text(text: str) -> str:
|
|
| 76 |
text = text[:s].strip()
|
| 77 |
break
|
| 78 |
|
|
|
|
|
|
|
|
|
|
| 79 |
# Remove common AI preamble patterns (must be at start of text followed by colon/newline)
|
| 80 |
prefixes_to_strip = [
|
| 81 |
"assistant:", "ai:", "bot:", "response:", "reply:",
|
|
@@ -115,7 +189,8 @@ def clean_text(text: str) -> str:
|
|
| 115 |
|
| 116 |
|
| 117 |
def sanitize_for_display(text: str, max_chars: int = 500) -> str:
|
| 118 |
-
"""Final polish before showing to the player:
|
|
|
|
| 119 |
if not text or not text.strip():
|
| 120 |
return ""
|
| 121 |
text = text.strip()
|
|
|
|
| 57 |
pass
|
| 58 |
|
| 59 |
|
| 60 |
+
def strip_reasoning_narration(text: str) -> str:
|
| 61 |
+
"""Detect and remove model's internal monologue where it repeats
|
| 62 |
+
instructions/processes the prompt before giving the actual answer.
|
| 63 |
+
Nemotron often outputs its reasoning as plain text, e.g.:
|
| 64 |
+
'User wants a single sentence... Output only the sentence. Hold cash.'
|
| 65 |
+
We keep only the actual answer portion."""
|
| 66 |
+
if not text:
|
| 67 |
+
return text
|
| 68 |
+
|
| 69 |
+
# Split into paragraphs (double-newline preferred, single newline as fallback)
|
| 70 |
+
paras = re.split(r'\n\s*\n', text)
|
| 71 |
+
paras = [p.strip() for p in paras if p.strip()]
|
| 72 |
+
if len(paras) <= 1:
|
| 73 |
+
# Try single newlines
|
| 74 |
+
lines = [l.strip() for l in text.split('\n') if l.strip()]
|
| 75 |
+
if len(lines) <= 1:
|
| 76 |
+
return text
|
| 77 |
+
paras = lines
|
| 78 |
+
|
| 79 |
+
if len(paras) <= 1:
|
| 80 |
+
return text
|
| 81 |
+
|
| 82 |
+
# Reasoning markers: phrases the model uses when talking to itself
|
| 83 |
+
reasoning_markers = [
|
| 84 |
+
r'^user\s+(wants|says|asks|is\s|needs|has|gave|provided)',
|
| 85 |
+
r'^the\s+user\s',
|
| 86 |
+
r'^(i\s+)?need\s+to\s',
|
| 87 |
+
r'^(let|let\'s)\s+(me\s+|us\s+)?(think|analyze|consider|check|review|break|figure|process|reason)',
|
| 88 |
+
r'^(we|i)\s+(need|should|must|have\s+to|want)\s',
|
| 89 |
+
r'^output\s+only\s',
|
| 90 |
+
r'^(this|it)\s+(is|seems|appears|looks)\s+(like|to\s+be)',
|
| 91 |
+
r'^(okay|ok|so|alright|well|now|right|hmm|hmmm)[\s,]+',
|
| 92 |
+
r'^the\s+(task|prompt|instruction|request|question)\s',
|
| 93 |
+
r'^(based|given)\s+(on|the)\s',
|
| 94 |
+
r'^respond\s+(with|to|as)\s',
|
| 95 |
+
r'^reply\s+(with|to|as)\s',
|
| 96 |
+
r'^(my|the)\s+(response|reply|answer|output)\s+(should|must|needs|will|is)\s',
|
| 97 |
+
r'^starting\s+portfolio',
|
| 98 |
+
r'^portfolio[\s:]+',
|
| 99 |
+
r'^\d+%\s+cash',
|
| 100 |
+
r'^(total|pnl|sharpe|drawdown)[\s:]+',
|
| 101 |
+
r'^that\'?s\s+\d+\s+sentenc',
|
| 102 |
+
r'^in\s+(ai|the)\s+(insight|chat|advisory)',
|
| 103 |
+
r'^need\s+(to\s+)?be\s+under\s',
|
| 104 |
+
r'^so\s+reply',
|
| 105 |
+
r'^keep\s+in\s+character',
|
| 106 |
+
]
|
| 107 |
+
|
| 108 |
+
# Classify each paragraph as reasoning or answer
|
| 109 |
+
results = []
|
| 110 |
+
for para in paras:
|
| 111 |
+
plow = para.lower().strip()
|
| 112 |
+
is_reasoning = False
|
| 113 |
+
for pattern in reasoning_markers:
|
| 114 |
+
if re.search(pattern, plow):
|
| 115 |
+
is_reasoning = True
|
| 116 |
+
break
|
| 117 |
+
results.append((para, is_reasoning))
|
| 118 |
+
|
| 119 |
+
# If the first paragraph is reasoning, take the last non-reasoning paragraph
|
| 120 |
+
if results and results[0][1]:
|
| 121 |
+
for para, is_r in reversed(results):
|
| 122 |
+
if not is_r:
|
| 123 |
+
return para.strip()
|
| 124 |
+
# All paragraphs look like reasoning — take the last one since it's
|
| 125 |
+
# most likely the answer (model often ends with the actual response)
|
| 126 |
+
return results[-1][0].strip()
|
| 127 |
+
|
| 128 |
+
return text
|
| 129 |
+
|
| 130 |
+
|
| 131 |
def clean_text(text: str) -> str:
|
| 132 |
"""Aggressively strip model cruft: think blocks, AI prefixes, markdown, noise."""
|
| 133 |
if not text or not text.strip():
|
|
|
|
| 147 |
text = text[:s].strip()
|
| 148 |
break
|
| 149 |
|
| 150 |
+
# Strip reasoning narration (model talking to itself)
|
| 151 |
+
text = strip_reasoning_narration(text)
|
| 152 |
+
|
| 153 |
# Remove common AI preamble patterns (must be at start of text followed by colon/newline)
|
| 154 |
prefixes_to_strip = [
|
| 155 |
"assistant:", "ai:", "bot:", "response:", "reply:",
|
|
|
|
| 189 |
|
| 190 |
|
| 191 |
def sanitize_for_display(text: str, max_chars: int = 500) -> str:
|
| 192 |
+
"""Final polish before showing to the player: full clean + truncate."""
|
| 193 |
+
text = clean_text(text)
|
| 194 |
if not text or not text.strip():
|
| 195 |
return ""
|
| 196 |
text = text.strip()
|