[LoRA Tuning] LLaMA Factory

이유진·2025년 4월 25일

LLaMA Factory란?

data 디렉토리

  • 사용자는 이 data 디렉토리 안에 있는 다양한 데이터 중에서 파인튜닝할 것을 json 형식으로 입력해야 한다. 이 json 형식의 데이터는 template과 결합되어 LLM 학습에 사용된다.
  • ✨사용되는 json 데이터 형식은 instruction, input, output 키가 반드시! 포함되어야 한다.
    • instruction : 유저가 챗봇에게 질문할 내용
    • input: instruction과 비슷하게 챗봇에게 전달할 내용. 보통은 아무것도 없이 비워놓는다
    • output: 챗봇이 답변하는 내용

dataset_info.json

  • 새로운 데이터 파일 json을 만들었다면 이 데이터에 대한 정보도 추가해줘야 한다. data 디렉토리 내에 dataset_info.json에 추가된 새로운 json 파일에 대한 정보를 적어주면 됨.

template

  • LLM의 챗봇 템플릿을 지정하는 파이썬 파일.
  • 위치: src/llamafactory/data/template.py
  • 템플릿은 각 LLM이 훈련될 때 맞춰져 있기 때문에 특정 LLM을 학습시키기 위해서는 각 모델에 맞는 템플릿을 사용!

default 템플릿

_register_template(
    name="default",
    format_user=StringFormatter(slots=["Human: {{content}}\nAssistant: "]),
    format_system=StringFormatter(slots=["{{content}}\n"]),
    format_separator=EmptyFormatter(slots=["\n"]),
)

LLaMA-3 템플릿 템플릿

_register_template(
    name="llama3",
    format_user=StringFormatter(
        slots=[
            (
                "<|start_header_id|>user<|end_header_id|>\n\n{{content}}<|eot_id|>"
                "<|start_header_id|>assistant<|end_header_id|>\n\n"
            )
        ]
    ),
    format_system=StringFormatter(
        slots=[{"bos_token"}, "<|start_header_id|>system<|end_header_id|>\n\n{{content}}<|eot_id|>"]
    ),
    format_observation=StringFormatter(
        slots=[
            (
                "<|start_header_id|>tool<|end_header_id|>\n\n{{content}}<|eot_id|>"
                "<|start_header_id|>assistant<|end_header_id|>\n\n"
            )
        ]
    ),
    default_system="You are a helpful assistant.",
    stop_words=["<|eot_id|>"],
    replace_eos=True,
)
  • 결과적으로 data 디렉토리에 있는 json형태의 LLaMA Factory 입력 데이터의 형식과, 모델의 ChatTemplate이 합쳐져 LLM에서 요구하는 형식의 질문을 하게 됩니다.
sample_data.json

{
    "instruction": "갈비찜이 뭐야??",
    "input": "",
    "output": "갈비찜은 한국 전통 음식으로, 부드럽게 익힌 갈비에 양념을 넣어 맛을 낸 요리입니다."
}
profile
하이

0개의 댓글