code: def hf_chat_template(
      model: str, messages: list, chat_template: Optional[Any] = None
  ):
      ## get the tokenizer config from huggingface
      bos_token = ""
      eos_token = ""
      if chat_template is None:
  
          def _get_tokenizer_config(hf_model_name):
              url = f"https://huggingface.co/{hf_model_name}/raw/main/tokenizer_config.json"
              # Make a GET request to fetch the JSON data
              response = requests.get(url)
              if response.status_code == 200:
                  # Parse the JSON data
                  tokenizer_config = json.loads(response.content)
                  return {
                      "status": "success",
                      "tokenizer": tokenizer_config,
                  }
              else:
                  return {"status": "failure"}
  
          tokenizer_config = _get_tokenizer_config(model)
          if (
              tokenizer_config["status"] == "failure"
              or "chat_template" not in tokenizer_config["tokenizer"]
          ):
              raise Exception("No chat template found")
          ## read the bos token, eos token and chat template from the json
          tokenizer_config = tokenizer_config["tokenizer"]
          bos_token = tokenizer_config["bos_token"]
          eos_token = tokenizer_config["eos_token"]
          chat_template = tokenizer_config["chat_template"]
  
      def raise_exception(message):
          raise Exception(f"Error message - {message}")
  
      # Create a template object from the template text
      env = Environment()
      env.globals["raise_exception"] = raise_exception
      try:
          template = env.from_string(chat_template)
      except Exception as e:
          raise e
  
      def _is_system_in_template():
          try:
              # Try rendering the template with a system message
              response = template.render(
                  messages=[{"role": "system", "content": "test"}],
                  eos_token="<eos>",
                  bos_token="<bos>",
              )
              return True
  
          # This will be raised if Jinja attempts to render the system message and it can't
          except:
              return False
  
      try:
          # Render the template with the provided values
          if _is_system_in_template():
              rendered_text = template.render(
                  bos_token=bos_token,
                  eos_token=eos_token,
                  messages=messages,
              )
          else:
              # treat a system message as a user message, if system not in template
              try:
                  reformatted_messages = []
                  for message in messages:
                      if message["role"] == "system":
                          reformatted_messages.append(
                              {
                                  "role": "user",
                                  "content": message["content"],
                              }
                          )
                      else:
                          reformatted_messages.append(message)
                  rendered_text = template.render(
                      bos_token=bos_token,
                      eos_token=eos_token,
                      messages=reformatted_messages,
                  )
              except Exception as e:
                  if (
                      "Conversation roles must alternate user/assistant"
                      in str(e)
                  ):
                      # reformat messages to ensure user/assistant are alternating, if there's either 2 consecutive 'user' messages or 2 consecutive 'assistant' message, add a blank 'user' or 'assistant' message to ensure compatibility
                      new_messages = []
                      for i in range(len(reformatted_messages) - 1):
                          new_messages.append(reformatted_messages[i])
                          if (
                              reformatted_messages[i]["role"]
                              == reformatted_messages[i + 1]["role"]
                          ):
                              if (
                                  reformatted_messages[i]["role"]
                                  == "user"
                              ):
                                  new_messages.append(
                                      {
                                          "role": "assistant",
                                          "content": "",
                                      }
                                  )
                              else:
                                  new_messages.append(
                                      {"role": "user", "content": ""}
                                  )
                      new_messages.append(reformatted_messages[-1])
                      rendered_text = template.render(
                          bos_token=bos_token,
                          eos_token=eos_token,
                          messages=new_messages,
                      )
          return rendered_text
      except Exception as e:
          raise Exception(f"Error rendering template - {str(e)}")
  
imports: from enum import Enum
  import requests, traceback
  import json
  from jinja2 import Template, exceptions, Environment, meta
  from typing import Optional, Any
label_desc: Use image node_id 11 for calling a global function with example usage: hf_chat_template(model, messages, chat_template) and returns: rendered_text, True, dict, dict, False