Skip to content

Always pass add_generation_prompt=True to apply_chat_template #1416

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 10 additions & 15 deletions src/smolagents/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -536,18 +536,12 @@ def generate(
tools = completion_kwargs.pop("tools", None)
completion_kwargs.pop("tool_choice", None)

if tools_to_call_from is not None:
prompt = self.tokenizer.apply_chat_template(
messages,
tools=tools,
add_generation_prompt=True,
tokenize=False,
)
else:
prompt = self.tokenizer.apply_chat_template(
messages,
tokenize=False,
)
prompt = self.tokenizer.apply_chat_template(
messages,
tools=tools,
add_generation_prompt=True,
tokenize=False,
)

sampling_params = SamplingParams(
n=kwargs.get("n", 1),
Expand Down Expand Up @@ -834,6 +828,7 @@ def _prepare_completion_args(

messages = completion_kwargs.pop("messages")
stop_sequences = completion_kwargs.pop("stop", None)
tools = completion_kwargs.pop("tools", None)

max_new_tokens = (
kwargs.get("max_new_tokens")
Expand All @@ -843,10 +838,10 @@ def _prepare_completion_args(
or 1024
)
prompt_tensor = (self.processor if hasattr(self, "processor") else self.tokenizer).apply_chat_template(
messages, # type: ignore
tools=[get_tool_json_schema(tool) for tool in tools_to_call_from] if tools_to_call_from else None,
messages,
tools=tools,
return_tensors="pt",
add_generation_prompt=True if tools_to_call_from else False,
add_generation_prompt=True,
tokenize=True,
return_dict=True,
)
Expand Down
16 changes: 9 additions & 7 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,14 +163,14 @@ def test_transformers_message_no_tool(self, monkeypatch):
do_sample=False,
)
messages = [{"role": "user", "content": [{"type": "text", "text": "Hello!"}]}]
output = model.generate(messages, stop_sequences=["great"]).content
assert output == "assistant\nHello"
output = model.generate(messages).content
assert output == "Hello! I'm here"

output = model.generate_stream(messages, stop_sequences=["great"])
output_str = ""
for el in output:
output_str += el.content
assert output_str == "assistant\nHello"
assert output_str == "Hello! I'm here"

def test_transformers_message_vl_no_tool(self, shared_datadir, monkeypatch):
monkeypatch.setattr("huggingface_hub.constants.HF_HUB_DOWNLOAD_TIMEOUT", 30) # instead of 10
Expand All @@ -183,15 +183,17 @@ def test_transformers_message_vl_no_tool(self, shared_datadir, monkeypatch):
device_map="cpu",
do_sample=False,
)
messages = [{"role": "user", "content": [{"type": "text", "text": "Hello!"}, {"type": "image", "image": img}]}]
output = model.generate(messages, stop_sequences=["great"]).content
assert output == "I am"
messages = [
{"role": "user", "content": [{"type": "text", "text": "What is this?"}, {"type": "image", "image": img}]}
]
output = model.generate(messages).content
assert output == "This is a very"

output = model.generate_stream(messages, stop_sequences=["great"])
output_str = ""
for el in output:
output_str += el.content
assert output_str == "I am"
assert output_str == "This is a very"

def test_parse_json_if_needed(self):
args = "abc"
Expand Down