pub struct LengthConstraint { /* private fields */ }Expand description
A constraint that enforces hard minimum and maximum token-count limits.
- While
count < min_len: if astop_tokenis configured it is excluded from the mask (cannot stop early). - While
count >= max_len: if astop_tokenis configured only that token is allowed; otherwise an all-falsemask is returned, signalling the caller to halt generation externally. - Between
min_lenandmax_len: all tokens are allowed (None).
Completion is defined as either reaching max_len OR generating min_len or
more tokens followed by the stop_token.
§Example
use oxibonsai_runtime::constrained_decoding::{LengthConstraint, TokenConstraint};
// Must generate at least 2 tokens, stop token is 1 (EOS), max 10.
let mut c = LengthConstraint::new(2, 10, Some(1));
// Before min_len: stop_token excluded
let mask = c.allowed_tokens(&[], 4).unwrap();
assert!(!mask[1]); // stop token blocked
assert!(mask[0]); // other tokens allowedImplementations§
Trait Implementations§
Source§impl TokenConstraint for LengthConstraint
impl TokenConstraint for LengthConstraint
Source§fn advance(&mut self, token: u32) -> bool
fn advance(&mut self, token: u32) -> bool
Commits token, updating count and stop_seen. Always returns true.
Source§fn is_complete(&self) -> bool
fn is_complete(&self) -> bool
Returns true when at least min_len tokens have been generated AND either
the stop_token was seen or max_len has been reached.
Auto Trait Implementations§
impl Freeze for LengthConstraint
impl RefUnwindSafe for LengthConstraint
impl Send for LengthConstraint
impl Sync for LengthConstraint
impl Unpin for LengthConstraint
impl UnsafeUnpin for LengthConstraint
impl UnwindSafe for LengthConstraint
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
Mutably borrows from an owned value. Read more
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>
Converts
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>
Converts
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