The chat templates used in the Hugging Face 🤗 Transformers library generally follow a standardized conversational format that structures user and assistant (or system) messages using specific tokens or roles to delineate turns and roles in a dialogue. However, it's important to note that there isn't one single universal format enforced across all models — instead, each model (especially instruction-tuned or chat-tuned ones like LLaMA, ChatGLM, Falcon, Mistral, etc.) often comes with its own predefined chat template, tailored to how the model was trained.
That said, there are common conventions that many of these templates follow:
🔧 Common Format Elements
-
Role Indicators
<s>[INST] ... [/INST] — Used by models like LLaMA 2.
### Human: and ### Assistant: — Used by Falcon and others.
<|im_start|>user and <|im_end|> + <|im_start|>assistant — Used by OpenChat, Mistral, and other TII models.
USER: and ASSISTANT: — Sometimes used in older or simpler templates.
<|User|> and <|Bot|> — Used in some multilingual or community models.
-
System Prompts (Optional)
-
End-of-Turn Tokens
- Most templates include special tokens to indicate the end of a user or assistant message, such as:
-
Input Formatting
📦 How the Transformers Library Handles This
In the Transformers library (as of version 4.30+), chat templates are managed via the 🤗 Dialog framework and the transformers' PreTrainedTokenizer or AutoTokenizer class, especially when using models from the transformers Chat API or transformers's Conversation classes.
Key tools/interfaces:
AutoTokenizer.get_chat_template(model_name) – returns the chat template string for a given model.
tokenizer.apply_chat_template() – applies the template to a list of messages.
transformers.ChatTemplate (experimental): A more structured way to define templates.
Example usage:
from transformers import AutoTokenizer
tokenizer = AutoTokenizer.from_pretrained("meta-llama/Llama-2-7b-chat-hf")
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "What is the capital of France?"}
]
chat_str = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
print(chat_str)
📘 Examples of Model-Specific Templates
-
LLaMA 2
<s>[INST] <<SYS>>You are a helpful assistant.<</SYS>>
User: Hello
Assistant: [/INST]
(Note: newer versions use a simplified format with just <s>[INST] User: ... Assistant:)
-
Mistral / Mixtral
<|im_start|>user
Hello
<|im_end|>
<|im_start|>assistant
Hi there!
<|im_end|>
-
Falcon
### Human: Hello
### Assistant: Hi there!
🛠️ Customizing or Adding Templates
You can define your own chat template and register it to a tokenizer using tokenizer.chat_template = "<your-template>", or contribute to the Hugging Face chat template registry.
✅ Summary
| Feature | Description |
|---|
| Standardization | Varies by model; no strict global format |
| Common delimiters | <s>, </s>, [INST], [/INST], `< |
| Roles | Usually user, assistant, sometimes system |
| Tokenizer Support | apply_chat_template(), get_chat_template() |
| Template Registry | Available in Hugging Face docs and codebase |
For best results, always refer to the specific model card or tokenizer config to see which chat template it uses.