Skip to content

Commit

Permalink
Change _fix_chat_template in case a template has both endif and endfor (
Browse files Browse the repository at this point in the history
  • Loading branch information
giuliabaldini authored Dec 20, 2024
1 parent e49e5e1 commit 9ef3c7a
Showing 1 changed file with 27 additions and 10 deletions.
37 changes: 27 additions & 10 deletions unsloth/tokenizer_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -585,26 +585,43 @@ def load_correct_tokenizer(
pass


def _find_end_position(template, endfor, endif):
where_endfor = template.find(endfor)
where_endif = template.find(endif)
if where_endfor == where_endif == -1:
return None
elif where_endfor > where_endif:
return endfor
else:
return endif
pass
pass


def _fix_chat_template(chat_template):
endfor = "{% endif %}"
where = chat_template.find(endfor)
if where == -1:
endfor = "{%- endif %}"
where = chat_template.find(endfor)
if where == -1:
endfor = "{% endfor %}"
endif = "{% endif %}"
chosen_end = _find_end_position(chat_template, endfor, endif)
if chosen_end is None:
endfor = "{%- endfor %}"
endif = "{%- endif %}"
chosen_end = _find_end_position(chat_template, endfor, endif)
if chosen_end is None:
return chat_template

where = chat_template.find(chosen_end)

after_endfor = chat_template[where + len(endfor):]
after_endfor = chat_template[where + len(chosen_end):]

dash = "-" if endfor.startswith("{%-") else ""
dash = "-" if chosen_end.startswith("{%-") else ""

if "{%" + dash + " if" not in after_endfor and "{%" + dash + " set " not in after_endfor and \
after_endfor.startswith("{{") and after_endfor.endswith("}}") and \
after_endfor.count("{{") == 1 and after_endfor.count("}}") == 1:

after_endfor = "{%" + dash + " if add_generation_prompt %}" + after_endfor + endfor
after_endfor = "{%" + dash + " if add_generation_prompt %}" + after_endfor + endif

chat_template = chat_template[:where + len(endfor)] + after_endfor
chat_template = chat_template[:where + len(chosen_end)] + after_endfor
pass
return chat_template
pass
Expand Down

0 comments on commit 9ef3c7a

Please sign in to comment.