Skip to content

Commit 0f63d4f

Browse files
authored
remove system message if TemplateError (#3076)
1 parent 532909c commit 0f63d4f

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

lm_eval/models/vllm_causallms.py

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from time import sleep
1111
from typing import TYPE_CHECKING, Dict, List, Literal, Optional, Tuple, Union
1212

13+
import jinja2
1314
from more_itertools import distribute
1415
from packaging.version import parse as parse_version
1516
from tqdm import tqdm
@@ -300,14 +301,27 @@ def apply_chat_template(
300301
"""
301302
Method to apply a chat template to a list of chat history between user and model.
302303
"""
303-
chat_templated = self.tokenizer.apply_chat_template(
304-
chat_history,
305-
tokenize=False,
306-
add_generation_prompt=add_generation_prompt,
307-
continue_final_message=not add_generation_prompt,
308-
chat_template=self.hf_chat_template,
309-
enable_thinking=self.enable_thinking,
310-
)
304+
try:
305+
chat_templated = self.tokenizer.apply_chat_template(
306+
chat_history,
307+
tokenize=False,
308+
add_generation_prompt=add_generation_prompt,
309+
continue_final_message=not add_generation_prompt,
310+
chat_template=self.hf_chat_template,
311+
enable_thinking=self.enable_thinking,
312+
)
313+
except jinja2.exceptions.TemplateError:
314+
eval_logger.warning(
315+
"Failed to apply chat template. removing the system role in chat history."
316+
)
317+
chat_templated = self.tokenizer.apply_chat_template(
318+
[msg for msg in chat_history if msg["role"] != "system"],
319+
tokenize=False,
320+
add_generation_prompt=add_generation_prompt,
321+
continue_final_message=not add_generation_prompt,
322+
chat_template=self.hf_chat_template,
323+
enable_thinking=self.enable_thinking,
324+
)
311325

312326
return chat_templated
313327

0 commit comments

Comments
 (0)