<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๋ฅผ ์๋์ ํจ์๋ฅผ ์ด์ฉํด์ ๋ณํํ ์ ์์ต๋๋ค.