pub struct ConstrainedDecoder { /* private fields */ }Expand description
High-level orchestrator for constrained LLM decoding.
This struct manages the decode loop for generating TØR-G graphs from an LLM. It maintains builder state, generates masks, and converts LLM tokens back to TØR-G tokens.
§Example
let mapping = TokenMapping::sequential(256);
let generator = MaskGenerator::new(mapping, vocab_size);
let mut decoder = ConstrainedDecoder::new(generator);
while !decoder.is_complete() {
let mask = decoder.next_mask();
mask.apply_to_logits(&mut logits);
let token_id = sample(&logits);
decoder.feed_token(token_id)?;
}
let graph = decoder.finish()?;Implementations§
Source§impl ConstrainedDecoder
impl ConstrainedDecoder
Sourcepub fn new(generator: MaskGenerator) -> Self
pub fn new(generator: MaskGenerator) -> Self
Create a new constrained decoder with default limits.
Sourcepub fn with_limits(generator: MaskGenerator, limits: Limits) -> Self
pub fn with_limits(generator: MaskGenerator, limits: Limits) -> Self
Create a new constrained decoder with custom limits.
Sourcepub fn is_complete(&self) -> bool
pub fn is_complete(&self) -> bool
Check if decoding is complete.
Returns true when the graph is in a completable state (at least
one output declared) or when the builder is in the Done phase.
Sourcepub fn next_mask(&self) -> LogitMask
pub fn next_mask(&self) -> LogitMask
Generate the logit mask for the next token.
Apply this mask to your LLM’s logits before sampling.
Sourcepub fn feed_token(&mut self, llm_token_id: u32) -> Result<(), DecodeError>
pub fn feed_token(&mut self, llm_token_id: u32) -> Result<(), DecodeError>
Feed an LLM token ID into the decoder.
The token ID is reverse-mapped to a TØR-G token and pushed to the internal builder.
§Errors
Returns an error if:
- The token ID doesn’t map to a valid TØR-G token
- The token is not valid in the current builder state
Sourcepub fn finish(self) -> Result<Graph, BuildError>
pub fn finish(self) -> Result<Graph, BuildError>
Finish decoding and return the constructed graph.
§Errors
Returns an error if the graph is incomplete (e.g., no outputs declared).
Sourcepub fn generator(&self) -> &MaskGenerator
pub fn generator(&self) -> &MaskGenerator
Get the mask generator.