ConstrainedDecoder

Struct ConstrainedDecoder 

Source
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

Source

pub fn new(generator: MaskGenerator) -> Self

Create a new constrained decoder with default limits.

Source

pub fn with_limits(generator: MaskGenerator, limits: Limits) -> Self

Create a new constrained decoder with custom limits.

Source

pub fn phase(&self) -> Phase

Get the current build phase.

Source

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.

Source

pub fn next_mask(&self) -> LogitMask

Generate the logit mask for the next token.

Apply this mask to your LLM’s logits before sampling.

Source

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
Source

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

Source

pub fn builder(&self) -> &Builder

Get a reference to the internal builder.

Source

pub fn generator(&self) -> &MaskGenerator

Get the mask generator.

Trait Implementations§

Source§

impl Debug for ConstrainedDecoder

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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