Class Tokenizer
java.lang.Object
smile.onnx.genai.Tokenizer
- All Implemented Interfaces:
AutoCloseable
Converts between text and GenAI token ids, including chat templates.
-
Method Summary
Modifier and TypeMethodDescriptionapplyChatTemplate(String templateStr, String messagesJson, String toolsJson, boolean addGenerationPrompt) Applies the model's chat template.intReturns the BOS token id.voidclose()Creates a streaming decoder for use withGenerator.decode(int[] tokens) Decodes token ids to text.String[]decodeBatch(Sequences sequences) Decodes each sequence in aSequencesto text.Encodes a single string into aSequenceswith one sequence.encodeBatch(String[] texts) Encodes multiple strings into oneSequences(one entry per string).int[]encodeToArray(String text) Encodes text and returns the first sequence as anint[].static TokenizerCreates a tokenizer from a model directory path.static TokenizerCreates a tokenizer from a model directory path.static TokenizerCreates a tokenizer from a config.static TokenizerCreates a tokenizer from a loaded model.intReturns the PAD token id.intConverts a string to a single token id.voidupdateOptions(Map<String, String> options) Updates tokenizer options (key/value string pairs).
-
Method Details
-
of
-
of
-
of
-
of
-
encode
-
encodeBatch
-
encodeToArray
Encodes text and returns the first sequence as anint[].- Parameters:
text- text to encode.- Returns:
- token ids.
-
decode
Decodes token ids to text.- Parameters:
tokens- token ids.- Returns:
- decoded text.
-
decodeBatch
-
applyChatTemplate
public String applyChatTemplate(String templateStr, String messagesJson, String toolsJson, boolean addGenerationPrompt) Applies the model's chat template.- Parameters:
templateStr- template string, ornullto use the model default.messagesJson- messages as a JSON array string.toolsJson- tools JSON, ornullwhen unused.addGenerationPrompt- whether to append the assistant generation prompt.- Returns:
- formatted prompt text.
-
updateOptions
-
createStream
Creates a streaming decoder for use withGenerator.- Returns:
- a new stream owned by the caller.
-
bosTokenId
public int bosTokenId()Returns the BOS token id.- Returns:
- BOS token id.
-
padTokenId
public int padTokenId()Returns the PAD token id.- Returns:
- PAD token id.
-
toTokenId
Converts a string to a single token id.- Parameters:
token- token string.- Returns:
- token id.
-
close
public void close()- Specified by:
closein interfaceAutoCloseable
-