fix: Check response prefix. - #423
Conversation
In case if the model adds <think> at the end of user prompt and only generates </think> end, then the detection goes wrong.
| for cot_initializer, closed_cot_block in settings.chain_of_thought_skips: | ||
| if settings.response_prefix.startswith(cot_initializer): | ||
| settings.response_prefix = closed_cot_block | ||
| if dummy_prompt.endswith( |
There was a problem hiding this comment.
This is too brittle. For example if the chat template ends with <think>\n instead of <think> this logic fails.
There was a problem hiding this comment.
good catch, I think applying rstrip() might be enough for it.
and I also expect this to fix CoT detection for cases like qwen3.5 models (which I almost never saw get properly detected) because qwen3.5 template has:
{%- if add_generation_prompt %}
{{- '<|im_start|>assistant\n' }}
{%- if enable_thinking is defined and enable_thinking is false %}
{{- '<think>\n\n</think>\n\n' }}
{%- else %}
{{- '<think>\n' }}
{%- endif %}
{%- endif %}I'll test it and push if it works...
There was a problem hiding this comment.
rstrip should do the trick in this case, but it's basically a hack. For example, the template could end with </think>\nOkay,, and we're back to zero. The correct approach would be to get the complete text (prompt + response) and then do a regex match. But this is probably not worth the complexity.
At some point we should revisit trying to set enable_thinking to False in the template, which would make the whole prefix detection unnecessary for many models. I've looked into this in the past, but there were several inconsistencies that might have been fixed in the meantime.
Tested
|
Checking for common response prefix...
* Closed Chain-of-Thought block: '</think>\n'
* Rechecking with prefix...got this now for @p-e-w yet I have still not decided what to do with hashes for now. |
In case if the model adds
<think>at the end of user prompt and only generates</think>end in the model response, then the detection goes wrong.LiquidAI/LFM2.5-2.6B generates response like this:
USER_PROMPT + <think>tag added to it at last fromchat_templateviatokenizer.apply_chat_template:For fixing, we just create a dummy prompt to see whatever model is used with heretic...
"does its
apply_chat_template(add_generation_prompt=True)method adds the tag or not to the prompt".If yes, then we extract only the end part (like
</think>) and set it as theresponse_prefix.Before
After