Class DeepSeekTokenizer

java.lang.Object
smile.llm.tokenizer.Tiktoken
smile.llm.model.deepseek.DeepSeekTokenizer
All Implemented Interfaces:
Tokenizer

public class DeepSeekTokenizer extends Tiktoken
DeepSeek-V4.1 tokenizer: no normalizer; added tokens are matched first and stay atomic; the text between them goes through the three "Isolated" splits of tokenizer.json (1-3 digit groups, CJK/kana runs, then the word/punctuation/whitespace regex) before byte-level BPE.

Unlike tiktoken's find() loop, an "Isolated" split keeps the text between matches as pieces of their own, which the following stage splits further, so tokenize(String) is overridden.

  • Field Details

  • Constructor Details

    • DeepSeekTokenizer

      public DeepSeekTokenizer(Map<Bytes,Integer> ranks, List<DeepSeekTokenizer.AddedToken> addedTokens)
      Constructor.
      Parameters:
      ranks - the token byte sequence to id map, including added tokens.
      addedTokens - the added_tokens of tokenizer.json.
  • Method Details

    • of

      public static DeepSeekTokenizer of(String checkpointDir) throws IOException
      Loads the tokenizer from a checkpoint directory containing tokenizer.json.
      Parameters:
      checkpointDir - model directory.
      Returns:
      the tokenizer.
      Throws:
      IOException - if tokenizer.json is missing or malformed.
    • tokenize

      public String[] tokenize(String text)
      Splits text into BPE words. With special tokens allowed, added tokens are cut out first (leftmost, longest) and kept whole; everything else, including text between matches, goes through the three isolated splits.
      Specified by:
      tokenize in interface Tokenizer
      Overrides:
      tokenize in class Tiktoken
      Parameters:
      text - The input string to be tokenized.
      Returns:
      The tokenized sequence.
    • encodeWithSpecials

      public int[] encodeWithSpecials(String text)
      Encodes template text in which markers such as <|User|> stand for single tokens.
      Parameters:
      text - a rendered chat prompt.
      Returns:
      the token ids.
    • tryDecode

      public String tryDecode(int[] tokens, boolean skipSpecial) throws CharacterCodingException
      Decodes tokens; with skipSpecial, BOS/EOS/pad and the role markers are dropped while <think>, </think> and |DSML| stay so reasoning and tool calls can be parsed.
      Specified by:
      tryDecode in interface Tokenizer
      Overrides:
      tryDecode in class Tiktoken
      Parameters:
      tokens - The list of token IDs to be decoded.
      skipSpecial - when true, registered special-token IDs are omitted (useful for streaming chat so headers/eot are not shown as literal <|...|> text).
      Returns:
      The decoded string.
      Throws:
      CharacterCodingException - If the byte sequence is not legal UTF-8.
    • pad

      public int pad()
      Padding token id.
      Returns:
      the pad token id.
    • stopTokens

      public int[] stopTokens()
      Stop token ids used during generation.
      Returns:
      the end-of-sentence token id.