<aside> 🔥
LLM의 입력을 모델적으로 어떻게 구성해야 하는지에 대해서 알아봅니다.
</aside>
일반 사용자들은 Chat-GPT같은 생성형 모델을 텍스트를 입력으로 주고 있습니다. 그러나 컴퓨터는 텍스트를 이해하지 않고, 0과 1만 이해할 뿐입니다. 즉, 텍스트를 숫자화를 해줘야만 이해를 할 수 있습니다.
일반 유저가 생각하는 입력과 모델의 입력이 서로 다르기 때문에 이를 어떻게 맞춰줘야 할지에 대해서 생각해봐야 합니다.
chat templates 라는 것을 통해 LLM이 어떻게 생성을 구조화하는지에 대해서 이해해봅시다 ****Now that we understand how LLMs work, let’s look at how they structure their generations through chat templates.
chat templates는 허깅페이스에서 제공하는 prompt를 모델의 입력으로 변환해주는 기능입니다. chat template에 맞춰서 입력을 주기만 하면 알아서 모델의 입력으로 변환해주는 것이죠! 그러면 저희는 chat template에 맞는 입력을 주는 것을 알아봐야 합니다!
우리가 Chat-GPT에 Hello how are you? 라고 텍스트를 입력으로 넣는다고 해봅시다! 그러면 Chat-GPT는 아래와 같은 답변을 한다고 해봅시다!

Chat template (출처 : ‣)
Hello! I'm just a language model, so I don't have feelings or emotions like humans do, but I'm functioning properly and ready to assis you with any questions or tasks you may have. How about you? How's your day going so far?
이 경우에 그 중간에 생략되어 있는 단계가 매우 많습니다! 생략된 단계는 다음과 같습니다!
User의 텍스트 입력 : Hello how are you?
chat template화 : <|begin_of_text|><|start_header_id|>user<|end_header_id|>\\n\\nHello how are you?
토큰화 : [”<|begin_of_text|>”, “<|start_header_id|>”, “user”, “<|end_header_id|>”, “\\n\\n”, “Hello”, “how”, “are”, “you”, “?”]
숫자화 : [2, 3, 10, 23, 523, 4332, 39942, 22143, 3232, 5323]
모델의 입력으로 넣기
모델의 output logit → softmax → Next Token Prediction
Next Token Prediction으로 완성된 Text
<|begin_of_text|><|start_header_id|>user<|end_header_id|>\n\nHello how are you?\n\n<start_header_id>assistant<|end_header_id|>\n\nHello! I'm just a language model, so I don't have feelings or emotions like humans do, but I'm functioning properly and ready to assis you with any questions or tasks you may have. How about you? How's your day going so far?
Hello! I'm just a language model, so I don't have feelings or emotions like humans do, but I'm functioning properly and ready to assis you with any questions or tasks you may have. How about you? How's your day going so far?
이처럼 LLM 모델에 적절한 입력을 주면 적절한 output text가 나오게 됩니다. 그렇지만, 각 모델마다 들어가야 하는 입력의 형태가 다릅니다. 2. chat template화 : <|begin_of_text|><|start_header_id|>user<|end_header_id|>\\n\\nHello how are you? 단계에서 모델마다 special token이 다르고, 어떻게 입력을 넣어야 하는지에 대해서 다르기 때문입니다.
따라서, 각각 다른 입력을 하나로 통일시켜주는 브릿지가 필요합니다. 이것이 chat template입니다! 그렇기에 chat template을 만드는 방법만 안다면 모든 모델의 입력을 만들 수 있습니다! 따라서 chat template에 대해 자세히 알아봅시다!
Messages는 LLM의 입력과 관련된 체계를 의미합니다. Messages 혹은 Prompts 등으로 불릴 수 있기 때문에 다양한 용어가 헷갈리더라도 핵심은 LLM 모델의 입력이라고 생각해주시면 될거 같습니다!
Messages는 크게 3가지로 나눌 수 있습니다.
그러면 하나하나 자세히 알아봅시다!
system message(또는 system prompt)는 어떻게 모델이 행동해야 하는지를 정의하는 텍스트입니다. system messages는 다음 특성을 가집니다.
LLM 모델이 여러번 User와 상호작용하더라도 지속적으로 영향을 주는 Message
예를 들어, “너는 친절한 조언자야” 라고 system message를 주게 되면 user가 LLM 모델에게 계속 여러번 물어보더라도 system message는 동일하게 “너는 친절한 조언자야” 로 입력으로 들어가게 됩니다.
모든 대화 상호작용에 가이드가 됩니다.
예를 들어, 1번 예시처럼 “너는 친절한 조언자야” 라고 system message가 사용되면, 모든 user 입력 및 모델의 답변에 “너는 친절한 조언자야” 라는 system message가 같이 들어가게 되므로 User와 LLM의 모든 티키타카에 영향을 주게 됩니다.
Agent의 예시를 들어봅시다.

