๐ญ Seoul Culture Event Recommendation RAG System (LoRA Adapter)
๐ Model Overview
์ด ๋ชจ๋ธ์ ์์ธ์ ๋ฌธํ ํ์ฌ ์ ๋ณด๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ์ฌ์ฉ์์๊ฒ ๋ง์ถคํ ๋ต๋ณ์ ์ ๊ณตํ๋ RAG(Retrieval-Augmented Generation) ์์คํ ์ ์ํด ๋ฏธ์ธ์กฐ์ (Fine-tuning)๋ Llama-3 ๊ธฐ๋ฐ์ LoRA Adapter์ ๋๋ค.
์ฌ์ฉ์์ ์ง๋ฌธ์ ๋ํด ๋จ์ํ LLM์ ๋ด๋ถ ์ง์์ผ๋ก ๋ต๋ณํ๋ ๊ฒ์ด ์๋๋ผ, ์ธ๋ถ ๋ฐ์ดํฐ๋ฒ ์ด์ค(culture.csv)์์ ์ค์๊ฐ์ผ๋ก ๊ฒ์๋(Retrieved) ์ ํํ ํ์ฌ ์ ๋ณด๋ฅผ ๋ฐํ์ผ๋ก ๋ต๋ณ์ ์์ฑํฉ๋๋ค.
- Base Model:
beomi/Llama-3-Open-Ko-8B - Fine-tuning Method: QLoRA (4-bit quantization + LoRA)
- Target Task: ์์ธ ๋ฌธํ ํ์ฌ ์ถ์ฒ ๋ฐ ์ ๋ณด ์๋ด (Instruction Following)
- Dataset Source: ์์ธ์ ๋ฌธํ ํ์ฌ ์ ๋ณด (4,275๊ฐ ๋ฐ์ดํฐ)
๐ ๏ธ How to Use
์ด ๋ชจ๋ธ์ LoRA Adapter์ด๋ฏ๋ก, ๋ฐ๋์ ๋ฒ ์ด์ค ๋ชจ๋ธ๊ณผ ํจ๊ป ๋ก๋ํด์ผ ํฉ๋๋ค.
1. Install Dependencies
pip install torch transformers peft bitsandbytes pandas accelerate
2. Run Inference Code
import torch from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig from peft import PeftModel1. Base Model Load (4-bit Quantization for efficiency)
base_model_id = "beomi/Llama-3-Open-Ko-8B" bnb_config = BitsAndBytesConfig( load_in_4bit=True, bnb_4bit_quant_type="nf4", bnb_4bit_compute_dtype=torch.float16 )
base_model = AutoModelForCausalLM.from_pretrained( base_model_id, quantization_config=bnb_config, device_map="auto" ) tokenizer = AutoTokenizer.from_pretrained(base_model_id)
2. Load LoRA Adapter
โ ๏ธ [YOUR_HUGGINGFACE_ID/YOUR_MODEL_NAME] ๋ถ๋ถ์ ๋ณธ์ธ ๋ชจ๋ธ ID๋ก ์์ ํ์ธ์!
adapter_model_id = "YOUR_HUGGINGFACE_ID/YOUR_MODEL_NAME" model = PeftModel.from_pretrained(base_model, adapter_model_id)
3. Define Inference Function
def generate_response(context, persona, query): input_text = f"Context: {context} Persona: [{persona}] ์ง๋ฌธ: [{query}]" prompt = f"""### Instruction: {persona} ํค์ผ๋ก ๋ต๋ณํด ์ฃผ์ธ์.
Input:
{input_text}
Output:
""" inputs = tokenizer(prompt, return_tensors="pt").to(model.device) with torch.no_grad(): outputs = model.generate( **inputs, max_new_tokens=300, temperature=0.7, top_p=0.9, eos_token_id=tokenizer.eos_token_id ) return tokenizer.decode(outputs, skip_special_tokens=True).split("### Output:")[-1].strip()
4. Test
context_data = "[์์ธ ํธ๋ ๋ฉ์ด๋ ํ์ด 2025, ์ฌ์ ์๋งค: 8,000์, ์์ธ ์ผ์ฑ๋ ์ฝ์์ค 1์ธต Bํ]" print(generate_response(context_data, "์น์ ํ ๋ฌธํ ๊ฐ์ด๋", "๊ฐ๋จ๊ตฌ์์ ์ด๋ฆฌ๋ ํธ๋๋ฉ์ด๋ ํ์ฌ ์๋ ค์ค."))
๐ Training Details
Dataset
- Source: ์์ธ์ด๋ฆฐ๋ฐ์ดํฐ๊ด์ฅ (๋ฌธํ ํ์ฌ ์ ๋ณด)
- Size: 4,275 rows (Original CSV), Augmented for training
- Format:
Instruction(ํ๋ฅด์๋ ์ง์),Input(Context + Question),Output(Answer) - Preprocessing: UTF-8 / CP949 ์ธ์ฝ๋ฉ ์๋ ์ฒ๋ฆฌ, ๊ฒฐ์ธก์น ์ ๊ฑฐ, 512 ํ ํฐ ๊ธธ์ด ์ ํ
Hyperparameters
| Parameter | Value | Reason |
|---|---|---|
| LoRA Rank (r) | 32 |
์ด๊ธฐ Rank 8/16 ์คํ ๊ฒฐ๊ณผ, Rank 32์์ Loss 1.51๋ก ๊ฐ์ฅ ์ฐ์ํ ์ฑ๋ฅ ๊ธฐ๋ก |
| LoRA Alpha | 32 |
Scaling factor |
| Target Modules | q_proj, k_proj, v_proj, o_proj, gate_proj, up_proj, down_proj |
๋ชจ๋ Linear Layer ํ์ต์ผ๋ก ์ถ๋ก ๋ฅ๋ ฅ ๊ทน๋ํ |
| Batch Size | 1 |
Colab T4 GPU ๋ฉ๋ชจ๋ฆฌ ์ ์ฝ ๊ทน๋ณต |
| Gradient Accumulation | 8 |
์ค์ Batch Size 8 ํจ๊ณผ (์์ ์ ์๋ ด) |
| Learning Rate | 2e-4 |
- |
| Epochs | 3 |
- |
| Optimizer | paged_adamw_8bit |
๋ฉ๋ชจ๋ฆฌ ํจ์จ ์ต์ ํ |
Performance Metrics
- Validation Loss:
1.5152(Rank 32 ๊ธฐ์ค)- Rank 8 Loss: 2.1565
- Rank 16 Loss: 1.7950
- Rank 32 Loss: 1.5152 (Best)
- ROUGE-L Score:
0.83(๋์ ๋ต๋ณ ์ ํ๋ ๋ฐ ์ผ๊ด์ฑ ํ์ธ) - Temperature Analysis: Temperature 0.5~0.7 ๊ตฌ๊ฐ์์ ROUGE ์ ์ ์ต์ ํ ํ์ธ (๋๋ฌด ๋ฎ์ผ๋ฉด ๋ฐ๋ณต, ๋๋ฌด ๋์ผ๋ฉด ํ๊ฐ ๋ฐ์)
| Rank | Validation Loss |
|---|---|
| 8 | 2.1565 |
| 16 | 1.7950 |
| 32 | 1.5152 |
โ ๏ธ Limitations & Future Work
- Hallucination (ํ๊ฐ ํ์):
- ๋ชจ๋ธ์ด ํ์ตํ์ง ์์ ์ธ๋ถ ์ ๋ณด(Unknown Data)์ ๋ํด ์ฝ 50%์ ํ๊ฐ ๋ฐ์๋ฅ ์ ๋ณด์์ต๋๋ค.
- RAG ์์คํ ์์ ๊ฒ์๋์ง ์์ ์ ๋ณด์ ๋ํด "์ ์ ์์"์ด๋ผ๊ณ ๋ต๋ณํ๋๋ก ์ถ๊ฐ ํ์ต(Negative Sampling)์ด ํ์ํฉ๋๋ค.
- Hardware Constraints:
- Colab T4 (free tier) ํ๊ฒฝ์ ์ ์ฝ์ผ๋ก ์ธํด Batch Size๋ฅผ 1๋ก ์ค์ ํ์ต๋๋ค. ๋ ํฐ VRAM ํ๊ฒฝ์์ Full Fine-tuning ์ ์ฑ๋ฅ ํฅ์์ด ๊ธฐ๋๋ฉ๋๋ค.
- Scope:
- ํ์ฌ ์์ธ์ ๋ฐ์ดํฐ์ ํนํ๋์ด ์์ด, ํ ์ง์ญ ํ์ฌ๋ ์ผ๋ฐ ์์ ์ง๋ฌธ์๋ ๋ต๋ณ ํ์ง์ด ๋ฎ์ ์ ์์ต๋๋ค.
๐ Citation & License
- License: Llama 3 Community License
- Base Model: beomi/Llama-3-Open-Ko-8B
Model tree for hushpond/llama-3-seoul-culture-lora-rag
Base model
beomi/Llama-3-Open-Ko-8B