Guiding Text Generation with Constrained Beam Search in 🤗 Transformers
Hugging Face Blog
Hugging Face's Transformers library now supports constrained beam search, which allows users to force specific words or phrases into generated text outputs during the decoding process. The feature uses a "bank" system that organizes candidate sequences by their progress toward fulfilling constraints, then selects from these banks in round-robin fashion to balance constraint satisfaction with output quality. This enables applications like machine translation where certain words must appear in the output, or where users can specify alternative word forms that should appear at least once.