Skip to content

Commit b50c60a

Browse files
committed
fix(studio): preserve rendered chat special tokens during encoding
1 parent c3e23cc commit b50c60a

1 file changed

Lines changed: 11 additions & 1 deletion

File tree

‎studio/backend/core/inference/inference.py‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1217,6 +1217,7 @@ def _generate_chat_response_inner(
12171217
else:
12181218
template_messages = messages
12191219
reasoning_channel_markers_resolved = False
1220+
add_special_tokens = True
12201221
try:
12211222
if not (hasattr(tokenizer, "chat_template") and tokenizer.chat_template):
12221223
raise ValueError(
@@ -1261,6 +1262,8 @@ def _generate_chat_response_inner(
12611262
formatted_prompt = render_result.prompt
12621263
reasoning_channel_markers = render_result.reasoning_channel_markers
12631264
reasoning_channel_markers_resolved = True
1265+
# Chat templates already supply the model-specific special tokens.
1266+
add_special_tokens = False
12641267

12651268
logger.debug(f"Formatted prompt: {formatted_prompt[:200]}...")
12661269
except Exception as e:
@@ -1287,6 +1290,7 @@ def _generate_chat_response_inner(
12871290
reasoning_channel_markers = reasoning_channel_markers,
12881291
reasoning_channel_markers_resolved = reasoning_channel_markers_resolved,
12891292
continued = bool(continue_final_message and trailing_assistant_text(template_messages)),
1293+
add_special_tokens = add_special_tokens,
12901294
)
12911295

12921296
def _generate_vision_response(
@@ -1963,9 +1967,13 @@ def generate_stream(
19631967
reasoning_channel_markers = None,
19641968
reasoning_channel_markers_resolved: bool = False,
19651969
continued: bool = False,
1970+
add_special_tokens: bool = True,
19661971
) -> Generator[str, None, None]:
19671972
"""Generate a streaming text response (text models only).
19681973
1974+
Rendered chat prompts pass add_special_tokens=False; raw prompts keep
1975+
the tokenizer defaults, including BOS insertion for base models.
1976+
19691977
_adapter_state: if not None, the background thread toggles adapters
19701978
before model.generate(), under _generation_lock.
19711979
``presence_penalty`` matches the GGUF sampling path via a logits processor (0 disables it).
@@ -1985,7 +1993,9 @@ def generate_stream(
19851993
tokenizer = getattr(tokenizer, "tokenizer", tokenizer)
19861994

19871995
try:
1988-
inputs = tokenizer(prompt, return_tensors = "pt").to(model.device)
1996+
inputs = tokenizer(
1997+
prompt, return_tensors = "pt", add_special_tokens = add_special_tokens
1998+
).to(model.device)
19891999

19902000
import threading
19912001

0 commit comments

Comments
 (0)