Skip to main content

StreamDetokenizer

Struct StreamDetokenizer 

Source
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>

Source

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).

Source

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
Source

pub fn push_all( &mut self, tokens: impl IntoIterator<Item = LlamaToken>, ) -> Result<String, DetokenizeError>

Feed several tokens, returning the concatenation of all completed text.

§Errors

See push.

Source

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.

Source

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.

Trait Implementations§

Source§

impl<'a> Debug for StreamDetokenizer<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for StreamDetokenizer<'a>

§

impl<'a> RefUnwindSafe for StreamDetokenizer<'a>

§

impl<'a> Send for StreamDetokenizer<'a>

§

impl<'a> Sync for StreamDetokenizer<'a>

§

impl<'a> Unpin for StreamDetokenizer<'a>

§

impl<'a> UnsafeUnpin for StreamDetokenizer<'a>

§

impl<'a> UnwindSafe for StreamDetokenizer<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more