Abstract
Large Language Models (LLMs) often struggle to adhere to strict logical constraints, such as specific keyword inclusions or precise length requirements, due to the mathematical intractability of calculating look-ahead probabilities during autoregressive generation. We present Ctrl-G, a novel framework that enables LLMs to follow complex logical constraints without retraining. By distilling a tractable Hidden Markov Model (HMM) to serve as a probabilistic guide and representing constraints as Deterministic Finite Automata (DFAs), Ctrl-G achieves superior constraint satisfaction and text quality compared to state-of-the-art models like GPT-4.
Introduction
While LLMs demonstrate remarkable generative capabilities, they are notoriously unreliable when tasked with "hard" constraints. For instance, requiring a model to generate a paragraph of exactly 28 words containing three specific keywords often results in failure. This stems from the fact that standard LLMs cannot efficiently determine if a current token choice will eventually lead to a valid sequence. Previous solutions typically involved expensive fine-tuning or "greedy" logical filters that often produced disfluent text. Ctrl-G addresses this by introducing a scalable, adaptable pipeline for inference-time guidance.
Methodology
The Ctrl-G framework operates through a three-step process designed to provide reliable and adaptable control while keeping the underlying LLM frozen:
- Distillation: A Hidden Markov Model (HMM) is trained to act as a "white-box" approximation of the target LLM. Unlike 7-billion-parameter Transformers, HMMs are mathematically tractable, allowing for efficient probabilistic calculations regarding future states.
- Constraint Specification: Constraints are represented as Deterministic Finite Automata (DFAs). These structures can represent any regular language and can be combined using logical operations (AND, OR, NOT) to form highly complex, multi-layered rules.
- Inference-time Guidance: During generation, the HMM calculates the probability that a specific token will lead to a state satisfying the DFA. This value is used to "nudge" the LLM’s output, ensuring the model does not "paint itself into a corner" where it must produce gibberish to satisfy a constraint at the last second.
Implementation and Application
One of the primary advantages of Ctrl-G is its ease of use. Complex constraints can be implemented with minimal code, as the system treats stacked constraints as a single unified rule.
Experimental Results
Ctrl-G was evaluated across several benchmarks, including CommonGen (commonsense generation), ROC Stories (text infilling), and CoAuthor (interactive editing). Key findings include:
- Superior Satisfaction: When applied to a TULU2-7B model, Ctrl-G achieved a 30% higher satisfaction rate in human evaluations than GPT-4 on complex text editing tasks.
- Reliability: The framework achieved 100% constraint satisfaction on CommonGen and text infilling benchmarks while maintaining higher text quality (BLEU/ROUGE) than previous constrained generation methods.
- Efficiency: The computational cost scales linearly with the size of the DFA and remains constant regardless of sequence length, making it suitable for real-time applications.
Discussion and Future Work
The success of Ctrl-G highlights the importance of probabilistic reasoning over pure logical filtering. By looking at the "budget" of the remaining sentence, the system guides the model toward tokens that make future constraint satisfaction easier. While currently limited by the approximation accuracy of the HMM and quadratic scaling relative to HMM hidden states, future work may explore encoding abstract reasoning steps into the DFA framework to enhance mathematical and logical problem-solving in LLMs.
Summary updated