pub struct StreamDetokenizer<'a> { /* private fields */ }Expand description
A stateful, incremental detokenizer built on
LlamaModel::token_to_raw_bytes.
Feed tokens one at a time with push; each call
returns the text that has become complete, buffering any partial multi-byte
character until it can be finished. Call
finish at the end of a generation to flush and
validate any remaining bytes.
Implementations§
Source§impl<'a> StreamDetokenizer<'a>
impl<'a> StreamDetokenizer<'a>
Sourcepub fn new(model: &'a LlamaModel, special: Special) -> Self
pub fn new(model: &'a LlamaModel, special: Special) -> Self
Create a new streaming detokenizer over model.
special is forwarded to LlamaModel::token_to_raw_bytes and controls
whether special/control tokens are rendered (Special::Tokenize) or
treated as plaintext (Special::Plaintext).
Sourcepub fn push(&mut self, token: LlamaToken) -> Result<String, DetokenizeError>
pub fn push(&mut self, token: LlamaToken) -> Result<String, DetokenizeError>
Feed a single token, returning any text that is now complete.
A trailing incomplete UTF-8 sequence is retained internally and emitted once a subsequent token completes it.
§Errors
DetokenizeError::TokenToBytesif the token cannot be converted.DetokenizeError::InvalidUtf8if the accumulated bytes contain a genuinely malformed (not merely incomplete) UTF-8 sequence.
Sourcepub fn push_all(
&mut self,
tokens: impl IntoIterator<Item = LlamaToken>,
) -> Result<String, DetokenizeError>
pub fn push_all( &mut self, tokens: impl IntoIterator<Item = LlamaToken>, ) -> Result<String, DetokenizeError>
Sourcepub fn pending(&self) -> &[u8] ⓘ
pub fn pending(&self) -> &[u8] ⓘ
The bytes currently buffered awaiting completion of a multi-byte character. Empty when the stream is on a UTF-8 boundary.
Sourcepub fn finish(self) -> Result<String, DetokenizeError>
pub fn finish(self) -> Result<String, DetokenizeError>
Finish the stream, returning any remaining complete text.
§Errors
DetokenizeError::IncompleteUtf8 if bytes remain that do not form a
complete UTF-8 sequence (carrying the leftover bytes), or
DetokenizeError::InvalidUtf8 if the buffered bytes are malformed.