def hf_chat_template(model: str, messages: list, chat_template: Optional[Any] = None):
  # ... more code
    env = Environment()
    env.globals["raise_exception"] = raise_exception
    try:
        template = env.from_string(chat_template)
    except Exception as e:
        raise e