A Plug-and-Play Method for Controlled Text Generation

Damian Pascual, Beni Egressy, Clara Meister, Ryan Cotterell, Roger Wattenhofer

Abstract

Large Language Models (LLMs) excel at fluent text generation but often struggle with hard constraints, such as including specific keywords. We present Keyword2Text (K2T), a simple, training-free decoding-time modification that steers LLMs toward target keywords. By shifting the model's output distribution toward semantically similar words and employing an exponential annealing schedule, K2T guarantees the inclusion of keywords while maintaining high fluency. Our results show a 100% success rate in keyword inclusion, outperforming existing fine-tuning and search-based methods.

Introduction

The "stochastic parrot" nature of LLMs makes them unpredictable for tasks requiring specific content constraints. Standard decoding methods often fail to incorporate required keywords naturally, if at all. Existing solutions like fine-tuning or complex search algorithms are either computationally expensive or difficult to implement. Keyword2Text (K2T) addresses this by providing a "plug-and-play" mechanism that works with any off-the-shelf autoregressive model.

Methodology

K2T operates by modifying the logits of the language model at each generation step through two primary mechanisms:

1. Semantic Steering

The method calculates the cosine similarity between the vocabulary and the target keyword in a pre-trained embedding space (e.g., GloVe). A shift $\lambda$ is added to the log-probabilities of words semantically close to the target:

$$score'(y_t, w) = score(y_t) + \lambda \cdot \max(0, \cos(\gamma(y_t), \gamma(w)))$$

This encourages the model to generate a context where the keyword is likely to appear naturally.

2. Annealing Schedule

To ensure the keyword eventually appears, K2T uses an exponential annealing schedule. As the sequence approaches its maximum length, the steering strength $\lambda$ increases, eventually forcing the model to output the required keyword if it hasn't already appeared.

Impact of steering strength $\lambda$ on generation metrics.

Experimental Setup

The authors evaluated K2T on several benchmarks, including ROC Stories (story generation) and 500N-KPCrowd (news generation). Performance was measured using Success Rate (SR), Perplexity (PPL) for fluency, and Repetition rate (Rep).

Results

K2T achieves a perfect 100% success rate in keyword inclusion across various benchmarks. Unlike competing methods like CGMH or Plan-and-Write, K2T maintains a better balance between constraint satisfaction and text quality. In human evaluations, K2T-generated stories were rated significantly higher in creativity and overall quality than competing hard-control methods.

Performance comparison of K2T against baseline controlled generation methods.

Conclusion

Keyword2Text offers a robust and simple solution for hard-constrained text generation. Its ability to steer models without additional training makes it a versatile tool for applications ranging from creative writing to automated news generation. Future work may explore negative constraints or steering for more abstract attributes like sentiment.

Citation: Pascual, D., et al. (2021). A Plug-and-Play Method for Controlled Text Generation. EMNLP 2021.

Summary updated

BibTeX
@inproceedings{pascual2021plug,
  author = {Damian Pascual and Beni Egressy and Clara Meister and Ryan Cotterell and Roger Wattenhofer},
  title = {{A Plug-and-Play Method for Controlled Text Generation}},
  booktitle = {Conference on Empirical Methods in Natural Language Processing},
  year = {2021},
  doi = {10.18653/v1/2021.findings-emnlp.334},
}
Figure