지식을 작은 LLM에 증류하기
Source: Dev.to

대형 언어 모델(LLM)은 AI의 마법과 같습니다. 이러한 수십억‑ 및 수조‑파라미터 규모의 모델은 충분한 데이터를 학습하면 일반화 능력이 뛰어납니다.
하지만 큰 문제는 실행이 어렵고 비용이 많이 든다는 점입니다. 그래서 많은 개발자들이 OpenAI나 Claude와 같은 API를 통해 LLM을 호출합니다. 실제로 개발자들은 복잡한 프롬프트 로직을 만들어 가장자리 사례들을 다루는 데 많은 시간을 투자하는데, 이는 모든 규칙을 처리하려면 거대한 모델이 필요하다고 믿기 때문입니다.
비즈니스 프로세스에 대한 진정한 제어권을 원한다면 로컬 모델을 운영하는 것이 더 좋은 선택입니다. 좋은 소식은 반드시 수십억 파라미터의 거대 모델일 필요는 없다는 것입니다. 작은 LLM을 파인튜닝하면 특정 비즈니스 로직을 처리하고, 프롬프트 복잡성을 줄이며, 모든 것을 사내에서 관리할 수 있습니다.
이 글에서는 지식을 작은 LLM으로 증류하는 방법을 소개합니다.
종속성 설치
txtai와 필요한 라이브러리를 설치합니다:
pip install txtai[pipeline-train] datasets
LLM
우리는 이 예제에서 600 M 파라미터 Qwen‑3 모델을 사용할 것입니다. 목표 작업은 사용자 요청을 Linux 명령어로 변환하는 것입니다.
from txtai import LLM
llm = LLM("Qwen/Qwen3-0.6B")
기본 모델 테스트
llm("""
Translate the following request into a linux command. Only print the command.
Find number of logged in users
""", maxlength=1024)
출력
ps -e
모델은 요청을 이해하지만 명령어가 정확하지 않습니다. 파인튜닝을 통해 개선해 보겠습니다.
Source:
LLM을 지식으로 파인튜닝하기
600 M 모델이라도 도메인‑특화 지식을 증류하여 향상시킬 수 있습니다. 여기서는 Hugging Face의 Linux commands dataset와 txtai의 학습 파이프라인을 사용할 것입니다.
학습 데이터셋 만들기
"""
Translate the following request into a linux command. Only print the command.
{user request}
"""
from datasets import load_dataset
from transformers import AutoTokenizer
# 모델 경로
path = "Qwen/Qwen3-0.6B"
tokenizer = AutoTokenizer.from_pretrained(path)
# 학습 데이터셋 로드
dataset = load_dataset("mecha-org/linux-command-dataset", split="train")
def prompt(row):
text = tokenizer.apply_chat_template([
{"role": "system", "content": "Translate the following request into a linux command. Only print the command."},
{"role": "user", "content": row["input"]},
{"role": "assistant", "content": row["output"]}
], tokenize=False, enable_thinking=False)
return {"text": text}
# 학습 프롬프트로 매핑
train = dataset.map(prompt, remove_columns=["input", "output"])
모델 학습
from txtai.pipeline import HFTrainer
trainer = HFTrainer()
model = trainer(
"Qwen/Qwen3-0.6B",
train,
task="language-generation",
maxlength=512,
bf16=True,
per_device_train_batch_size=4,
num_train_epochs=1,
logging_steps=50,
)
파인튜닝된 모델 평가
from txtai import LLM
llm = LLM(model)
# 예시 1
llm([
{"role": "system", "content": "Translate the following request into a linux command. Only print the command."},
{"role": "user", "content": "Find number of logged in users"}
])
출력
who | wc -l
# 예시 2
llm([
{"role": "system", "content": "Translate the following request into a linux command. Only print the command."},
{"role": "user", "content": "List the files in my home directory"}
])
출력
ls ~/
# 예시 3
llm([
{"role": "system", "content": "Translate the following request into a linux command. Only print the command."},
{"role": "user", "content": "Zip the data directory with all its contents"}
])
출력
zip -r data.zip data
시스템 프롬프트를 명시하지 않아도 모델이 동작합니다:
llm("Calculate the total amount of disk space used for my home directory. Only print the total.")
출력
du -sh ~
마무리
이 기사에서는 txtai를 사용하여 지식을 LLM에 증류하는 것이 얼마나 간단한지 보여주었습니다. 항상 거대한 모델이 필요하지는 않습니다—작은 LLM을 약간의 시간 동안 파인튜닝하는 것이 충분히 가치 있는 노력일 수 있습니다.