|
10 | 10 | from time import sleep |
11 | 11 | from typing import TYPE_CHECKING, Dict, List, Literal, Optional, Tuple, Union |
12 | 12 |
|
| 13 | +import jinja2 |
13 | 14 | from more_itertools import distribute |
14 | 15 | from packaging.version import parse as parse_version |
15 | 16 | from tqdm import tqdm |
@@ -300,14 +301,27 @@ def apply_chat_template( |
300 | 301 | """ |
301 | 302 | Method to apply a chat template to a list of chat history between user and model. |
302 | 303 | """ |
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 | + ) |
311 | 325 |
|
312 | 326 | return chat_templated |
313 | 327 |
|
|
0 commit comments