pub trait TokenConstraint: Send + Sync {
// Required methods
fn allowed_tokens(
&self,
generated: &[u32],
vocab_size: usize,
) -> Option<Vec<bool>>;
fn advance(&mut self, token: u32) -> bool;
fn is_complete(&self) -> bool;
fn reset(&mut self);
fn name(&self) -> &str;
}Expand description
A constraint that restricts which tokens are valid at each decoding step.
Implementors maintain internal state representing how far through the constrained structure the generation has progressed.
Required Methods§
Sourcefn allowed_tokens(
&self,
generated: &[u32],
vocab_size: usize,
) -> Option<Vec<bool>>
fn allowed_tokens( &self, generated: &[u32], vocab_size: usize, ) -> Option<Vec<bool>>
Given the tokens generated so far, return a bitmask of allowed next tokens.
vocab_size is the total vocabulary size. Returns None if all tokens
are allowed (no active constraint).
Sourcefn advance(&mut self, token: u32) -> bool
fn advance(&mut self, token: u32) -> bool
Called after a token is committed.
Returns false if the constraint is now violated (generation should stop).
Sourcefn is_complete(&self) -> bool
fn is_complete(&self) -> bool
Returns true if the current state is a valid terminal state.