Most Language Models can be Poets too: An AI Writing Assistant and Constrained Text Generation Studio (2022)

Abstract: Despite rapid advancement in the field of Constrained Natural Language Generation, little time has been spent on exploring the potential of language models which have had their vocabularies lexically, semantically, and/or phonetically constrained. We find that most language models generate compelling text even under significant constraints. We present a simple and universally applicable technique for modifying the output of a language model by compositionally applying filter functions to the language models vocabulary before a unit of text is generated. This approach is plug-and-play and requires no modification to the model. To showcase the value of this technique, we present an easy to use AI writing assistant called “Constrained Text Generation Studio” (CTGS). CTGS allows users to generate or choose from text with any combination of a wide variety of constraints, such as banning a particular letter, forcing the generated words to have a certain number of syllables, and/or forcing the words to be partial anagrams of another word. We introduce a novel dataset of prose that omits the letter “e”. We show that our method results in strictly superior performance compared to fine-tuning alone on this dataset. We also present a Huggingface “space” web-app presenting this technique called Gadsby. The code is available to the public here: https://github.com/Hellisotherpeople/Constrained-Text-Generation-Studio

## introduction constrained writing is a literary approach in which the writer decides to impose patterns, constraints, or conditions on their text. the most obvious example of this application is within poetry -but many other communities of writers also find imposing constraints on themselves to be enjoyable. we can divide constraints into two types, soft-constraints and hard-constraints. soft constraints are the kind that are fuzzy, e.g. deciding to write in a certain style. soft constraints are almost exclusively applied at the sequence level, rather than being applied directly on each token. hard constraints are concrete lexical, semantic, or phonetic requirements about the contents of a token or sequence. in this paper, we are presenting a system that applied token level hard-constraints to large-scale language models. one notable group who create hard-constrained texts are the oulipo (short for ouvroir de littérature potentielle; roughly translated as the "workshop of potential literature") writing collective. oulipo affiliated writers have produced a prolific amount of constrained literature since the 1960s. oulipos founder has described the writers within the collective as "rats who construct the labyrinth from which they plan to escape". one does not need to be a rodent to find "recreational linguistics" useful. any suitor who has pledged their affection in print can attest to how difficult it can be to write good love poetry; and being able to generate rhyming text that also has the lengths of consecutive words matching the digits of pi is sure to swoon all but the most frigid of mathematicians. natural language generation has advanced at a breakneck pace. as models have scaled up, their ## prior work we are not the first to explore constrained natural language generation with language models. probably the closest prior work to our own comes from @xcite . they propose a single plug-and-play semantic filter which shifts the sampling probabilities of a language models vocabulary towards a user defined keyword or set of keywords. ctgs instead offers a rich array of compositional lexical, phonetic, and semantic filters, and it preserves the original language models sampling probabilities with the exception of the filtered out tokens, which are banned. @xcite show that language models using constrained beam search can effectively generate text with the constraint of either banning or requiring certain words to appear in a sequence. notably, the transformers library from huggingface recently integrated this functionality foot_2 . constrained beam search is effective for translation and other sequence-tosequence tasks, but it makes it impossible for the language model to assist humans on a per-token basis. ctgs adopts an optional human-in-the-loop approach where the user can decide which token to choose following the listed constraints at each step, rather than necessarily relying on sampling. given the inherit creativity required for constrained writing, using language models for inspiration rather than blindly generating with them is uniquely helpful for recreational linguists. @xcite propose a method for controlled text generation by formulating it as an optimization problem given a list of constraints and using gradient descent to maximize the log probability of the language model as well as the constraint objectives. the constraints that they provide are exclusively sequence level. by contrast, ctgs's filters are at the token level and are correspondingly much more appropriate for oulipo or poetry. their method also requires a potentially lengthy optimization process. @xcite propose a reinforcement learning based technique for generating sequences with conceptual constraints. this method requires training and is not applicable for hard lexical or phonetic constraints. ## implementation details in this section, we explore the quirks, caveats, and details of the implementation of our technique within ctgs. ## dataset without the "e" one of the issues that large language models present for constrained writers is that even when heavily fine-tuned on a particular dataset, they 5 e.g. gpt, gpt-2, and gpt-3 all have a vocab size of 50257 words. frequently ignore their constraints. for example, poetry models that were fine-tuned on the works of william shakespeare frequently stumble and fail to maintain rhyme or meter. 6 we show that language models, which are fine-tuned even on the simple lexical constraint of omitting the letter "e", still occasionally ignore their constraints. in fact, even when these models are overtrained to an absurd degree, complete adherence to these constraints is unlikely. such behavior motivates the creation of datasets which include some forms of hard lexical, semantic, or phonetic constraints. by doing so, we can measure how often language models ignore them, and more importantly, we can show that this method of filtering out these tokens before the generation step leads to strictly better performance and eliminates these kinds of errors. we present a dataset, called "lipogram-e", which consists of all known complete book-length english works which do not use the letter "e". this dataset includes all of gadsby by ernest vincent wright, all of a void by georges perec, and almost all of eunoia by christian bok 7 . we name it "lipogram-e" because a lipogram is a text where the author omits one or more letters from the alphabet. while it may be possible to produce a dataset without the letter "e" by simply computationally looking through an existing large scale dataset for sentences which match that constraint, doing so would result in jumbled and incoherent training examples, with little relation to each other. by contrast, books and prose written with constraints have clear, coherent narratives. we chose the constraint of banning "e" because it is extremely easy to computationally verify and because there is no potential for error from the filter function. ## experiment we design the experiment to measure how often a language model makes constraint-based mistakes on the lipogram-e dataset. we look at the perplexity and the ignored constraint error rate of gpt-2-medium. we choose gpt-2-medium because of its relatively well-understood finetunability. we compare the untrained gpt-2 model to the regularly fine-tuned model, and the overfine-tuned model. we show that in all instances, applying the constraint to ban the letter "e" from the vocabulary of these models results in both improved perplexity, as well as zero ignored constraint errors. ## discussion and observations language models that have had their vocabularies filtered act significantly differently from unaltered models. because the filters remove significant amounts of entries with high probability of being generated, models are more likely to behave undesirably. some of the undesirable behavior observed included models generating total gibberish, generating repetitive text, generating potentially personally identifying information, generating profanity, and generating computer code. the more tokens which are filtered, and the higher their probability, the more likely it is that models will end up in these degenerate states. we hope that this paper motivates further and more exhaustive analysis of the vocabularies of language models and in particular, what properties they have when altered. filtering the vocabularies of language models opens up unique possibilities for adversarial machine learning. any model which is exposing its full probability distribution before decoding could potentially be "attacked" by a sophisticated actor who has figured out what they "don't want" the 7 eunoia is a work where each chapter only uses one vowel. we omit the chapter that uses the vowel "e" it would be extremely interesting to see how this technique will work with large scale language models such as openais gpt-3 or huggingfaces bloom model. it is likely to make this technique extremely sophisticated, but large scale models frequently are not released to the public and their vocabularies probability distributions are not always exposed to the end user. ## final thoughts and conclusion in this paper, we introduced the ai constrained writing assistant called ctgs, explained its features and rationale, and mused about its potential use cases. we also introduced a huggingface hosted webapp which demonstrates the plug-and-play nature of constraining the vocabulary of a language model. we introduced a dataset of english books which do not contain the letter "e" called "lipogram-e". we showed that our technique results in lower perplexity and zero ignored constraint errors in a variety of circumstances. finally, we discussed the unique behaviors that models with constraints have. we also hope to use this paper to serve as a call to action for the language modeling community to not abandon research into word level tokenizers and training models using them. if that's not possible, at least some form of "signposting" should be built into subsequently trained models using potentially a new subword tokenization scheme designed for this purpose. we hope this paper motivates future work on word-level tokenization, and on language models trained with extremely large vocabularies.