assistant message (polite, clear, helpful) (출처 : ‣)
위의 system message를 사용할 경우 좀더 polite, clear, helpful의 특성을 가진 답변이 나오게 됩니다.

assistant message (rabel) (출처 : ‣)
라이벌 관계라는 system message를 준 경우 불친절한 답변이 나오게 됩니다. 이런 식으로 어떤 답변을 줄지 그 맥락 정보를 같이 준다고 생각하면 좀더 이해가 쉬우실 거 같습니다!

Example of system message about tool (출처 : ‣)
Agent에서는 또한 tool에 대한 정보를 포함합니다. tool에 대하여 다음 정보를 포함할 수 있습니다.
보통 system message는 각 LLM 모델에서 기본적으로 사용하는 default값이 있는데요. Agent향의 system message가 아니기 때문에 Agent로 사용하기 위해서는 system message를 새롭게 정의해줘야 합니다!
3가지 message 중에서 system message를 제외한 나머지 user message와 assistant message를 묶어 Conversations라고 합니다. 사람(User)과 LLM(assistant) 사이의 주고 받는 대화라는 의미를 담고 있습니다!
아래와 같은 Chat templates가 있다고 해봅시다!
conversation = [
{"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"},
]
user가 2번, assistant가 1번 있습니다. 이 상황은 다음으로 구성되어 있습니다.
“I need help with my order" 라고 LLM에 물어보고“I'd be happy to help. Could you provide your order number?" 라고 대답한 후에,“It's ORDER-123” 라고 되물은 상황입니다.이러한 Conversations 상황을 Python의 리스트와 딕셔너리의 일정한 형태로 구성한 데이터를 Chat templates라고 합니다.
Chat templates는 사용자와 어시스턴트 간의 이전 대화를 저장하여 대화 맥락을 유지하는 데 도움을 줍니다. 즉, 이전 대화들도 LLM의 입력으로 같이 넣어줘서 과거의 맥락 정보도 고려할 수 있도록 합니다. 이를 통해 여러 차례 이어지는 대화를 더욱 일관되게 만들 수 있습니다. Chat templates는 항상 대화의 모든 메시지를 연결(concatenate)하여 LLM에게 단일한 독립형 시퀀스(sequence)로 전달합니다.
Chat templates의 형식처럼 일관된 형식을 사용하게 되면 huggingface에서 Chat template를 인식하고 다양한 LLM 모델 입력으로 변환해줍니다.
예를 들어, SmolLM2라는 huggingface에서 만든 모델의 LLM 입력은 다음과 같이 변환합니다.
<|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
반면에, Llama3.2 모델의 입력은 다음과 같이 변환합니다.
<|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|>
즉, 우리가 어떤 데이터를 Chat templates 형식으로 만들어내기만 한다면, 저희가 만든 Chat templates형식의 데이터인 Messages를 huggingface에서 제공하는 변환 함수를 통해 사용하고자 하는 LLM 모델의 입력(Prompt)으로 변환할 수 있습니다!
위의 Prompt와 관련해서 이해해야 하는 것은 Base Model 과 Insruct Model 입니다.
즉, Instruct Model은 Chat templates를 이용한 LLM 입력(Prompt)으로 학습을 진행한 모델입니다. 그렇기에 Chat templates를 이용하여 LLM 입력(Prompt)로 모델의 입력을 넣어줘야 높은 성능의 결과물을 얻을 수 있습니다.
위에서 언급했던 말을 다시 상기해봅시다.
즉, 우리가 어떤 데이터를 Chat templates 형식으로 만들어내기만 한다면, 저희가 만든 Chat templates형식의 데이터인 Messages를 huggingface에서 제공하는 변환 함수를 통해 사용하고자 하는 LLM 모델의 입력(Prompt)으로 변환할 수 있습니다!
Messages를 Prompt로 변환하는 방법을 알기 위해서는 huggingface에서 제공하는 변환 함수를 이해한다면 변환 방법에 대해서 이해할 수 있습니다!
위의 message를 아래의 함수를 이용해서 변환할 수 있습니다.