In Python, how do I set the stop strings for AutoModelForCausalLM?
Private research annotation
Sign in to add a private research label or note.
- User
In Python, how do I set the stop strings for AutoModelForCausalLM?
Assistant
Sign in to add a private research label or note.
In Python, how do I set the stop strings for AutoModelForCausalLM?
You can set the stop strings for AutoModelForCausalLM in a few ways, depending on your needs:
1. Using tokenizer.stop_tokens:
This is the most common and flexible method. You can directly specify a list of stop tokens to be removed from the input text.
Example:
from transformers import AutoModelForCausalLM, AutoTokenizer
model_name = "gpt2"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)
# Define your stop tokens
stop_tokens = ["the", "a", "an", "is", "are", "was", "were", "be", "been", "being"]
# Add stop tokens to the tokenizer
tokenizer.add_special_tokens({"pad_token": "##", "eos_token": "##"})
tokenizer.add_special_tokens({"stop_tokens": stop_tokens})
# Now you can use the tokenizer with the stop tokens
input_text = "This is a test sentence with some stop words."
input_ids = tokenizer.encode(input_text, add_special_tokens=True)
2. Using model.config.stop_tokens:
This method allows you to set the stop tokens directly within the model's configuration.
Example:
from transformers import AutoModelForCausalLM, AutoTokenizer
model_name = "gpt2"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)
# Set the stop tokens
model.config.stop_tokens = ["the", "a", "an", "is", "are", "was", "were", "be", "been", "being"]
# Now you can use the model with the configured stop tokens
input_text = "This is a test sentence with some stop words."
output = model.generate(input_text, max_length=50, do_sample=True)
3. Using model.config.eos_token:
This method allows you to set the end-of-sentence token.
Example:
from transformers import AutoModelForCausalLM, AutoTokenizer
model_name = "gpt2"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)
# Set the end-of-sentence token
model.config.eos_token = "##"
# Now you can use the model with the configured end-of-sentence token
input_text = "This is a test sentence with some stop words."
output = model.generate(input_text, max_length=50, do_sample=True)
Important Notes:
tokenizer.stop_tokens offers the most flexibility, allowing you to customize stop tokens for specific tasks or datasets.model.config.stop_tokens directly sets the stop tokens for the model's internal processing.model.config.eos_token is used to define the end-of-sentence token.Remember to choose the method that best suits your needs and the specific requirements of your task.
“In Python, how do I set the stop strings for AutoModelForCausalLM?” Conversation with gemma-2-2b-it, from the LMArena Human Preference 100K dataset. The Chat Archive, 2026. https://thechatarchive.com/c/01m850wmzzy5mahwydjkqf7s6f
Not yet included in a published release.