from langchain.llms import OpenAI
import os
os.environ["OPENAI_API_KEY"] = "my api keys"
llm = OpenAI(temperature=0.6)
text = "Write about bangladesh" + "Donot generate any hashtags end the end" + "donot write more than 50 words"
output = llm.predict(text)
print(len(output))
print(output)
print(len(output.split()))
outputs :
492
Bangladesh, located in South Asia, is a country known for its lush green landscapes, vibrant culture, and resilient people. With a population of over 160 million, it is the 8th most populous country in the world. Despite facing various challenges, Bangladesh has made significant progress in areas such as education, health, and economy. The country's main industries include agriculture, textile, and tourism. Bangladesh is also home to the world's largest mangrove forest, the Sundarbans.
74
As a user I will provide a prompt between 100 characters the gpt model provide me informations between under 100 words about given prompt
Firstly it looks as if you are using gpt-3.5-turbo-instruct rather than gpt-3.5-turbo-0125. You can print the model name from your code by using:
The consensus seems to be that there is no foolproof way to force any of the gpt LLMs to reliably set or limit the word length of their output. But the code below works reasonably well. In it I have set the temperature to zero. This means that the same prompt should always give rise to the same output. This means that if you do get a particularly long answer on a specific subject, you can explore adjusting the prompt to get a shorter answer.
I have also modified the prompt, asking for further editing down to the word limit, which seems to improve length limitation, and have set the word limit to 90. You mention a 100 word limit in your question, and I have found that the current prompt and a specified 90 word limit gives a good clustering of lengths around 100, with most falling within +/- 10%.
I have also added an input statement so that it is easy to test with different topics
Remember that gpt behaves as if it had a mind of its own, and will sometimes throw up unexpected results. This probably goes for the length of its replies as well as its content!
Finally, note that although this prompt works for responses of around 100 words, you would need to explore other prompts for larger word counts.