动机、参考资料、涉及内容

FAQ

thinkno_think 的本质

从触发方式上, 有两种:

  • tokenizer.apply_chat_template 里的 enable_thinking 指定
  • enable_thinking 为 true 时, 还可以在 message 的末尾(字符串)加上 /think 或者 /no_think
from transformers import AutoModelForCausalLM, AutoTokenizer

model_name = "Qwen/Qwen3-30B-A3B"

# qwen3 的 tokenizer class 使用的是 qwen2 的 tokenizer class
tokenizer = AutoTokenizer.from_pretrained(model_name)
text = tokenizer.apply_chat_template(
    messages,
    tokenize=False,
    add_generation_prompt=True,  # 这个是为了适应对话型任务加上 <|im_start|>assistant
    enable_thinking=False  # Setting enable_thinking=False disables thinking mode
)

注意: 原理上, 是 tokenizer_config.json 里关于 chat_template 的 jinja2 模板末尾的这段配置

也就是如果 enable_thinking 为 False 时, 会自动加上 <think>\n\n</think>, 而 enable_thinking 为 True 时, 模型因为训练时的策略, 总会自动输出 <think>...</think>... 的结构.

而当 enable_thinking 为 True, 用户手工在问句末尾添加 /no_think, 在 tokenizer 层面, /no_think 并不是特殊 token, 而是模型因为训练时的策略, 总会自动输出 <think>\n\n</think>...