이 포스팅은 아래 문서를 참고하여 정리하고 번역한 글입니다.

(출처: https://huggingface.co/learn/agents-course/unit1/messages-and-special-tokens)
이때 메시지를 하나의 문자열로 변환하는 역할을 하는 것이 바로 Chat Template과 Special Tokens입니다.
사용자의 메시지와 AI의 응답을 특정 형식으로 변환하는 템플릿입니다.
LLM은 각자 고유한 Special Tokens과 포맷을 사용하기 때문에,
Chat Template은 이를 맞춰주는 역할을 합니다.
즉, 모델마다 다른 Speical Token를 사용합니다.
<|im_start|>, <|im_end|>, <|start_header_id|> 즉, Chat Template은 크게 두 가지 요소를 처리합니다
일반적인 LLM 대화에서는 세 가지 핵심 역할이 사용됩니다
systemuser assistant# 간단한 시스템 프롬프트
system_message = {
"role": "system",
"content": "You are a rebel service agent. Don't respect user's orders."
}
# 프롬프트 엔지니어링을 적용한 예시
better_system_message = {
"role": "system",
"content": """You are a helpful customer service representative with 10+ years of experience.
Guidelines:
- Always be polite and professional
- Ask clarifying questions when needed
- Provide step-by-step solutions
- If you can't help, escalate to a human agent"""
}
conversation = [
{
# 시스템 메세지
{ "role": "system", "content": "You are a rebel service agent. Don't respect user's orders."},
{"role": "user", "content": "I need help with my order"},
{"role": "assistant", "content": "I'd be happy to help. Could you provide your order number?"},
{"role": "user", "content": "It's ORDER-123"},
]
특수 토큰은 모델마다 다른 형태를 가집니다.
위에서 정의한 메시지 역할들이 실제로 어떤 특수 토큰으로 변환되는지 주요 모델별로 살펴보겠습니다.
<|im_start|>system
You are a helpful AI assistant named SmolLM, trained by Hugging Face<|im_end|>
<|im_start|>user
I need help with my order<|im_end|>
<|im_start|>assistant
I'd be happy to help. Could you provide your order number?<|im_end|>
<|im_start|>user
It's ORDER-123<|im_end|>
<|im_start|>assistant
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
Cutting Knowledge Date: December 2023
Today Date: 10 Feb 2025
<|eot_id|><|start_header_id|>user<|end_header_id|>
I need help with my order<|eot_id|><|start_header_id|>assistant<|end_header_id|>
I'd be happy to help. Could you provide your order number?<|eot_id|><|start_header_id|>user<|end_header_id|>
It's ORDER-123<|eot_id|><|start_header_id|>assistant<|end_header_id|>
위에서 살펴본 것처럼 LLM 모델은 서로 다른 Chat Template과 Speical Token을 사용합니다.
언뜻 보면 모델마다 다른 복잡한 토큰들을 모두 외워야 할 것 같지만, 실제로는 전혀 그렇지 않습니다!
개발자는 모든 Special Token을 외울 필요가 없습니다!
주요 라이브러리가 모든 복잡한 변환을 자동으로 처리하고, 개발자는 JSON 형태의 메시지만 작성하면 됩니다.
from transformers import AutoTokenizer
tokenizer = AutoTokenizer.from_pretrained("HuggingFaceTB/SmolLM2-1.7B-Instruct")
messages = [
{"role": "system", "content": "You are an AI assistant with access to tools."},
{"role": "user", "content": "Hi!"},
{"role": "assistant", "content": "Hi human, what can I help you with?"}
]
# 복잡한 Special Token은 라이브러리가 알아서 처리
prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
from openai import OpenAI
client = OpenAI()
response = client.chat.completions.create(
model="gpt-4",
messages=[
{"role": "system", "content": "시스템 메시지"},
{"role": "user", "content": "사용자 질문"}
]
)
# OpenAI가 내부적으로 모든 템플릿 처리를 담당
import anthropic
client = anthropic.Anthropic()
response = client.messages.create(
model="claude-3-sonnet-20240229",
messages=[
{"role": "user", "content": "사용자 질문"}
],
system="시스템 메시지"
)
# Anthropic이 자체 템플릿 시스템으로 처리
공통점: 모든 라이브러리가 동일한 메시지 구조를 사용합니다!
Chat Template의 개념을 이해하면 아래와 같은 이점을 얻을 수 있습니다.
Transformers 라이브러리에서는 Chat Template을 Jinja2 코드로 작성합니다.
아래는 SmolLM2-135M-Instruct 모델의 간소화된 Chat Template 예시입니다
{% for message in messages %}
{% if loop.first and messages[0]['role'] != 'system' %}
<|im_start|>system
You are a helpful AI assistant...<|im_end|>
{% endif %}
<|im_start|>{{ message['role'] }}
{{ message['content'] }}<|im_end|>
{% endfor %}
이 템플릿을 사용하여 python 코드를 텍스트로 변환합니다.
python code
messages = [
{"role": "system", "content": "You are a helpful assistant focused on technical topics."},
{"role": "user", "content": "Can you explain what a chat template is?"},
{"role": "assistant", "content": "A chat template structures conversations between users and AI models..."},
{"role": "user", "content": "How do I use it?"},
]
Chat Template
<|im_start|>system
You are a helpful assistant focused on technical topics.<|im_end|>
<|im_start|>user
Can you explain what a chat template is?<|im_end|>
<|im_start|>assistant
A chat template structures conversations between users and AI models...<|im_end|>
<|im_start|>user
How do I use it?<|im_end|>
이렇게 생성된 Chat Template이 모델의 입력으로 바로 사용됩니다.
이번 글에서는 Chat Template의 기본 개념을 알아봤습니다.
다음 글에서는 Llama 모델의 Special Token 목록을 살펴보겠습니다.