How can I restrict the number of words in a certain range OpenAI generates?

Hi All,

I want OpenAI to generate output in a specific range of word counts. For example - summarize this text in 20 to 40 words (for text summarization. But the output I get from OpenAI is not too deviated from the range mentioned but at the same time it’s not what I am asking for.

How can I achieve OpenAI generate text for me in the word range I specify?

You can try variations of the phrase “summarize this text in 20 to 40 words” for example: “Summarize the following text in 20 to 40 words: \n\n” or you can provide max_tokens parameter

Thanks Anant for your msg!

I did try with prompts like - “summarize this text in 20 to 40 words”, it does work but I don’t see that it’s bounded to 20 to 40 words everytime.

Secondly, around “max_tokens”, can you help me understand how would this help?
As per “Best practices for prompt engineering with OpenAI API | OpenAI Help Center”:

  1. max_tokens (maximum length) - Does not control the length of the output, but a hard cutoff limit for token generation. Ideally you won’t hit this limit often, as your model will stop either when it thinks it’s finished, or when it hits a stop sequence you defined.

I would recommend trying different phrases in the OpenAI playground till you find the one that works

1 Like

This is hard as it doesn’t usually work. I’ve not found a consistent way to do it other than one-shot or two-shot examples…

I also have very similar experience.

Hi All,

The simple answer to this thread is word count doesn’t work in OpenAI GPT models. But we give a limit or range to word count that works. Below are the examples for both the cases.

Prompt 1: Write a 40 words long article on blockchain. (Doesn’t Work)
Output: It created an article for me with 54 words.

Prompt 2: Write an article on blockchain within 40 words. (Works)
Output: It creates an article with less than 40 words.