pub struct Sequence { /* private fields */ }Expand description
Maintains state for an ongoing sequence of tokens and their decoded text.
Mirrors the design of the native DecodeStream in the HuggingFace tokenizers
crate but works through the dyn Tokenizer trait so it supports all backends
(HuggingFace, Tiktoken, Mock).
Key design decisions (matching native DecodeStream):
- Token draining: consumed tokens are drained from the buffer after each successful step, keeping memory bounded regardless of generation length.
- Prefix caching: the decoded prefix string is cached between calls,
avoiding a redundant
decode()on the next step.
Implementations§
Source§impl Sequence
impl Sequence
Sourcepub fn new(tokenizer: Arc<dyn TokenizerTrait>) -> Self
pub fn new(tokenizer: Arc<dyn TokenizerTrait>) -> Self
Create a new empty sequence
Sourcepub fn new_with_options(
tokenizer: Arc<dyn TokenizerTrait>,
skip_special_tokens: bool,
) -> Self
pub fn new_with_options( tokenizer: Arc<dyn TokenizerTrait>, skip_special_tokens: bool, ) -> Self
Create a new empty sequence with skip_special_tokens option
Sourcepub fn with_tokens(
tokenizer: Arc<dyn TokenizerTrait>,
token_ids: Vec<TokenIdType>,
) -> Self
pub fn with_tokens( tokenizer: Arc<dyn TokenizerTrait>, token_ids: Vec<TokenIdType>, ) -> Self
Create a sequence with initial tokens
Sourcepub fn with_tokens_and_options(
tokenizer: Arc<dyn TokenizerTrait>,
token_ids: Vec<TokenIdType>,
skip_special_tokens: bool,
) -> Self
pub fn with_tokens_and_options( tokenizer: Arc<dyn TokenizerTrait>, token_ids: Vec<TokenIdType>, skip_special_tokens: bool, ) -> Self
Create a sequence with initial tokens and skip_special_tokens option
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Get the total number of tokens appended (logical length, not buffer size)
Sourcepub fn append_text(
&mut self,
input: &str,
add_special_tokens: bool,
) -> Result<()>
pub fn append_text( &mut self, input: &str, add_special_tokens: bool, ) -> Result<()>
Append text to the sequence by encoding it.
WARNING: Do not mix append_text() and append_token() on the same
instance. append_text() does not invalidate the incremental decode
cache (cached_prefix/prefix_index), so subsequent append_token()
calls would diff against stale state.
Set add_special_tokens to true for embeddings, or false for chat completion
where the chat template already handles special tokens.
Sourcepub fn append_token(&mut self, token_id: TokenIdType) -> Result<String>
pub fn append_token(&mut self, token_id: TokenIdType) -> Result<String>
Append a single token to the sequence and return newly decoded text.
Delegates to Decoder::decode_step on the tokenizer trait. For HuggingFace
tokenizers this uses the native step_decode_stream; other backends use the
default double-decode fallback. Both paths handle token draining and prefix
caching internally.
Sourcepub fn tokenizer(&self) -> &Arc<dyn TokenizerTrait>
pub fn tokenizer(&self) -> &Arc<dyn TokenizerTrait>
Get a reference to the tokenizer
Sourcepub fn token_ids(&self) -> &[TokenIdType] ⓘ
pub fn token_ids(&self) -> &[TokenIdType] ⓘ
Get the current token ids in the buffer (sliding window, not full history)
Sourcepub fn text(&self) -> Result<String>
pub fn text(&self) -> Result<String>
Decode the current buffer to text.
WARNING: after append_token() calls, this only decodes the sliding
window (retained tokens), not the full sequence history. Use the
incremental return values from append_token() to build the full text.
Sourcepub fn skip_special_tokens(&self) -> bool
pub fn skip_special_tokens(&self) -> bool
Get whether special tokens are skipped during decoding
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Sequence
impl !RefUnwindSafe for Sequence
impl Send for Sequence
impl Sync for Sequence
impl Unpin for Sequence
impl UnsafeUnpin for Sequence
impl !UnwindSafe for Sequence
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more