Class QwenTokenizer

java.lang.Object
com.alibaba.dashscope.tokenization.QwenTokenizer
All Implemented Interfaces:
Tokenizer

public class QwenTokenizer extends Object implements Tokenizer
BPE encode and decode (3.0 owner of the 2.x tokenizers.QwenTokenizer leaf); implementation reference https://github.com/openai/tiktoken and https://github.com/karpathy/minbpe. The vocabulary qwen.tiktoken is resolved at runtime from DASHSCOPE_QWEN_VOCAB, the classpath, a verified local cache, or a ModelScope download (it is no longer shipped in the jar).
  • Constructor Details

    • QwenTokenizer

      public QwenTokenizer()
  • Method Details

    • encodeOrdinary

      public List<Integer> encodeOrdinary(String text)
      Description copied from interface: Tokenizer
      Encoding that ignores any special tokens. There is no guarantee that the results will be completely consistent with the model output. Compared with the python version, there are some differences. It is known that the results of processing 2 or 4 consecutive spaces are different. Detailed information for qwen reference: https://github.com/QwenLM/Qwen/blob/main/tokenization_note_zh.md
      Specified by:
      encodeOrdinary in interface Tokenizer
      Parameters:
      text - The input.
      Returns:
      The list of token ids.
    • encode

      public List<Integer> encode(String text, String allowedSpecial)
      Description copied from interface: Tokenizer
      Encode the input text, handles special tokens. Same accuracy caveats as Tokenizer.encodeOrdinary(String).
      Specified by:
      encode in interface Tokenizer
      Parameters:
      text - The input to be encoded.
      allowedSpecial - The special token options, one of "all"|"none"|"none_raise"; with "none_raise" a NoSpecialTokenException is thrown when no special token is present; null means "all".
      Returns:
      The list of token ids.
    • decode

      public String decode(List<Integer> tokens)
      Description copied from interface: Tokenizer
      Decode token ids to String.
      Specified by:
      decode in interface Tokenizer
      Parameters:
      tokens - The input token ids, eg: [2610, 525, 264, 10950, 17847, 13], will be decoded to "You are a helpful assistant."
      Returns:
      The token ids corresponding string.