# Controlled Text Generation

**URL:** <https://discuss.huggingface.co/t/controlled-text-generation/15726>\
**Category:** 🤗Transformers\
**Created:** [March 15, 2022, 1:32pm UTC](https://discuss.huggingface.co/t/controlled-text-generation/15726 "2022-03-15T13:32:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![josephgatto](https://avatars.discourse-cdn.com/v4/letter/j/b38774/32.png) [@josephgatto](https://discuss.huggingface.co/u/josephgatto)\
**Post date:** [March 15, 2022, 1:32pm UTC](https://discuss.huggingface.co/t/controlled-text-generation/15726/1 "2022-03-15T13:32:27Z")

</div>

I am trying to perform in context learning with GPT-Neo and I have noticed that it’s hard to get the text generation pipeline to just complete a single line. More specifically, suppose I have the following prompt:

```auto
Give a complement about a topic: 

Topic: Soccer
Complement: You are so good at soccer

Topic: Cooking
Complement: I love your cooking 

Topic: Public Speaking
Complement: 

```

Now when I ask GPT to continue using the text generation pipeline, I often get much more than the “complement” line completed. If the max length is large enough, it will go on to make up it’s own Topics and Complements. Is there any way to make it stop after completing a single line?

Thanks!

---

<div class="post-metadata">

**Author:** ![YannAgora](https://avatars.discourse-cdn.com/v4/letter/y/c0e974/32.png) [@YannAgora](https://discuss.huggingface.co/u/YannAgora)\
**Post date:** [March 15, 2022, 2:03pm UTC](https://discuss.huggingface.co/t/controlled-text-generation/15726/2 "2022-03-15T14:03:32Z")

</div>

Hey 👋

In the `generate()` method, you can define the EOS token id ( `eos_token_id`), I guess that if you set this parameter with the id of `\n`, it would generate only a single line.

But I’m not sure there is this parameter in the `TextGenerationPipeline`, workaround in this case would be to use regex after the generation to get everything before the first `\n`.

---

<div class="post-metadata">

**Author:** ![Yiping](https://avatars.discourse-cdn.com/v4/letter/y/ccd318/32.png) [@Yiping](https://discuss.huggingface.co/u/Yiping)\
**Post date:** [March 26, 2022, 7:17am UTC](https://discuss.huggingface.co/t/controlled-text-generation/15726/3 "2022-03-26T07:17:20Z")

</div>

Hi @josephgatto,

you can refer to this paper which addresses the problem.

[Generating Datasets with Pretrained Language Models (EMNLP 2021)](https://arxiv.org/abs/2104.07540)

They used an artificial `eos_token` (the end quotation) which is similar to what @YannAgora mentioned.

However, the model may not always generate the `eos_token` as you wish, especially if it’s not fine-tuned. In this case, you can just discard the sequence if it doesn’t generate the `eos_token` within `max_len`.
