Class Qwen3ChatTemplate

java.lang.Object
smile.llm.template.Qwen3ChatTemplate
All Implemented Interfaces:
ChatTemplate

public final class Qwen3ChatTemplate extends Object implements ChatTemplate
Java port of the Qwen3.5 / Qwen3.8 chat_template.jinja, byte for byte (the golden tests compare it with transformers' rendering of the real templates).

The released templates differ in a few independent behaviours, which Qwen3ChatTemplate.Variant.detect(String) reads from the checkpoint's own template text so every checkpoint renders like its reference:

  • Qwen3.8 adds a reasoning-effort system turn (reasoning_effort: xhigh by default, low, medium = none) whenever thinking is on, and keeps the <think> block of earlier assistant turns unless preserve_thinking is false;
  • Qwen3.5 splits a <think>...</think> found in an assistant message's content, and prints an empty system turn for an empty system message;
  • small Qwen3.5 models think only when enable_thinking is true, the larger ones unless it is false.
The generation prompt opens the thinking block (<think>\n) or closes an empty one (<think>\n\n</think>\n\n); the model then continues inside the block, so replies carry no opening tag.
  • Constructor Details

    • Qwen3ChatTemplate

      public Qwen3ChatTemplate()
      Qwen3.8 behaviour with an open assistant turn.
    • Qwen3ChatTemplate

      public Qwen3ChatTemplate(Qwen3ChatTemplate.Variant variant, boolean addGenerationPrompt)
      Creates the template of a checkpoint.
      Parameters:
      variant - the checkpoint's behaviours.
      addGenerationPrompt - whether to append the open assistant turn.
  • Method Details

    • encode

      public String encode(List<Message> dialog, ChatOptions options)
      Description copied from interface: ChatTemplate
      Encodes dialog messages with optional tool-calling options.
      Specified by:
      encode in interface ChatTemplate
      Parameters:
      dialog - ordered conversation turns.
      options - chat options; may be null.
      Returns:
      prompt text including an open assistant turn when applicable.
    • encodeDialog

      public static String encodeDialog(Message[] dialog, ChatOptions options)
      Encodes dialog to a prompt string (generation prompt open) with the Qwen3.8 behaviours.
      Parameters:
      dialog - conversation turns.
      options - chat options; may be null.
      Returns:
      prompt text.