# How to set stopping criteria in model.generate() when a certain word appears

**URL:** <https://discuss.huggingface.co/t/how-to-set-stopping-criteria-in-model-generate-when-a-certain-word-appears/70274>\
**Category:** 🤗Transformers\
**Created:** [January 23, 2024, 3:28pm UTC](https://discuss.huggingface.co/t/how-to-set-stopping-criteria-in-model-generate-when-a-certain-word-appears/70274 "2024-01-23T15:28:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pradeep1995](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/pradeep1995/32/21207_2.png) [@Pradeep1995](https://discuss.huggingface.co/u/Pradeep1995)\
**Post date:** [January 23, 2024, 3:28pm UTC](https://discuss.huggingface.co/t/how-to-set-stopping-criteria-in-model-generate-when-a-certain-word-appears/70274/1 "2024-01-23T15:28:56Z")

</div>

How to set stopping criteria in model.generate() when a certain word appears

The word I need to stop the generation when found is : [/SENTENCE]  
But the model doesn’t generate the word itself, instead, it generates the subwords  
[[/,SEN,TE,NC,E] ]  
like this.

corresponding IDs from the tokenizer are,  
( Id and subword word)  
28792 =\> [  
28748 =\> /  
28759 =\> SEN  
2654 =\> TE  
1197 =\> NC  
28793 =\> E]

so how can I put the condition in **StoppingCriteriaList** that I should stop the generation when the [/SENTENCE] found?

---

<div class="post-metadata">

**Author:** ![Sandy1857](https://avatars.discourse-cdn.com/v4/letter/s/ecae2f/32.png) [@Sandy1857](https://discuss.huggingface.co/u/Sandy1857)\
**Post date:** [January 23, 2024, 6:08pm UTC](https://discuss.huggingface.co/t/how-to-set-stopping-criteria-in-model-generate-when-a-certain-word-appears/70274/2 "2024-01-23T18:08:23Z")

</div>

Imho if you are fine tuning the model to stop generation at encountering [/sentence] token and it’s generating subwords, you should probably train it for a few more epochs.

You could include the prediction generated during training in the evaluation loop and see when the complete eos token is being generated.

---

<div class="post-metadata">

**Author:** ![Pradeep1995](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/pradeep1995/32/21207_2.png) [@Pradeep1995](https://discuss.huggingface.co/u/Pradeep1995)\
**Post date:** [January 24, 2024, 1:46am UTC](https://discuss.huggingface.co/t/how-to-set-stopping-criteria-in-model-generate-when-a-certain-word-appears/70274/3 "2024-01-24T01:46:26Z")

</div>

@Sandy1857 so why we don’t solve this problem by using the transformers.StoppingCriteriaList() class inside model.generate() function

---

<div class="post-metadata">

**Author:** ![berkecr](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/berkecr/32/32071_2.png) [@berkecr](https://discuss.huggingface.co/u/berkecr)\
**Post date:** [February 18, 2024, 11:01am UTC](https://discuss.huggingface.co/t/how-to-set-stopping-criteria-in-model-generate-when-a-certain-word-appears/70274/4 "2024-02-18T11:01:24Z")

</div>

@Pradeep1995 [this](https://discuss.huggingface.co/t/implimentation-of-stopping-criteria-list/20040/9) may be a good solution.
