# Base Model, Instruct Model
Base Model: ๋จ์ํ ๋ค์ ํ ํฐ ์์ธก์ด๋ผ๋ ๋ชฉํ๋ก ์ฌ์ ํ์ต๋ง์ ๊ฑฐ์น ๋ชจ๋ธ
Instruct Model: ํน์ ํ ๋ชฉ์ ์ ํ์คํฌ๋ฅผ ์ํํ๋๋ก ๋ณ๋์ ํ์ธํ๋์ ๊ฑฐ์น ๋ชจ๋ธ
์๋ ์ฌ์ง์ฒ๋ผ,
์๋ฌด๊ฒ๋ ๋ถ์ด์์ง ์์ผ๋ฉด base์ด๊ณ instruct ๋ชจ๋ธ์ ๋ค์ Instruct, it, chat ๋ฑ ๋ญ๊ฐ ์ถ๊ฐ๋ก ๋ถ์ด ์๋ค.
์ฝ๊ฒ ๋งํ๋ฉด GPT์ ChatGPT ๊ฐ์ ๋๋์ด๋ค.
ChatGPT๋ ์๋๋ InstructGPT๋ผ๋ ๋ชจ๋ธ์ ๋ฒ ์ด์ค๋ก ํ๋๋ฐ,
์ด ๋ชจ๋ธ์ด ๋ฐ๋ก ์ฌ์ฉ์์ ์
๋ ฅ์ ๋ง๋ ์ ์ ํ ์๋ต์ ์์ฑํ๋๋ก ๋ณ๋๋ก ํ์ต์ด ๋ ๋ชจ๋ธ์ด๋ค.
๋ฌผ๋ก ๋ณธ์ง์ ์ผ๋ก ๋ค์ ํ ํฐ์ ์์ธกํ๋ค๋ ์ฌ์ค์ ๋ณํ์ง ์์ง๋ง,
์ฌ์ ํ์ต๋ง ๋ Base Model์ ๋ง ๊ทธ๋๋ก, ์
๋ ฅ์ด ์ง๋ฌธ์ธ์ง ์๋์ง์๋ ๊ด์ฌ์ด ์๊ณ ๊ทธ๋ด ๋ฏํ ๋ฌธ์ฅ์ ์ด์ด์ ์์ฑํ๊ธฐ๋ง ํ๋ค. ๋ฐ๋ฉด Instruct Model์ ๋ช
์์ ์ผ๋ก ์ง๋ฌธ์ ๋ํ ์๋ต์ ์์ฑํ๋๋ก ์ถ๊ฐ์ ์ผ๋ก ํ๋ จ์ด ๋์๋ค๋ ์ฐจ์ด๊ฐ ์๋ค.
# Chat Template
์ฐ๋ฆฌ๊ฐ LLM์ ์ฌ์ฉํ๋ ๋ชฉ์ ์ ๋๋ถ๋ถ ์ง๋ฌธ์ ๋ํ ๋๋ต์ ์ป๊ฑฐ๋, ์ด๋ค ์์ ์ ์ํํ๋๋ฐ ์๋ค.
๊ทธ๋ฌ๋ฉด ๋จ์ํ Instruct Model์ ์ฌ์ฉํ๋ ๊ฒ๋ง์ผ๋ก ํด๊ฒฐ์ด ๋ ๊น?
๋ง๊ธด ํ์ง๋ง, ์ด ๋ชจ๋ธ์ ์ฌ์ฉํ ๋๋ ์ฌ๋ฐ๋ฅธ ํ๋กฌํํธ๋ฅผ ๋๊ฒจ์ฃผ๋ ๊ฒ์ด ํ์ํ๋ค.
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM
device = "cuda" if torch.cuda.is_available() else "cpu"
# base_model = "meta-llama/Llama-3.2-1B"
base_model = "meta-llama/Llama-3.2-1B-Instruct"
tokenizer = AutoTokenizer.from_pretrained(base_model)
model = AutoModelForCausalLM.from_pretrained(base_model).to(device)
raw_input = "What is Large Language Model?"
encoded_input = tokenizer(raw_input, return_tensors="pt").to(device)
outputs = model.generate(**encoded_input, max_new_tokens=20)
print(tokenizer.decode(outputs[0]))
# <|begin_of_text|>What is Large Language Model? (with examples and applications)
# A large language model is a type of artificial intelligence (AI) model
์ถ๋ ฅ์ ์ดํด๋ณด๋ฉด, ์ธ๋ป ์ง๋ฌธ์ ๋ต์ ํ๋ ๊ฒ์ฒ๋ผ ๋ณด์ด์ง๋ง ์ด์ํ ๋ถ๋ถ์ด ์๋ค.
(with examples and applications)๋ผ๋ ๋ฌธ์ฅ์ ๋ง์๋๋ก ์ถ๊ฐํ๊ณ , ๊ทธ ๋ค์์ ๊ทธ์ ๋ํ ๋๋ต์ ์์ฑํ๊ณ ์๊ธฐ ๋๋ฌธ์ด๋ค.
๊ทธ ์ด์ ๋, LLM์ ๋์ ์๋ฆฌ๋ฅผ ์ดํด๋ณด๋ฉด ์ดํดํ ์ ์๋ค.
์ฌ์ฉ์๊ฐ LLM์ ๋๊ฒจ์ฃผ๋ ์ ๋ ฅ์ ์ฌ์ค ์๋ ๊ทธ๋๋ก ๋ค์ด๊ฐ์ง ์๋๋ค.
์ฌ์ฉ์๊ฐ ์ ๋ ฅํด์ค ์๋ณธ ๋ฌธ์ฅ์ Query๋ผ๊ณ ํ๋ฉด, ์ด ์ฟผ๋ฆฌ ์๋ค์ ๋ค์ํ ์์๊ฐ ๋ถ์ด์ LLM์ผ๋ก ์ ๋ ฅ๋๋ค.
์์คํ ํ๋กฌํํธ
LLM์ ์ ๋ ฅ๋๋ ํ๋กฌํํธ๋ ๋ค์ํ ๊ตฌ์ฑ๋๋ค.
- ์์คํ ํ๋กฌํํธ
- ์ง์์ฌํญ
- ์ฌ์ฉ์์ ์ ๋ ฅ
{system} ๋น์ ์ ์ ์ฉํ AI ์ด์์คํดํธ์ ๋๋ค. {/system} {instruction} ์ง๋ฌธ์๋ ํญ์ ํ๊ตญ์ด๋ก ๋๋ตํ์ธ์. {/instruction} {user} Large Language Model์ด ๋ฌด์์ธ๊ฐ์? {/user} {assistant}โ
์์คํ ํ๋กฌํํธ์๋ LLM์ด ์ด๋ค ์ง์นจ์ ๋ฐํ์ผ๋ก ์๋ต์ ์์ฑํด์ผ ํ๋์ง๊ฐ ๋ช ์๋์ด ์๊ธฐ ๋๋ฌธ์, ๊ฐ๋ก๊ฐ์ด ๋งํด๋ ์ฐฐ๋ก๊ฐ์ด ์์๋ฃ๋ ๊ฒฝ์ฐ๊ฐ ๋ง๋ค.
์ฐ๋ฆฌ๊ฐ LLM์ ์ ๋ ฅํ๋ ๋ด์ฉ์ {user} {\user} ์ฌ์ด์ ๋ค์ด๊ฐ๋ ๋ด์ฉ์ด๊ฒ ์ง๋ง,
์ฑํ ์ ์ํด ํ์ต๋ LLM์ด ์ค์ ๋ก ํ์ต ๊ณผ์ ์์ ์ ๋ ฅ ๋ฐ๋ ํ๋กฌํํธ์ ์ ๋ฌธ์ ์์ ๊ฐ๋ค.
๋ฐ๋ผ์ Inference ๋จ๊ณ์์๋ ์์ ๊ฐ์ ํ์์ ๋ง์ถฐ์ ์ ๋ ฅ์ ์ ๋ฌํด์ค์ผ ํ๋ค.
์ด๋ฅผ ์ฑํ ํ ํ๋ฆฟ Chat Template์ด๋ผ๊ณ ํ๋ค.
Meta์์๋ ๋ฒ์ ๋ณ๋ก Llama ๋ชจ๋ธ์ Prompt Template์ ๋ํ ๊ฐ์ด๋๋ฅผ ์์ธํ๊ฒ ์ ๊ณตํ๋ค.
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
Cutting Knowledge Date: December 2023
Today Date: 23 July 2024
You are a helpful assistant<|eot_id|><|start_header_id|>user<|end_header_id|>
What is the capital of France?<|eot_id|><|start_header_id|>assistant<|end_header_id|>
<|begin_of_text|>: ๋ง ๊ทธ๋๋ก ํ๋กฌํํธ์ ์์์์ ์๋ฆฌ๋ ์ฉ๋
<|start_header_id|>, <|end_header_id|>: ํน์ ํ ์ฃผ์ฒด์ ํด(Turn)์์ ์๋ฆฌ๋ ์ฉ๋ (system, user, assistant)
<|eot_id|>: end of turn์ ์ค์๋ง๋ก, ํน์ ํ ์ฃผ์ฒด์ ํด์ด ๋๋ฌ์์ ์๋ฆฌ๋ ์ฉ๋
์ ํ ํ๋ฆฟ์์๋ ์์คํ ํ๋กฌํํธ์ LLM์๊ฒ ๊ฐ๋จํ ์ง์์ฌํญ์ ์ค ํ์ ์์คํ ํ๋กฌํํธ์ ํด์ด ๋๋ฌ์์ ์๋ฆฌ๊ณ , ์ด์ด์ ์ฌ์ฉ์๊ฐ ํ๋์ค์ ์๋๊ฐ ์ด๋์ง๋ฅผ ๋ฌผ์ ํ ์ง๋ฌธํ๋ ํด์ด ๋๋ฌ์์ ๋ช ์ํ๋ค.
์ด์ ์ด ํ๋กฌํํธ๋ฅผ ์ ๋ ฅ๋ฐ์ LLM์ ํ๋์ค์ ์๋๊ฐ ์ด๋์ง๋ฅผ ๋๋ตํ ํ assistant์ ํด์ ์ข ๋ฃํ๊ฒ ์ฃ .
๋งค๋ฒ ์ด๋ฐ ํ ํ๋ฆฟ์ ๋ง์ถฐ ํ๋กฌํํธ๋ฅผ ์ ๋ ฅํ๋๊ฑด ๋งค์ฐ ๊ท์ฐฎ์ ์ผ์ด๋ค.
transformers ๋ผ์ด๋ธ๋ฌ๋ฆฌ์๋ ๊ฐ ๋ชจ๋ธ์ ๋ง๊ฒ Chat Template์ ์๋์ผ๋ก ๊ตฌ์ฑํด์ฃผ๋ ๊ธฐ๋ฅ์ด ์๋ค.
messages = [{"role": "user", "content": "What is Large Language Model?"}]
encoded_prompt = tokenizer.apply_chat_template(messages,
add_generation_prompt=True,
return_tensors="pt").to(device)
outputs = model.generate(encoded_prompt, max_new_tokens=50)
print(tokenizer.decode(outputs[0]))
"""
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
Cutting Knowledge Date: December 2023
Today Date: 01 Jan 2025
<|eot_id|><|start_header_id|>user<|end_header_id|>
What is Large Language Model?<|eot_id|><|start_header_id|>assistant<|end_header_id|>
A Large Language Model (LLM) is a type of artificial intelligence (AI) model that is designed to process and understand human language. It is a type of neural network that uses deep learning techniques to analyze and generate text.
A Large Language Model
"""
max_new_tokens ๊ฐ ๊ธธ์ง ์์์ ์๋ต์ด ์๋ ธ์ง๋ง,
์๋ฌดํผ ์ด์ ๋ ์ง๋ฌธ์ ๋ญ๊ฐ๋ฅผ ๋ง๋ถ์ด์ง ์๊ณ ๋๋ต๋ง์ ์์ฑํ๊ณ ์์์ ํ์ธํ ์ ์๋ค.
์ฐธ๊ณ ๋ก ์๋ต์ ๋ง์น๋ฉด ์๋์ ๊ฐ์ด ๋์ <|eot_id|> ๊ฐ ์์ฑ๋๋ค.
max_length ๋ฅผ ๋ชจ๋ ์ฑ์ฐ์ง ์์๋, ์ง๋ฌธ์ ๋ํ ๋ต์ ์ถฉ๋ถํ ํ๋ค๊ณ ์๊ฐ๋๋ฉด ํด์ ๋ง์น๋ค๋ ์๋ฏธ์ ์คํ์ ํ ํฐ์ ๋ง์ง๋ง์ผ๋ก ์ถ๋ ฅํ๊ณ , ์์ฑ์ ๋๋ด๋ ๊ฒ์ด๋ค.
Instruct ๋ชจ๋ธ์ ์ฌ์ฉํ ๋๋ ๋ฐ๋์ Chat Template์ ๋ง์ถฐ์ ์ ๋ ฅ์ ๋ฃ์ด์ค์ผ ํ๋ค.
LLM์๋ ๋จ์ํ ์ฌ์ฉ์๊ฐ ์ง์ ์ ์ผ๋ก ์์ฑํ ์ฟผ๋ฆฌ๋ง ์ ๋ ฅ๋๋ ๊ฒ ์๋๋ค. (์์คํ ํ๋กฌํํธ ๋ฑ)
ex.
*๋จ์ํ ์ฌ์ฉ์ ์ฟผ๋ฆฌ๋ง ์ฃผ๋ ๊ฒฝ์ฐ๋ง์ ์๊ฐํ๊ธฐ ์ฝ์ง๋ง*
์ฌ์ฉ์: ํ๋ฆฌ๋ ์ด๋ ๋๋ผ์ ์๋์ผ?
*์ค์ LLM ์ ๋ ฅ์ ์ด๋ ๊ฒ ๋ ํ๋ถํ๋ค:*
<|system|> ๋น์ ์ ์น์ ํ๊ณ ๋ ผ๋ฆฌ์ ์ธ AI์ ๋๋ค. <|end|>
<|user|> ํ๋ฆฌ๋ ์ด๋ ๋๋ผ์ ์๋์ผ? <|end|>