unity4ar commited on
Commit
5f5ab47
·
verified ·
1 Parent(s): fdb54cf

Strip MiniCPM <think>...</think> reasoning tags from generated text

Browse files
Files changed (1) hide show
  1. llm/zerogpu_backend.py +17 -0
llm/zerogpu_backend.py CHANGED
@@ -142,11 +142,28 @@ def _generate_on_gpu(
142
 
143
  new_tokens = outputs[0][inputs["input_ids"].shape[1]:]
144
  text = _tokenizer.decode(new_tokens, skip_special_tokens=True).strip()
 
145
  if json_mode:
146
  text = _extract_json(text)
147
  return text
148
 
149
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
150
  def _extract_json(text: str) -> str:
151
  cleaned = text.strip()
152
  if cleaned.startswith("```"):
 
142
 
143
  new_tokens = outputs[0][inputs["input_ids"].shape[1]:]
144
  text = _tokenizer.decode(new_tokens, skip_special_tokens=True).strip()
145
+ text = _strip_think_tags(text)
146
  if json_mode:
147
  text = _extract_json(text)
148
  return text
149
 
150
 
151
+ _THINK_TAG_RE = None
152
+
153
+
154
+ def _strip_think_tags(text: str) -> str:
155
+ """Remove <think>...</think> blocks (incl. empty ones) that MiniCPM4 and
156
+ other hybrid-reasoning models emit even when /no_think is present in the
157
+ system prompt. Also strips a stray opening/closing tag if unmatched."""
158
+ import re
159
+ global _THINK_TAG_RE
160
+ if _THINK_TAG_RE is None:
161
+ _THINK_TAG_RE = re.compile(r"<think>.*?</think>\s*", re.DOTALL | re.IGNORECASE)
162
+ cleaned = _THINK_TAG_RE.sub("", text)
163
+ cleaned = re.sub(r"</?think>\s*", "", cleaned, flags=re.IGNORECASE)
164
+ return cleaned.strip()
165
+
166
+
167
  def _extract_json(text: str) -> str:
168
  cleaned = text.strip()
169
  if cleaned.startswith("```"):