Class DeepSeekTokenizer
java.lang.Object
smile.llm.tokenizer.Tiktoken
smile.llm.model.deepseek.DeepSeekTokenizer
- All Implemented Interfaces:
Tokenizer
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.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordOne entry ofadded_tokensintokenizer.json. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringBeginning-of-sentence token.static final StringEnd-of-sentence token.static final StringPadding token.Fields inherited from class Tiktoken
ranks, specialTokens -
Constructor Summary
ConstructorsConstructorDescriptionDeepSeekTokenizer(Map<Bytes, Integer> ranks, List<DeepSeekTokenizer.AddedToken> addedTokens) Constructor. -
Method Summary
Modifier and TypeMethodDescriptionint[]encodeWithSpecials(String text) Encodes template text in which markers such as<|User|>stand for single tokens.static DeepSeekTokenizerLoads the tokenizer from a checkpoint directory containingtokenizer.json.intpad()Padding token id.int[]Stop token ids used during generation.String[]Splits text into BPE words.tryDecode(int[] tokens, boolean skipSpecial) Decodes tokens; withskipSpecial, BOS/EOS/pad and the role markers are dropped while<think>,</think>and|DSML|stay so reasoning and tool calls can be parsed.Methods inherited from class Tiktoken
allowSpecialTokens, decode, encode, encode, isSpecialTokenAllowed, load, size, specialToken, tryDecode
-
Field Details
-
BOS
-
EOS
-
PAD
-
-
Constructor Details
-
DeepSeekTokenizer
Constructor.- Parameters:
ranks- the token byte sequence to id map, including added tokens.addedTokens- theadded_tokensoftokenizer.json.
-
-
Method Details
-
of
Loads the tokenizer from a checkpoint directory containingtokenizer.json.- Parameters:
checkpointDir- model directory.- Returns:
- the tokenizer.
- Throws:
IOException- iftokenizer.jsonis missing or malformed.
-
tokenize
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. -
encodeWithSpecials
Encodes template text in which markers such as<|User|>stand for single tokens.- Parameters:
text- a rendered chat prompt.- Returns:
- the token ids.
-
tryDecode
Decodes tokens; withskipSpecial, 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:
tryDecodein interfaceTokenizer- Overrides:
tryDecodein classTiktoken- Parameters:
tokens- The list of token IDs to be decoded.skipSpecial- whentrue, 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.
-