Views
No views yet
memory_state JSON을 생성합니다.사용자 입력 → [Memory State Generator] → Router → LLM/VLM| 베이스 모델 | Qwen/Qwen2.5-3B-Instruct |
| 파인튜닝 방식 | SFT + LoRA |
| 학습 데이터 | DialogSum + QMSum |
| 최대 시퀀스 길이 | 512 |
| LoRA rank | 16 |
| GPU | NVIDIA A100 40GB |
| Epoch | 3 |
| 최종 Validation Loss | 0.693 |
1{
2 "memory_state": {
3 "key_facts": ["사실1", "사실2"],
4 "unresolved_refs": ["불명확한 지시어나 대명사"],
5 "topic": "대화의 주제",
6 "turn_count": 5
7 },
8 "memory_summary": "지금까지의 대화를 한 문장으로 요약한 내용"
9}1from transformers import AutoTokenizer, AutoModelForCausalLM
2import torch
3import json
4
5model_id = "your-username/qwen2.5-3b-memory-summary-v1"
6
7tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
8model = AutoModelForCausalLM.from_pretrained(
9 model_id,
10 torch_dtype=torch.bfloat16,
11 device_map="auto",
12 trust_remote_code=True
13)
14
15SYSTEM_PROMPT = """You are a Memory State Generator in a multi-turn dialogue system.
16Given a conversation, extract and output a structured memory state as JSON.
17
18Output format (strictly follow this):
19{
20 "memory_state": {
21 "key_facts": ["fact1", "fact2"],
22 "unresolved_refs": ["any unclear references or pronouns"],
23 "topic": "main topic of the conversation",
24 "turn_count": <number of turns>
25 },
26 "memory_summary": "One concise sentence summarizing the conversation so far."
27}
28
29Output only valid JSON. No explanation, no markdown."""
30
31dialogue = """
32A: RAG 파이프라인 구현 완료했어요.
33B: 모델은 어떤 걸 쓰기로 했어요?
34A: Qwen2.5-3B-Instruct로 결정했어요. LoRA로 파인튜닝할 예정입니다.
35"""
36
37messages = [
38 {"role": "system", "content": SYSTEM_PROMPT},
39 {"role": "user", "content": f"Conversation:\n{dialogue}"}
40]
41
42input_text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
43inputs = tokenizer(input_text, return_tensors="pt").to(model.device)
44
45with torch.no_grad():
46 outputs = model.generate(
47 **inputs,
48 max_new_tokens=512,
49 temperature=0.1,
50 do_sample=True,
51 pad_token_id=tokenizer.eos_token_id
52 )
53
54response = tokenizer.decode(outputs[0][inputs["input_ids"].shape[1]:], skip_special_tokens=True)
55parsed = json.loads(response)
56print(json.dumps(parsed, indent=2, ensure_ascii=False))memory_state JSON 형식으로 변환하여 SFT 학습에 사용했습니다.1LoraConfig(
2 r=16,
3 lora_alpha=32,
4 lora_dropout=0.05,
5 target_modules=["q_proj", "k_proj", "v_proj", "o_proj",
6 "gate_proj", "up_proj", "down_proj"],
7)
8
9SFTConfig(
10 num_train_epochs=3,
11 per_device_train_batch_size=1,
12 gradient_accumulation_steps=16,
13 learning_rate=2e-4,
14 lr_scheduler_type="cosine",
15 max_seq_length=512,
16 bf16=True,
17)| Step | Training Loss | Validation Loss |
|---|---|---|
| 100 | 14.578 | 0.896 |
| 500 | 12.919 | 0.804 |
| 1000 | 11.361 | 0.734 |
| 1500 | 10.437 | 0.694 |
| 2000 | 9.783 | 0.694 |
| 2400 | 9.635 | 0.693 |
turn_count 추출이 부정확할 수 있습니다key_facts가 구체적인 사실 추출보다 추상적인 요약에 가깝게 나오는 경우가 있습니다 — synthetic 데이터 추가 학습으로 개선 예정입니다