Spaces:
Sleeping
Sleeping
Auto commit at 22-2025-08 4:00:04
Browse files
README.md
CHANGED
|
@@ -10,6 +10,9 @@ app_file: app.py
|
|
| 10 |
pinned: false
|
| 11 |
---
|
| 12 |
|
|
|
|
|
|
|
|
|
|
| 13 |
# 250821
|
| 14 |
- polyplot 5.8b ์๋ต ์๋ ๊ฐ์ , ๋ชจ๋ธ๋ณ tokenizer config settings json ๋ณ์ ๋ช
์์ ์ผ๋ก ๊ธฐ์
|
| 15 |
|
|
|
|
| 10 |
pinned: false
|
| 11 |
---
|
| 12 |
|
| 13 |
+
# 250822
|
| 14 |
+
- polyglot 1.3b lora ํ๋ผ๋ฉํฐ ์กฐ์ , ์๋ต ํ์ง ํฅ์
|
| 15 |
+
|
| 16 |
# 250821
|
| 17 |
- polyplot 5.8b ์๋ต ์๋ ๊ฐ์ , ๋ชจ๋ธ๋ณ tokenizer config settings json ๋ณ์ ๋ช
์์ ์ผ๋ก ๊ธฐ์
|
| 18 |
|
lily_llm_api/app_v2_250822_0312.py
DELETED
|
The diff for this file is too large to render.
See raw diff
|
|
|
lily_llm_api/models/polyglot_ko_1_3b_chat_250822_0312.py
DELETED
|
@@ -1,280 +0,0 @@
|
|
| 1 |
-
#!/usr/bin/env python3
|
| 2 |
-
"""
|
| 3 |
-
Polyglot-ko-1.3b-chat ๋ชจ๋ธ ํ๋กํ
|
| 4 |
-
heegyu/polyglot-ko-1.3b-chat ๋ชจ๋ธ์ฉ
|
| 5 |
-
"""
|
| 6 |
-
|
| 7 |
-
from typing import Dict, Any, Tuple
|
| 8 |
-
import torch
|
| 9 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 10 |
-
import logging
|
| 11 |
-
import os
|
| 12 |
-
from pathlib import Path
|
| 13 |
-
import re
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 17 |
-
|
| 18 |
-
logger = logging.getLogger(__name__)
|
| 19 |
-
|
| 20 |
-
class PolyglotKo13bChatProfile:
|
| 21 |
-
"""Polyglot-ko-1.3b-chat ๋ชจ๋ธ ํ๋กํ"""
|
| 22 |
-
|
| 23 |
-
def __init__(self):
|
| 24 |
-
self.model_name = "heegyu/polyglot-ko-1.3b-chat"
|
| 25 |
-
self.local_path = "./lily_llm_core/models/polyglot_ko_1_3b_chat"
|
| 26 |
-
self.display_name = "Polyglot-ko-1.3b-chat"
|
| 27 |
-
self.description = "ํ๊ตญ์ด ์ฑํ
์ ์ฉ ๊ฒฝ๋ ๋ชจ๋ธ (1.3B)"
|
| 28 |
-
self.language = "ko"
|
| 29 |
-
self.model_size = "1.3B"
|
| 30 |
-
|
| 31 |
-
def load_model(self) -> Tuple[AutoModelForCausalLM, AutoTokenizer]:
|
| 32 |
-
"""๋ชจ๋ธ ๋ก๋ (ํ ํฌ๋์ด์ ์ค์ ์์ )"""
|
| 33 |
-
logger.info(f"๐ฅ {self.display_name} ๋ชจ๋ธ ๋ก๋ ์ค...")
|
| 34 |
-
try:
|
| 35 |
-
use_local = Path(self.local_path).exists() and any(Path(self.local_path).iterdir())
|
| 36 |
-
model_path = self.local_path if use_local else self.model_name
|
| 37 |
-
logger.info(f"๐ ๋ชจ๋ธ ๊ฒฝ๋ก: {model_path} (local={'yes' if use_local else 'no'})")
|
| 38 |
-
|
| 39 |
-
# ๊ฐ์ ๋ก Hugging Face์์ ๋ค์ด๋ก๋ (๋ก์ปฌ ๋ชจ๋ธ ๋ฌธ์ ํด๊ฒฐ)
|
| 40 |
-
# use_local = False
|
| 41 |
-
# model_path = self.model_name
|
| 42 |
-
# logger.info(f"๐ ๋ชจ๋ธ ๊ฒฝ๋ก: {model_path} (local=no - ๊ฐ์ HF ๋ค์ด๋ก๋)")
|
| 43 |
-
|
| 44 |
-
tokenizer = AutoTokenizer.from_pretrained(
|
| 45 |
-
model_path,
|
| 46 |
-
token=HF_TOKEN,
|
| 47 |
-
use_fast=True,
|
| 48 |
-
trust_remote_code=True,
|
| 49 |
-
local_files_only=use_local,
|
| 50 |
-
)
|
| 51 |
-
|
| 52 |
-
# ํ ํฌ๋์ด์ ์ค์ ์์ - EOS ํ ํฐ ๋ฌธ์ ํด๊ฒฐ
|
| 53 |
-
if tokenizer.eos_token is None:
|
| 54 |
-
logger.warning("โ ๏ธ EOS ํ ํฐ์ด ์์ต๋๋ค. ๋ชจ๋ธ ๊ณต์ ๋ฌธ์์ ๋ฐ๋ผ <|endoftext|> ์ค์ ")
|
| 55 |
-
tokenizer.eos_token = "<|endoftext|>"
|
| 56 |
-
|
| 57 |
-
if tokenizer.pad_token is None:
|
| 58 |
-
logger.warning("โ ๏ธ PAD ํ ํฐ์ด ์์ต๋๋ค. EOS ํ ํฐ์ผ๋ก ์ค์ ")
|
| 59 |
-
tokenizer.pad_token = tokenizer.eos_token
|
| 60 |
-
|
| 61 |
-
# ํน์ ํ ํฐ ํ์ธ
|
| 62 |
-
logger.info(f"๐ ํ ํฌ๋์ด์ ์ค์ :")
|
| 63 |
-
logger.info(f" - EOS ํ ํฐ: {tokenizer.eos_token} (ID: {tokenizer.eos_token_id})")
|
| 64 |
-
logger.info(f" - PAD ํ ํฐ: {tokenizer.pad_token} (ID: {tokenizer.pad_token_id})")
|
| 65 |
-
# logger.info(f" - BOS ํ ํฐ: {tokenizer.bos_token} (ID: {tokenizer.bos_token_id})")
|
| 66 |
-
|
| 67 |
-
# CPU์์๋ float32๊ฐ ๋ ์์ ์ , CUDA์์๋ float16 ์ฌ์ฉ
|
| 68 |
-
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
| 69 |
-
selected_dtype = torch.float16 if device == 'cuda' else torch.float32
|
| 70 |
-
|
| 71 |
-
model = AutoModelForCausalLM.from_pretrained(
|
| 72 |
-
model_path,
|
| 73 |
-
token=HF_TOKEN,
|
| 74 |
-
trust_remote_code=True,
|
| 75 |
-
torch_dtype=selected_dtype,
|
| 76 |
-
local_files_only=use_local,
|
| 77 |
-
).to(device)
|
| 78 |
-
|
| 79 |
-
logger.info(f"โ
{self.display_name} ๋ชจ๋ธ ๋ก๋ ์ฑ๊ณต! (device={device}, dtype={selected_dtype})")
|
| 80 |
-
return model, tokenizer
|
| 81 |
-
except Exception as e:
|
| 82 |
-
logger.error(f"โ {self.display_name} ๋ชจ๋ธ ๋ก๋ ์คํจ: {e}")
|
| 83 |
-
raise
|
| 84 |
-
|
| 85 |
-
def format_prompt(self, user_input: str, context: str = None) -> str:
|
| 86 |
-
"""ํ๋กฌํํธ ํฌ๋งทํ
- ์์คํ
ํ๋กฌํํธ ๋จ์ํ"""
|
| 87 |
-
|
| 88 |
-
# ๊ธฐ๋ณธ ์์คํ
ํ๋กฌํํธ (๋จ์ํ)
|
| 89 |
-
system_prompt = """๋น์ ์ ์น์ ํ๊ณ ๋์์ด ๋๋ AI ์ฑ๋ด์
๋๋ค. ์ฌ์ฉ์์ ์ง๋ฌธ์ ์ ํํ๊ณ ์ ์ฉํ ๋ต๋ณ์ ์ ๊ณตํ์ธ์."""
|
| 90 |
-
|
| 91 |
-
# ์์คํ
ํ๋กฌํํธ๋ฅผ ํญ์ ๋จผ์ ํฌํจ
|
| 92 |
-
if context:
|
| 93 |
-
# ์ปจํ
์คํธ๊ฐ ์์ ๋
|
| 94 |
-
if user_input in context:
|
| 95 |
-
# ์ค๋ณต ๋ฐฉ์ง: ์ปจํ
์คํธ๋ง ์ฌ์ฉ
|
| 96 |
-
prompt = f"""{system_prompt}
|
| 97 |
-
|
| 98 |
-
{context}
|
| 99 |
-
|
| 100 |
-
### ์ฑ๋ด:"""
|
| 101 |
-
else:
|
| 102 |
-
# ์๋ก์ด ์ฌ์ฉ์ ์
๋ ฅ ์ถ๊ฐ
|
| 103 |
-
prompt = f"""{system_prompt}
|
| 104 |
-
|
| 105 |
-
{context}
|
| 106 |
-
|
| 107 |
-
### ์ฌ์ฉ์:
|
| 108 |
-
{user_input}
|
| 109 |
-
|
| 110 |
-
### ์ฑ๋ด:"""
|
| 111 |
-
else:
|
| 112 |
-
# ์ปจํ
์คํธ๊ฐ ์์ด๋ ์์คํ
ํ๋กฌํํธ๋ ํฌํจ
|
| 113 |
-
prompt = f"""{system_prompt}
|
| 114 |
-
|
| 115 |
-
### ์ฌ์ฉ์:
|
| 116 |
-
{user_input}
|
| 117 |
-
|
| 118 |
-
### ์ฑ๋ด:"""
|
| 119 |
-
return prompt
|
| 120 |
-
|
| 121 |
-
def extract_response(self, full_text: str, formatted_prompt: str = None) -> str:
|
| 122 |
-
"""์๋ต ์ถ์ถ - ํ์ง ๊ฒ์ฆ ๋ฐ ๊ฐ์ """
|
| 123 |
-
logger.info(f"--- Polyglot ์๋ต ์ถ์ถ ์์ ---")
|
| 124 |
-
logger.info(f"์ ์ฒด ์์ฑ ํ
์คํธ (Raw): \n---\n{full_text}\n---")
|
| 125 |
-
logger.info(f"์ฌ์ฉ๋ ํ๋กฌํํธ: {formatted_prompt}")
|
| 126 |
-
|
| 127 |
-
# 1์์: "### ์ฑ๋ด:" ํ๊ทธ๋ก ๏ฟฝ๏ฟฝ๏ฟฝ์ถ ์๋
|
| 128 |
-
if "### ์ฑ๋ด:" in full_text:
|
| 129 |
-
response = full_text.split("### ์ฑ๋ด:")[-1].strip()
|
| 130 |
-
logger.info(f"โ
์ฑ๊ณต: '### ์ฑ๋ด:' ํ๊ทธ๋ก ์๋ต ์ถ์ถ")
|
| 131 |
-
logger.info(f"์ถ์ถ๋ ์๋ต: {response}")
|
| 132 |
-
|
| 133 |
-
# ์๋ต ํ์ง ๊ฒ์ฆ
|
| 134 |
-
if self._validate_response_quality(response):
|
| 135 |
-
return response
|
| 136 |
-
else:
|
| 137 |
-
logger.warning("โ ๏ธ ์๋ต ํ์ง์ด ๋ฎ์ต๋๋ค. ํ์ง ๊ฐ์ ์ ์์ ์ถ๊ฐํฉ๋๋ค.")
|
| 138 |
-
return self._improve_response_quality(response)
|
| 139 |
-
|
| 140 |
-
# 2์์: ํ๋กฌํํธ ์ ๊ฑฐ๋ก ์ถ์ถ ์๋
|
| 141 |
-
if formatted_prompt and formatted_prompt in full_text:
|
| 142 |
-
response = full_text.replace(formatted_prompt, "").strip()
|
| 143 |
-
logger.info(f"โ
์ฑ๊ณต: ํ๋กฌํํธ ์ ๊ฑฐ๋ก ์๋ต ์ถ์ถ")
|
| 144 |
-
logger.info(f"์ถ์ถ๋ ์๋ต: {response}")
|
| 145 |
-
|
| 146 |
-
if self._validate_response_quality(response):
|
| 147 |
-
return response
|
| 148 |
-
else:
|
| 149 |
-
return self._improve_response_quality(response)
|
| 150 |
-
|
| 151 |
-
# 3์์: <|im_start|>assistant ํ๊ทธ ์ดํ ๋ด์ฉ ์ถ์ถ
|
| 152 |
-
if "<|im_start|>assistant" in full_text:
|
| 153 |
-
parts = full_text.split("<|im_start|>assistant")
|
| 154 |
-
if len(parts) > 1:
|
| 155 |
-
# ๋ง์ง๋ง assistant ํ๊ทธ ์ดํ ๋ด์ฉ
|
| 156 |
-
last_assistant_part = parts[-1]
|
| 157 |
-
# <|im_end|> ํ๊ทธ ์ ๊ฑฐ
|
| 158 |
-
if "<|im_end|>" in last_assistant_part:
|
| 159 |
-
response = last_assistant_part.split("<|im_end|>")[0].strip()
|
| 160 |
-
else:
|
| 161 |
-
response = last_assistant_part.strip()
|
| 162 |
-
|
| 163 |
-
logger.info(f"โ
์ฑ๊ณต: '<|im_start|>assistant' ํ๊ทธ๋ก ์๋ต ์ถ์ถ")
|
| 164 |
-
logger.info(f"์ถ์ถ๋ ์๋ต: {response}")
|
| 165 |
-
|
| 166 |
-
if self._validate_response_quality(response):
|
| 167 |
-
return response
|
| 168 |
-
else:
|
| 169 |
-
return self._improve_response_quality(response)
|
| 170 |
-
|
| 171 |
-
# 4์์: ์ผ๋ฐ์ ์ธ ํ๋กฌํํธ ํจํด ์ ๊ฑฐ ์๋
|
| 172 |
-
clean_text = full_text.strip()
|
| 173 |
-
patterns_to_remove = [
|
| 174 |
-
"(์๋ต์ด ๋๋ฌด ์งง์ต๋๋ค. ๋ ์์ธํ ๋ต๋ณ์ ์ํ์๋ฉด ๋ค์ ์ง๋ฌธํด์ฃผ์ธ์.)",
|
| 175 |
-
"### ์ฌ์ฉ์:",
|
| 176 |
-
"### ์ฑ๋ด:",
|
| 177 |
-
"์ฌ์ฉ์:",
|
| 178 |
-
"์ฑ๋ด:",
|
| 179 |
-
"assistant:",
|
| 180 |
-
"user:",
|
| 181 |
-
"<|im_start|>user",
|
| 182 |
-
"<|im_end|>",
|
| 183 |
-
"<|im_start|>assistant",
|
| 184 |
-
"<|im_start|>system"
|
| 185 |
-
]
|
| 186 |
-
|
| 187 |
-
for pattern in patterns_to_remove:
|
| 188 |
-
clean_text = clean_text.replace(pattern, "")
|
| 189 |
-
|
| 190 |
-
clean_text = clean_text.strip()
|
| 191 |
-
|
| 192 |
-
if clean_text and clean_text != full_text:
|
| 193 |
-
logger.info("โ
์ฑ๊ณต: ํจํด ์ ๊ฑฐ๋ก ์๋ต ์ ๋ฆฌ")
|
| 194 |
-
logger.info(f"์ ๋ฆฌ๋ ์๋ต: {clean_text}")
|
| 195 |
-
|
| 196 |
-
if self._validate_response_quality(clean_text):
|
| 197 |
-
return clean_text
|
| 198 |
-
else:
|
| 199 |
-
return self._improve_response_quality(clean_text)
|
| 200 |
-
|
| 201 |
-
# 5์์: ์ ์ฒด ํ
์คํธ์์ ๋ถํ์ํ ๋ถ๋ถ๋ง ์ ๊ฑฐ
|
| 202 |
-
final_response = full_text.strip()
|
| 203 |
-
logger.warning("โ ๏ธ ๊ฒฝ๊ณ : ํน๋ณํ ์๋ต ์ถ์ถ ํจํด์ ์ฐพ์ง ๋ชปํ์ต๋๋ค. ์ ์ฒด ํ
์คํธ๋ฅผ ์ ๋ฆฌํ์ฌ ๋ฐํํฉ๋๋ค.")
|
| 204 |
-
logger.info(f"์ต์ข
๋ฐํ ํ
์คํธ: {final_response}")
|
| 205 |
-
|
| 206 |
-
if self._validate_response_quality(final_response):
|
| 207 |
-
return final_response
|
| 208 |
-
else:
|
| 209 |
-
return self._improve_response_quality(final_response)
|
| 210 |
-
|
| 211 |
-
def _validate_response_quality(self, response: str) -> bool:
|
| 212 |
-
"""์๋ต ํ์ง ๊ฒ์ฆ"""
|
| 213 |
-
if not response or len(response.strip()) < 5:
|
| 214 |
-
return False
|
| 215 |
-
|
| 216 |
-
# ์์ด๊ฐ ํฌํจ๋์ด ์์ผ๋ฉด ํ์ง ๋ฎ์
|
| 217 |
-
# if any(char.isascii() and char.isalpha() for char in response):
|
| 218 |
-
# return False
|
| 219 |
-
|
| 220 |
-
# ๋ฌธ์ฅ์ด ์ค๊ฐ์ ๋์ด์ง ๊ฒฝ์ฐ ํ์ง ๋ฎ์
|
| 221 |
-
# if response.endswith(('ํ', '๋', '์', '๋ฅผ', '์ด', '๊ฐ', '์', '์', '๋ก')):
|
| 222 |
-
# return False
|
| 223 |
-
|
| 224 |
-
# ์ค๋ณต๋ ๋จ์ด๊ฐ ๋ง์ผ๋ฉด ํ์ง ๋ฎ์
|
| 225 |
-
# words = response.split()
|
| 226 |
-
# if len(words) > 3 and len(set(words)) / len(words) < 0.7:
|
| 227 |
-
# return False
|
| 228 |
-
|
| 229 |
-
return True
|
| 230 |
-
|
| 231 |
-
def _improve_response_quality(self, response: str) -> str:
|
| 232 |
-
"""์๋ต ํ์ง ๊ฐ์ """
|
| 233 |
-
# ๊ธฐ๋ณธ ์ ๋ฆฌ
|
| 234 |
-
improved = response.strip()
|
| 235 |
-
|
| 236 |
-
# ์์ด ์ ๊ฑฐ
|
| 237 |
-
|
| 238 |
-
# improved = re.sub(r'[a-zA-Z]+', '', improved)
|
| 239 |
-
|
| 240 |
-
# ์ค๋ณต ๊ณต๋ฐฑ ์ ๊ฑฐ
|
| 241 |
-
improved = re.sub(r'\s+', ' ', improved)
|
| 242 |
-
|
| 243 |
-
# ๋ฌธ์ฅ์ด ์ค๊ฐ์ ๋์ด์ง ๊ฒฝ์ฐ ์ฒ๋ฆฌ
|
| 244 |
-
# if improved.endswith(('ํ', '๋', '์', '๋ฅผ', '์ด', '๊ฐ', '์', '์', '๋ก')):
|
| 245 |
-
# improved += '๋๋ค.'
|
| 246 |
-
|
| 247 |
-
# ๋๋ฌด ์งง์ ๊ฒฝ์ฐ ๊ธฐ๋ณธ ์๋ต ์ถ๊ฐ
|
| 248 |
-
if len(improved) < 5:
|
| 249 |
-
improved = f"{improved} (์๋ต์ด ๋๋ฌด ์งง์ต๋๋ค. ๋ ์์ธํ ๋ต๋ณ์ ์ํ์๋ฉด ๋ค์ ์ง๋ฌธํด์ฃผ์ธ์.)"
|
| 250 |
-
|
| 251 |
-
logger.info(f"๐ง ์๋ต ํ์ง ๊ฐ์ ์๋ฃ: {improved}")
|
| 252 |
-
return improved
|
| 253 |
-
|
| 254 |
-
def get_generation_config(self) -> Dict[str, Any]:
|
| 255 |
-
"""์์ฑ ์ค์ - ๊ณต์ EOS ํ ํฐ ์ฌ์ฉ, ์์ฑ ํ๋ผ๋ฏธํฐ ์ต์ ํ"""
|
| 256 |
-
return {
|
| 257 |
-
"max_new_tokens": 128, # 256 โ 128๋ก ์ค์ (์ปจํ
์คํธ ๊ธธ์ด ๊ณ ๋ ค)
|
| 258 |
-
"temperature": 0.7, # 0.9 โ 0.7๋ก ์กฐ์ (์์ ์ฑ ํฅ์)
|
| 259 |
-
"do_sample": True, # ์ํ๋ง ํ์ฑํ
|
| 260 |
-
"top_k": 50, # 100 โ 50์ผ๋ก ์กฐ์ (ํ์ง๊ณผ ์์ ์ฑ ๊ท ํ)
|
| 261 |
-
"top_p": 0.9, # 0.95 โ 0.9๋ก ์กฐ์
|
| 262 |
-
"repetition_penalty": 1.1, # 1.05 โ 1.1๋ก ์กฐ์
|
| 263 |
-
"no_repeat_ngram_size": 3, # 2 โ 3์ผ๋ก ์กฐ์
|
| 264 |
-
"pad_token_id": 2, # ๊ณต์ ์ค์ ์ฌ์ฉ
|
| 265 |
-
"eos_token_id": 2, # ๊ณต์ ์ค์ ์ฌ์ฉ
|
| 266 |
-
"use_cache": True, # ์บ์ ํ์ฑํ (์๋ ํฅ์)
|
| 267 |
-
"early_stopping": False, # EOS ํ ํฐ๊น์ง ์์ฑํ๋๋ก ์ค์
|
| 268 |
-
}
|
| 269 |
-
|
| 270 |
-
def get_model_info(self) -> Dict[str, Any]:
|
| 271 |
-
"""๋ชจ๋ธ ์ ๋ณด"""
|
| 272 |
-
return {
|
| 273 |
-
"model_name": self.model_name,
|
| 274 |
-
"display_name": self.display_name,
|
| 275 |
-
"description": self.description,
|
| 276 |
-
"language": self.language,
|
| 277 |
-
"model_size": self.model_size,
|
| 278 |
-
"local_path": self.local_path,
|
| 279 |
-
"multimodal": False,
|
| 280 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
-
#!/usr/bin/env python3
|
| 2 |
-
"""
|
| 3 |
-
์ปจํ
์คํธ ๊ด๋ฆฌ์ (Context Manager)
|
| 4 |
-
๋ํ ํ์คํ ๋ฆฌ์ ๋จ๊ธฐ ๊ธฐ์ต์ ๊ด๋ฆฌํ๋ ์์คํ
|
| 5 |
-
"""
|
| 6 |
-
|
| 7 |
-
import logging
|
| 8 |
-
import time
|
| 9 |
-
from typing import List, Dict, Any, Optional, Tuple
|
| 10 |
-
from dataclasses import dataclass
|
| 11 |
-
from collections import deque
|
| 12 |
-
import json
|
| 13 |
-
|
| 14 |
-
logger = logging.getLogger(__name__)
|
| 15 |
-
|
| 16 |
-
@dataclass
|
| 17 |
-
class ConversationTurn:
|
| 18 |
-
"""๋ํ ํด์ ๋ํ๋ด๋ ๋ฐ์ดํฐ ํด๋์ค"""
|
| 19 |
-
role: str # 'user' ๋๋ 'assistant'
|
| 20 |
-
content: str
|
| 21 |
-
timestamp: float
|
| 22 |
-
message_id: str
|
| 23 |
-
metadata: Optional[Dict[str, Any]] = None
|
| 24 |
-
|
| 25 |
-
class ContextManager:
|
| 26 |
-
"""๋ํ ์ปจํ
์คํธ๋ฅผ ๊ด๋ฆฌํ๋ ํด๋์ค"""
|
| 27 |
-
|
| 28 |
-
def __init__(self,
|
| 29 |
-
max_tokens: int = 2000, # 4000 โ 2000์ผ๋ก ์ค์
|
| 30 |
-
max_turns: int = 20, # 20 โ 10์ผ๋ก ์ค์
|
| 31 |
-
strategy: str = "sliding_window"):
|
| 32 |
-
"""
|
| 33 |
-
Args:
|
| 34 |
-
max_tokens: ์ต๋ ํ ํฐ ์
|
| 35 |
-
max_turns: ์ต๋ ๋ํ ํด ์
|
| 36 |
-
strategy: ์ปจํ
์คํธ ๊ด๋ฆฌ ์ ๋ต ('sliding_window', 'priority_keep', 'circular')
|
| 37 |
-
"""
|
| 38 |
-
self.max_tokens = max_tokens
|
| 39 |
-
self.max_turns = max_turns
|
| 40 |
-
self.strategy = strategy
|
| 41 |
-
|
| 42 |
-
# ์ธ์
๋ณ ๋ํ ํ์คํ ๋ฆฌ (์ธ์
ID๋ก ๋ถ๋ฆฌ)
|
| 43 |
-
self.session_conversations: Dict[str, deque] = {}
|
| 44 |
-
self.default_session = "default"
|
| 45 |
-
|
| 46 |
-
# ๊ธฐ๋ณธ ์ธ์
์ด๊ธฐํ
|
| 47 |
-
self.session_conversations[self.default_session] = deque(maxlen=max_turns * 2)
|
| 48 |
-
|
| 49 |
-
# ์์คํ
ํ๋กฌํํธ
|
| 50 |
-
self.system_prompt = ""
|
| 51 |
-
|
| 52 |
-
# ์ปจํ
์คํธ ํต๊ณ
|
| 53 |
-
self.total_tokens = 0
|
| 54 |
-
self.current_context_length = 0
|
| 55 |
-
|
| 56 |
-
# ๋ฉ๋ชจ๋ฆฌ ์ต์ ํ ์ค์
|
| 57 |
-
self.enable_memory_optimization = True
|
| 58 |
-
self.compression_threshold = 0.8 # 80% ๋๋ฌ ์ ์์ถ ์์
|
| 59 |
-
|
| 60 |
-
# ๐ ์๋ ์ ๋ฆฌ ์ฃผ๊ธฐ ์ค์
|
| 61 |
-
self.auto_cleanup_enabled = True
|
| 62 |
-
self.cleanup_interval_turns = 5 # 8 โ 5ํด๋ง๋ค ์ ๋ฆฌ
|
| 63 |
-
self.cleanup_interval_time = 180 # 5๋ถ โ 3๋ถ๋ง๋ค ์ ๋ฆฌ
|
| 64 |
-
self.cleanup_strategy = "aggressive" # smart โ aggressive๋ก ๋ณ๊ฒฝ
|
| 65 |
-
self.last_cleanup_time = {} # ์ธ์
๋ณ ๋ง์ง๋ง ์ ๋ฆฌ ์๊ฐ
|
| 66 |
-
self.turn_counters = {} # ์ธ์
๋ณ ํด ์นด์ดํฐ
|
| 67 |
-
|
| 68 |
-
logger.info(f"๐ง ์ปจํ
์คํธ ๊ด๋ฆฌ์ ์ด๊ธฐํ: max_tokens={max_tokens}, strategy={strategy}, auto_cleanup={self.auto_cleanup_enabled}")
|
| 69 |
-
|
| 70 |
-
def set_system_prompt(self, prompt: str):
|
| 71 |
-
"""์์คํ
ํ๋กฌํํธ ์ค์ """
|
| 72 |
-
self.system_prompt = prompt
|
| 73 |
-
logger.info(f"๐ ์์คํ
ํ๋กฌํํธ ์ค์ : {len(prompt)} ๋ฌธ์")
|
| 74 |
-
|
| 75 |
-
def set_auto_cleanup_config(self,
|
| 76 |
-
enabled: bool = True,
|
| 77 |
-
interval_turns: int = 8,
|
| 78 |
-
interval_time: int = 300,
|
| 79 |
-
strategy: str = "smart"):
|
| 80 |
-
"""์๋ ์ ๋ฆฌ ์ค์ ๊ตฌ์ฑ"""
|
| 81 |
-
self.auto_cleanup_enabled = enabled
|
| 82 |
-
self.cleanup_interval_turns = max(1, interval_turns)
|
| 83 |
-
self.cleanup_interval_time = max(60, interval_time)
|
| 84 |
-
self.cleanup_strategy = strategy
|
| 85 |
-
|
| 86 |
-
logger.info(f"๐ ์๋ ์ ๋ฆฌ ์ค์ : enabled={enabled}, turns={interval_turns}, time={interval_time}s, strategy={strategy}")
|
| 87 |
-
|
| 88 |
-
def get_auto_cleanup_config(self) -> Dict[str, Any]:
|
| 89 |
-
"""์๋ ์ ๋ฆฌ ์ค์ ๋ฐํ"""
|
| 90 |
-
return {
|
| 91 |
-
"enabled": self.auto_cleanup_enabled,
|
| 92 |
-
"interval_turns": self.cleanup_interval_turns,
|
| 93 |
-
"interval_time": self.cleanup_interval_time,
|
| 94 |
-
"strategy": self.cleanup_strategy
|
| 95 |
-
}
|
| 96 |
-
|
| 97 |
-
def add_user_message(self, content: str, message_id: str = None, metadata: Dict[str, Any] = None) -> str:
|
| 98 |
-
"""์ฌ์ฉ์ ๋ฉ์์ง ์ถ๊ฐ"""
|
| 99 |
-
if not message_id:
|
| 100 |
-
message_id = f"user_{int(time.time() * 1000)}"
|
| 101 |
-
|
| 102 |
-
# ์ธ์
ID ์ถ์ถ (metadata์์)
|
| 103 |
-
session_id = "default"
|
| 104 |
-
if metadata and "session_id" in metadata:
|
| 105 |
-
session_id = metadata["session_id"]
|
| 106 |
-
|
| 107 |
-
# ์ธ์
์ด ์์ผ๋ฉด ์์ฑ
|
| 108 |
-
if session_id not in self.session_conversations:
|
| 109 |
-
self.session_conversations[session_id] = deque(maxlen=self.max_turns * 2)
|
| 110 |
-
|
| 111 |
-
turn = ConversationTurn(
|
| 112 |
-
role="user",
|
| 113 |
-
content=content,
|
| 114 |
-
timestamp=time.time(),
|
| 115 |
-
message_id=message_id,
|
| 116 |
-
metadata=metadata or {}
|
| 117 |
-
)
|
| 118 |
-
|
| 119 |
-
self.session_conversations[session_id].append(turn)
|
| 120 |
-
self._update_context_stats(session_id)
|
| 121 |
-
self._optimize_context(session_id)
|
| 122 |
-
|
| 123 |
-
# ๐ ์๋ ์ ๋ฆฌ ์ฒดํฌ
|
| 124 |
-
self._check_auto_cleanup(session_id)
|
| 125 |
-
|
| 126 |
-
logger.info(f"๐ค ์ฌ์ฉ์ ๋ฉ์์ง ์ถ๊ฐ: {len(content)} ๋ฌธ์ (์ธ์
: {session_id}, ์ด {len(self.session_conversations[session_id])} ํด)")
|
| 127 |
-
return message_id
|
| 128 |
-
|
| 129 |
-
def add_assistant_message(self, content: str, message_id: str = None, metadata: Dict[str, Any] = None) -> str:
|
| 130 |
-
"""์ด์์คํดํธ ๋ฉ์์ง ์ถ๊ฐ"""
|
| 131 |
-
if not message_id:
|
| 132 |
-
message_id = f"assistant_{int(time.time() * 1000)}"
|
| 133 |
-
|
| 134 |
-
# ์ธ์
ID ์ถ์ถ (metadata์์)
|
| 135 |
-
session_id = "default"
|
| 136 |
-
if metadata and "session_id" in metadata:
|
| 137 |
-
session_id = metadata["session_id"]
|
| 138 |
-
|
| 139 |
-
# ์ธ์
์ด ์์ผ๋ฉด ์์ฑ
|
| 140 |
-
if session_id not in self.session_conversations:
|
| 141 |
-
self.session_conversations[session_id] = deque(maxlen=self.max_turns * 2)
|
| 142 |
-
|
| 143 |
-
turn = ConversationTurn(
|
| 144 |
-
role="assistant",
|
| 145 |
-
content=content,
|
| 146 |
-
timestamp=time.time(),
|
| 147 |
-
message_id=message_id,
|
| 148 |
-
metadata=metadata or {}
|
| 149 |
-
)
|
| 150 |
-
|
| 151 |
-
self.session_conversations[session_id].append(turn)
|
| 152 |
-
self._update_context_stats(session_id)
|
| 153 |
-
self._optimize_context(session_id)
|
| 154 |
-
|
| 155 |
-
# ๐ ์๋ ์ ๋ฆฌ ์ฒดํฌ
|
| 156 |
-
self._check_auto_cleanup(session_id)
|
| 157 |
-
|
| 158 |
-
logger.info(f"๐ค ์ด์์คํดํธ ๋ฉ์์ง ์ถ๊ฐ: {len(content)} ๋ฌธ์ (์ธ์
: {session_id}, ์ด {len(self.session_conversations[session_id])} ํด)")
|
| 159 |
-
return message_id
|
| 160 |
-
|
| 161 |
-
def get_context(self, include_system: bool = True, max_length: Optional[int] = None, session_id: str = "default") -> str:
|
| 162 |
-
"""ํ์ฌ ์ปจํ
์คํธ๋ฅผ ๋ฌธ์์ด๋ก ๋ฐํ (์ธ์
๋ณ)"""
|
| 163 |
-
context_parts = []
|
| 164 |
-
|
| 165 |
-
# ์ธ์
์ด ์์ผ๋ฉด ๊ธฐ๋ณธ ์ธ์
์ฌ์ฉ
|
| 166 |
-
if session_id not in self.session_conversations:
|
| 167 |
-
session_id = "default"
|
| 168 |
-
|
| 169 |
-
conversation_history = self.session_conversations[session_id]
|
| 170 |
-
|
| 171 |
-
# ์์คํ
ํ๋กฌํํธ ํฌํจ
|
| 172 |
-
if include_system and self.system_prompt:
|
| 173 |
-
context_parts.append(f"<|im_start|>system\n{self.system_prompt}<|im_end|>")
|
| 174 |
-
|
| 175 |
-
# ๋ํ ํ์คํ ๋ฆฌ ํฌํจ
|
| 176 |
-
for turn in conversation_history:
|
| 177 |
-
if turn.role == "user":
|
| 178 |
-
context_parts.append(f"<|im_start|>user\n{turn.content}<|im_end|>")
|
| 179 |
-
elif turn.role == "assistant":
|
| 180 |
-
context_parts.append(f"<|im_start|>assistant\n{turn.content}<|im_end|>")
|
| 181 |
-
|
| 182 |
-
# ์ด์์คํดํธ ์๋ต ์์ ํ ํฐ ์ถ๊ฐ
|
| 183 |
-
context_parts.append("<|im_start|>assistant\n")
|
| 184 |
-
|
| 185 |
-
context = "\n".join(context_parts)
|
| 186 |
-
|
| 187 |
-
# ๊ธธ์ด ์ ํ ์ ์ฉ
|
| 188 |
-
if max_length and len(context) > max_length:
|
| 189 |
-
context = self._truncate_context(context, max_length)
|
| 190 |
-
|
| 191 |
-
return context
|
| 192 |
-
|
| 193 |
-
def get_context_for_model(self, model_name: str = "default", session_id: str = "default") -> str:
|
| 194 |
-
"""๋ชจ๋ธ๋ณ ์ต์ ํ๋ ์ปจํ
์คํธ ๋ฐํ (์ธ์
๋ณ)"""
|
| 195 |
-
# ๋ชจ๋ธ๋ณ ํน๋ณํ ์ฒ๋ฆฌ (ํ์์ ํ์ฅ)
|
| 196 |
-
if "kanana" in model_name.lower():
|
| 197 |
-
return self.get_context(include_system=True, session_id=session_id)
|
| 198 |
-
elif "llama" in model_name.lower():
|
| 199 |
-
# Llama ํ์
|
| 200 |
-
return self._format_for_llama(session_id)
|
| 201 |
-
elif "polyglot" in model_name.lower():
|
| 202 |
-
# Polyglot ํ์ - <|im_start|> ํ๊ทธ ์ฌ์ฉํ์ง ์์
|
| 203 |
-
return self._format_for_polyglot(session_id)
|
| 204 |
-
else:
|
| 205 |
-
return self.get_context(include_system=True, session_id=session_id)
|
| 206 |
-
|
| 207 |
-
def _format_for_llama(self, session_id: str = "default") -> str:
|
| 208 |
-
"""Llama ๋ชจ๋ธ์ฉ ํ์์ผ๋ก ๋ณํ (์ธ์
๋ณ)"""
|
| 209 |
-
context_parts = []
|
| 210 |
-
|
| 211 |
-
# ์ธ์
์ด ์์ผ๋ฉด ๊ธฐ๋ณธ ์ธ์
์ฌ์ฉ
|
| 212 |
-
if session_id not in self.session_conversations:
|
| 213 |
-
session_id = "default"
|
| 214 |
-
|
| 215 |
-
conversation_history = self.session_conversations[session_id]
|
| 216 |
-
|
| 217 |
-
if self.system_prompt:
|
| 218 |
-
context_parts.append(f"[INST] {self.system_prompt} [/INST]")
|
| 219 |
-
|
| 220 |
-
for turn in conversation_history:
|
| 221 |
-
if turn.role == "user":
|
| 222 |
-
context_parts.append(f"[INST] {turn.content} [/INST]")
|
| 223 |
-
elif turn.role == "assistant":
|
| 224 |
-
context_parts.append(turn.content)
|
| 225 |
-
|
| 226 |
-
return "\n".join(context_parts)
|
| 227 |
-
|
| 228 |
-
def _format_for_polyglot(self, session_id: str = "default") -> str:
|
| 229 |
-
"""Polyglot ๋ชจ๋ธ์ฉ ํ์์ผ๋ก ๋ณํ (์ธ์
๋ณ) - ๊ณต์ ํ์ ์ฌ์ฉ"""
|
| 230 |
-
context_parts = []
|
| 231 |
-
|
| 232 |
-
# ์ธ์
์ด ์์ผ๋ฉด ๊ธฐ๋ณธ ์ธ์
์ฌ์ฉ
|
| 233 |
-
if session_id not in self.session_conversations:
|
| 234 |
-
session_id = "default"
|
| 235 |
-
|
| 236 |
-
conversation_history = self.session_conversations[session_id]
|
| 237 |
-
|
| 238 |
-
# ๋ํ ํ์คํ ๋ฆฌ๋ง ํฌํจ (๊ณต์ ํ์ ์ฌ์ฉ)
|
| 239 |
-
for turn in conversation_history:
|
| 240 |
-
if turn.role == "user":
|
| 241 |
-
context_parts.append(f"### ์ฌ์ฉ์:\n{turn.content}")
|
| 242 |
-
elif turn.role == "assistant":
|
| 243 |
-
context_parts.append(f"### ์ฑ๋ด:\n{turn.content}")
|
| 244 |
-
|
| 245 |
-
if context_parts:
|
| 246 |
-
return "\n\n".join(context_parts)
|
| 247 |
-
else:
|
| 248 |
-
return ""
|
| 249 |
-
|
| 250 |
-
def get_recent_context(self, turns: int = 5, session_id: str = "default") -> str:
|
| 251 |
-
"""์ต๊ทผ N๊ฐ ํด์ ์ปจํ
์คํธ๋ง ๋ฐํ (์ธ์
๋ณ)"""
|
| 252 |
-
# ์ธ์
์ด ์์ผ๋ฉด ๊ธฐ๋ณธ ์ธ์
์ฌ์ฉ
|
| 253 |
-
if session_id not in self.session_conversations:
|
| 254 |
-
session_id = "default"
|
| 255 |
-
|
| 256 |
-
conversation_history = self.session_conversations[session_id]
|
| 257 |
-
recent_turns = list(conversation_history)[-turns:]
|
| 258 |
-
context_parts = []
|
| 259 |
-
|
| 260 |
-
for turn in recent_turns:
|
| 261 |
-
if turn.role == "user":
|
| 262 |
-
context_parts.append(f"<|im_start|>user\n{turn.content}<|im_end|>")
|
| 263 |
-
elif turn.role == "assistant":
|
| 264 |
-
context_parts.append(f"<|im_start|>assistant\n{turn.content}<|im_end|>")
|
| 265 |
-
|
| 266 |
-
context_parts.append("<|im_start|>assistant\n")
|
| 267 |
-
return "\n".join(context_parts)
|
| 268 |
-
|
| 269 |
-
def get_context_summary(self, session_id: str = "default") -> Dict[str, Any]:
|
| 270 |
-
"""์ปจํ
์คํธ ์์ฝ ์ ๋ณด ๋ฐํ (์ธ์
๋ณ)"""
|
| 271 |
-
# ์ธ์
์ด ์์ผ๋ฉด ๊ธฐ๋ณธ ์ธ์
์ฌ์ฉ
|
| 272 |
-
if session_id not in self.session_conversations:
|
| 273 |
-
session_id = "default"
|
| 274 |
-
|
| 275 |
-
conversation_history = self.session_conversations[session_id]
|
| 276 |
-
|
| 277 |
-
return {
|
| 278 |
-
"session_id": session_id,
|
| 279 |
-
"total_turns": len(conversation_history),
|
| 280 |
-
"user_messages": len([t for t in conversation_history if t.role == "user"]),
|
| 281 |
-
"assistant_messages": len([t for t in conversation_history if t.role == "assistant"]),
|
| 282 |
-
"estimated_tokens": self.total_tokens,
|
| 283 |
-
"context_length": self.current_context_length,
|
| 284 |
-
"memory_usage": len(conversation_history) / self.max_turns,
|
| 285 |
-
"oldest_message": conversation_history[0].timestamp if conversation_history else None,
|
| 286 |
-
"newest_message": conversation_history[-1].timestamp if conversation_history else None
|
| 287 |
-
}
|
| 288 |
-
|
| 289 |
-
def clear_context(self, session_id: str = "default"):
|
| 290 |
-
"""์ปจํ
์คํธ ์ด๊ธฐํ (์ธ์
๋ณ)"""
|
| 291 |
-
if session_id not in self.session_conversations:
|
| 292 |
-
logger.warning(f"โ ๏ธ ์ธ์
{session_id}๊ฐ ์กด์ฌํ์ง ์์ต๋๋ค.")
|
| 293 |
-
return
|
| 294 |
-
|
| 295 |
-
self.session_conversations[session_id].clear()
|
| 296 |
-
self.total_tokens = 0
|
| 297 |
-
self.current_context_length = 0
|
| 298 |
-
logger.info(f"๐๏ธ ์ธ์
{session_id} ์ปจํ
์คํธ ์ด๊ธฐํ ์๋ฃ")
|
| 299 |
-
|
| 300 |
-
def clear_all_sessions(self):
|
| 301 |
-
"""๋ชจ๋ ์ธ์
์ปจํ
์คํธ ์ด๊ธฐํ"""
|
| 302 |
-
for session_id in list(self.session_conversations.keys()):
|
| 303 |
-
self.session_conversations[session_id].clear()
|
| 304 |
-
self.total_tokens = 0
|
| 305 |
-
self.current_context_length = 0
|
| 306 |
-
logger.info("๐๏ธ ๋ชจ๋ ์ธ์
์ปจํ
์คํธ ์ด๊ธฐํ ์๋ฃ")
|
| 307 |
-
|
| 308 |
-
def remove_message(self, message_id: str, session_id: str = "default") -> bool:
|
| 309 |
-
"""ํน์ ๋ฉ์์ง ์ ๊ฑฐ (์ธ์
๋ณ)"""
|
| 310 |
-
if session_id not in self.session_conversations:
|
| 311 |
-
return False
|
| 312 |
-
|
| 313 |
-
conversation_history = self.session_conversations[session_id]
|
| 314 |
-
for i, turn in enumerate(conversation_history):
|
| 315 |
-
if turn.message_id == message_id:
|
| 316 |
-
removed_turn = conversation_history.pop(i)
|
| 317 |
-
self._update_context_stats(session_id)
|
| 318 |
-
logger.info(f"๐๏ธ ๋ฉ์์ง ์ ๊ฑฐ: {message_id} (์ธ์
: {session_id})")
|
| 319 |
-
return True
|
| 320 |
-
return False
|
| 321 |
-
|
| 322 |
-
def edit_message(self, message_id: str, new_content: str, session_id: str = "default") -> bool:
|
| 323 |
-
"""๋ฉ์์ง ๋ด์ฉ ์์ (์ธ์
๋ณ)"""
|
| 324 |
-
if session_id not in self.session_conversations:
|
| 325 |
-
return False
|
| 326 |
-
|
| 327 |
-
conversation_history = self.session_conversations[session_id]
|
| 328 |
-
for turn in conversation_history:
|
| 329 |
-
if turn.message_id == message_id:
|
| 330 |
-
turn.content = new_content
|
| 331 |
-
turn.timestamp = time.time()
|
| 332 |
-
self._update_context_stats(session_id)
|
| 333 |
-
logger.info(f"โ๏ธ ๋ฉ์์ง ์์ : {message_id} (์ธ์
: {session_id})")
|
| 334 |
-
return True
|
| 335 |
-
return False
|
| 336 |
-
|
| 337 |
-
def search_context(self, query: str, max_results: int = 5, session_id: str = "default") -> List[Dict[str, Any]]:
|
| 338 |
-
"""์ปจํ
์คํธ ๋ด์์ ๊ฒ์ (์ธ์
๋ณ)"""
|
| 339 |
-
if session_id not in self.session_conversations:
|
| 340 |
-
return []
|
| 341 |
-
|
| 342 |
-
conversation_history = self.session_conversations[session_id]
|
| 343 |
-
results = []
|
| 344 |
-
query_lower = query.lower()
|
| 345 |
-
|
| 346 |
-
for turn in conversation_history:
|
| 347 |
-
if query_lower in turn.content.lower():
|
| 348 |
-
results.append({
|
| 349 |
-
"message_id": turn.message_id,
|
| 350 |
-
"role": turn.role,
|
| 351 |
-
"content": turn.content,
|
| 352 |
-
"timestamp": turn.timestamp,
|
| 353 |
-
"relevance_score": self._calculate_relevance(query, turn.content)
|
| 354 |
-
})
|
| 355 |
-
|
| 356 |
-
# ๊ด๋ จ์ฑ ์ ์๋ก ์ ๋ ฌ
|
| 357 |
-
results.sort(key=lambda x: x["relevance_score"], reverse=True)
|
| 358 |
-
return results[:max_results]
|
| 359 |
-
|
| 360 |
-
def _calculate_relevance(self, query: str, content: str) -> float:
|
| 361 |
-
"""๊ฐ๋จํ ๊ด๋ จ์ฑ ์ ์ ๊ณ์ฐ"""
|
| 362 |
-
query_words = set(query.lower().split())
|
| 363 |
-
content_words = set(content.lower().split())
|
| 364 |
-
|
| 365 |
-
if not query_words:
|
| 366 |
-
return 0.0
|
| 367 |
-
|
| 368 |
-
intersection = query_words.intersection(content_words)
|
| 369 |
-
return len(intersection) / len(query_words)
|
| 370 |
-
|
| 371 |
-
def _update_context_stats(self, session_id: str = "default"):
|
| 372 |
-
"""์ปจํ
์คํธ ํต๊ณ ์
๋ฐ์ดํธ (์ธ์
๋ณ)"""
|
| 373 |
-
if session_id not in self.session_conversations:
|
| 374 |
-
return
|
| 375 |
-
|
| 376 |
-
self.current_context_length = len(self.get_context(session_id=session_id))
|
| 377 |
-
# ๊ฐ๋จํ ํ ํฐ ์ถ์ (์ค์ ํ ํฌ๋์ด์ ์ฌ์ฉ ๊ถ์ฅ)
|
| 378 |
-
self.total_tokens = self.current_context_length // 4
|
| 379 |
-
|
| 380 |
-
def _optimize_context(self, session_id: str = "default"):
|
| 381 |
-
"""์ปจํ
์คํธ ์ต์ ํ (์ธ์
๋ณ)"""
|
| 382 |
-
if not self.enable_memory_optimization:
|
| 383 |
-
return
|
| 384 |
-
|
| 385 |
-
if session_id not in self.session_conversations:
|
| 386 |
-
return
|
| 387 |
-
|
| 388 |
-
conversation_history = self.session_conversations[session_id]
|
| 389 |
-
|
| 390 |
-
# ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ๋์ด ์๊ณ๊ฐ์ ์ด๊ณผํ๋ฉด ์์ถ ์์
|
| 391 |
-
if len(conversation_history) / self.max_turns > self.compression_threshold:
|
| 392 |
-
self._compress_context(session_id)
|
| 393 |
-
|
| 394 |
-
def _compress_context(self, session_id: str = "default"):
|
| 395 |
-
"""์ปจํ
์คํธ ์์ถ (์ค์ํ ๋ฉ์์ง ์ ์ง, ์ธ์
๋ณ)"""
|
| 396 |
-
if session_id not in self.session_conversations:
|
| 397 |
-
return
|
| 398 |
-
|
| 399 |
-
conversation_history = self.session_conversations[session_id]
|
| 400 |
-
|
| 401 |
-
if len(conversation_history) <= self.max_turns:
|
| 402 |
-
return
|
| 403 |
-
|
| 404 |
-
logger.info(f"๐๏ธ ์ธ์
{session_id} ์ปจํ
์คํธ ์์ถ ์์: {len(conversation_history)} โ {self.max_turns}")
|
| 405 |
-
|
| 406 |
-
# ์ ๋ต์ ๋ฐ๋ฅธ ์์ถ
|
| 407 |
-
if self.strategy == "sliding_window":
|
| 408 |
-
# ์ฌ๋ผ์ด๋ฉ ์๋์ฐ: ์ต๊ทผ ๋ฉ์์ง ์ฐ์
|
| 409 |
-
while len(conversation_history) > self.max_turns:
|
| 410 |
-
conversation_history.popleft()
|
| 411 |
-
|
| 412 |
-
elif self.strategy == "priority_keep":
|
| 413 |
-
# ์ฐ์ ์์ ๊ธฐ๋ฐ: ์์คํ
ํ๋กฌํํธ์ ์ต๊ทผ ๋ฉ์์ง ์ฐ์
|
| 414 |
-
# ์ฒซ ๋ฒ์งธ์ ๋ง์ง๋ง ๋ฉ์์ง๋ ์ ์ง
|
| 415 |
-
if len(conversation_history) > self.max_turns:
|
| 416 |
-
# ์ค๊ฐ ๋ฉ์์ง๋ค ์ค ์ผ๋ถ ์ ๊ฑฐ
|
| 417 |
-
middle_start = self.max_turns // 2
|
| 418 |
-
middle_end = len(conversation_history) - self.max_turns // 2
|
| 419 |
-
|
| 420 |
-
# ์ค๊ฐ ๋ถ๋ถ์ ์์ฝ์ผ๋ก ๋์ฒด
|
| 421 |
-
removed_turns = list(conversation_history)[middle_start:middle_end]
|
| 422 |
-
summary_content = f"[์ด์ {len(removed_turns)}๊ฐ ๋ฉ์์ง ์์ฝ: {len(removed_turns)}๊ฐ ๋ํ ํด]"
|
| 423 |
-
|
| 424 |
-
# ์ค๊ฐ ๋ถ๋ถ ์ ๊ฑฐ
|
| 425 |
-
for _ in range(middle_end - middle_start):
|
| 426 |
-
conversation_history.pop(middle_start)
|
| 427 |
-
|
| 428 |
-
# ์์ฝ ๋ฉ์์ง ์ถ๊ฐ
|
| 429 |
-
summary_turn = ConversationTurn(
|
| 430 |
-
role="system",
|
| 431 |
-
content=summary_content,
|
| 432 |
-
timestamp=time.time(),
|
| 433 |
-
message_id=f"summary_{int(time.time() * 1000)}"
|
| 434 |
-
)
|
| 435 |
-
conversation_history.insert(middle_start, summary_turn)
|
| 436 |
-
|
| 437 |
-
elif self.strategy == "circular":
|
| 438 |
-
# ์ํ ๋ฒํผ: ๊ฐ์ฅ ์ค๋๋ ๋ฉ์์ง ์ ๊ฑฐ
|
| 439 |
-
while len(conversation_history) > self.max_turns:
|
| 440 |
-
conversation_history.popleft()
|
| 441 |
-
|
| 442 |
-
self._update_context_stats(session_id)
|
| 443 |
-
logger.info(f"โ
์ธ์
{session_id} ์ปจํ
์คํธ ์์ถ ์๋ฃ: {len(conversation_history)} ํด")
|
| 444 |
-
|
| 445 |
-
def _truncate_context(self, context: str, max_length: int) -> str:
|
| 446 |
-
"""์ปจํ
์คํธ ๊ธธ์ด ์ ํ"""
|
| 447 |
-
if len(context) <= max_length:
|
| 448 |
-
return context
|
| 449 |
-
|
| 450 |
-
# ๊ฐ์ฅ ์ต๊ทผ ๋ฉ์์ง๋ถํฐ ์ ์ง
|
| 451 |
-
truncated_context = context[-max_length:]
|
| 452 |
-
|
| 453 |
-
# ๋ฉ์์ง ๊ฒฝ๊ณ ํ์ธ
|
| 454 |
-
if not truncated_context.startswith("<|im_start|>"):
|
| 455 |
-
# ๋ฉ์์ง ๊ฒฝ๊ณ๋ฅผ ์ฐพ์์ ์๋ฅด๊ธฐ
|
| 456 |
-
start_idx = truncated_context.find("<|im_start|>")
|
| 457 |
-
if start_idx != -1:
|
| 458 |
-
truncated_context = truncated_context[start_idx:]
|
| 459 |
-
|
| 460 |
-
return truncated_context
|
| 461 |
-
|
| 462 |
-
def export_context(self, file_path: str = None, session_id: str = "default") -> str:
|
| 463 |
-
"""์ปจํ
์คํธ๋ฅผ ํ์ผ๋ก ๋ด๋ณด๋ด๊ธฐ (์ธ์
๋ณ)"""
|
| 464 |
-
if not file_path:
|
| 465 |
-
file_path = f"context_export_{session_id}_{int(time.time())}.json"
|
| 466 |
-
|
| 467 |
-
if session_id not in self.session_conversations:
|
| 468 |
-
logger.warning(f"โ ๏ธ ์ธ์
{session_id}๊ฐ ์กด์ฌํ์ง ์์ต๋๋ค.")
|
| 469 |
-
return None
|
| 470 |
-
|
| 471 |
-
conversation_history = self.session_conversations[session_id]
|
| 472 |
-
|
| 473 |
-
export_data = {
|
| 474 |
-
"export_timestamp": time.time(),
|
| 475 |
-
"session_id": session_id,
|
| 476 |
-
"system_prompt": self.system_prompt,
|
| 477 |
-
"conversation_history": [
|
| 478 |
-
{
|
| 479 |
-
"role": turn.role,
|
| 480 |
-
"content": turn.content,
|
| 481 |
-
"timestamp": turn.timestamp,
|
| 482 |
-
"message_id": turn.message_id,
|
| 483 |
-
"metadata": turn.metadata
|
| 484 |
-
}
|
| 485 |
-
for turn in conversation_history
|
| 486 |
-
],
|
| 487 |
-
"context_stats": self.get_context_summary(session_id)
|
| 488 |
-
}
|
| 489 |
-
|
| 490 |
-
with open(file_path, 'w', encoding='utf-8') as f:
|
| 491 |
-
json.dump(export_data, f, ensure_ascii=False, indent=2)
|
| 492 |
-
|
| 493 |
-
logger.info(f"๐พ ์ธ์
{session_id} ์ปจํ
์คํธ ๋ด๋ณด๋ด๊ธฐ ์๋ฃ: {file_path}")
|
| 494 |
-
return file_path
|
| 495 |
-
|
| 496 |
-
def import_context(self, file_path: str) -> bool:
|
| 497 |
-
"""ํ์ผ์์ ์ปจํ
์คํธ ๊ฐ์ ธ์ค๊ธฐ"""
|
| 498 |
-
try:
|
| 499 |
-
with open(file_path, 'r', encoding='utf-8') as f:
|
| 500 |
-
import_data = json.load(f)
|
| 501 |
-
|
| 502 |
-
# ๊ธฐ์กด ์ปจํ
์คํธ ์ด๊ธฐํ
|
| 503 |
-
self.clear_context()
|
| 504 |
-
|
| 505 |
-
# ์์คํ
ํ๋กฌํํธ ๋ณต์
|
| 506 |
-
if "system_prompt" in import_data:
|
| 507 |
-
self.system_prompt = import_data["system_prompt"]
|
| 508 |
-
|
| 509 |
-
# ๋ํ ํ์คํ ๋ฆฌ ๋ณต์
|
| 510 |
-
if "conversation_history" in import_data:
|
| 511 |
-
for turn_data in import_data["conversation_history"]:
|
| 512 |
-
turn = ConversationTurn(
|
| 513 |
-
role=turn_data["role"],
|
| 514 |
-
content=turn_data["content"],
|
| 515 |
-
timestamp=turn_data["timestamp"],
|
| 516 |
-
message_id=turn_data["message_id"],
|
| 517 |
-
metadata=turn_data.get("metadata", {})
|
| 518 |
-
)
|
| 519 |
-
self.conversation_history.append(turn)
|
| 520 |
-
|
| 521 |
-
self._update_context_stats()
|
| 522 |
-
logger.info(f"๐ฅ ์ปจํ
์คํธ ๊ฐ์ ธ์ค๊ธฐ ์๋ฃ: {file_path}")
|
| 523 |
-
return True
|
| 524 |
-
|
| 525 |
-
except Exception as e:
|
| 526 |
-
logger.error(f"โ ์ปจํ
์คํธ ๊ฐ์ ธ์ค๊ธฐ ์คํจ: {e}")
|
| 527 |
-
return False
|
| 528 |
-
|
| 529 |
-
def get_memory_efficiency(self, session_id: str = "default") -> Dict[str, float]:
|
| 530 |
-
"""๋ฉ๋ชจ๋ฆฌ ํจ์จ์ฑ ์งํ ๋ฐํ (์ธ์
๋ณ)"""
|
| 531 |
-
if session_id not in self.session_conversations:
|
| 532 |
-
return {}
|
| 533 |
-
|
| 534 |
-
conversation_history = self.session_conversations[session_id]
|
| 535 |
-
|
| 536 |
-
return {
|
| 537 |
-
"session_id": session_id,
|
| 538 |
-
"context_utilization": len(conversation_history) / self.max_turns,
|
| 539 |
-
"token_efficiency": self.total_tokens / self.max_tokens if self.max_tokens > 0 else 0,
|
| 540 |
-
"compression_ratio": 1.0 - (len(conversation_history) / (self.max_turns * 2)),
|
| 541 |
-
"memory_fragmentation": self._calculate_fragmentation(session_id)
|
| 542 |
-
}
|
| 543 |
-
|
| 544 |
-
def _calculate_fragmentation(self, session_id: str = "default") -> float:
|
| 545 |
-
"""๋ฉ๋ชจ๋ฆฌ ๋จํธํ ์ ๋ ๊ณ์ฐ (์ธ์
๋ณ)"""
|
| 546 |
-
if session_id not in self.session_conversations:
|
| 547 |
-
return 0.0
|
| 548 |
-
|
| 549 |
-
conversation_history = self.session_conversations[session_id]
|
| 550 |
-
|
| 551 |
-
if len(conversation_history) <= 1:
|
| 552 |
-
return 0.0
|
| 553 |
-
|
| 554 |
-
# ์ฐ์๋ ๋ฉ์์ง ๊ฐ์ ์๊ฐ ๊ฐ๊ฒฉ์ผ๋ก ๋จํธํ ๊ณ์ฐ
|
| 555 |
-
timestamps = [turn.timestamp for turn in conversation_history]
|
| 556 |
-
intervals = [timestamps[i+1] - timestamps[i] for i in range(len(timestamps)-1)]
|
| 557 |
-
|
| 558 |
-
if not intervals:
|
| 559 |
-
return 0.0
|
| 560 |
-
|
| 561 |
-
avg_interval = sum(intervals) / len(intervals)
|
| 562 |
-
variance = sum((x - avg_interval) ** 2 for x in intervals) / len(intervals)
|
| 563 |
-
|
| 564 |
-
# ์ ๊ทํ๋ ๋จํธํ ์ ์ (0-1)
|
| 565 |
-
return min(1.0, variance / (avg_interval ** 2) if avg_interval > 0 else 0.0)
|
| 566 |
-
|
| 567 |
-
def _check_auto_cleanup(self, session_id: str = "default"):
|
| 568 |
-
"""์๋ ์ ๋ฆฌ ์กฐ๊ฑด ์ฒดํฌ ๋ฐ ์คํ"""
|
| 569 |
-
if not self.auto_cleanup_enabled:
|
| 570 |
-
return
|
| 571 |
-
|
| 572 |
-
current_time = time.time()
|
| 573 |
-
|
| 574 |
-
# ์ธ์
๋ณ ์นด์ดํฐ ์ด๊ธฐํ
|
| 575 |
-
if session_id not in self.turn_counters:
|
| 576 |
-
self.turn_counters[session_id] = 0
|
| 577 |
-
if session_id not in self.last_cleanup_time:
|
| 578 |
-
self.last_cleanup_time[session_id] = current_time
|
| 579 |
-
|
| 580 |
-
# ํด ์นด์ดํฐ ์ฆ๊ฐ
|
| 581 |
-
self.turn_counters[session_id] += 1
|
| 582 |
-
|
| 583 |
-
# ์ ๋ฆฌ ์กฐ๊ฑด ์ฒดํฌ
|
| 584 |
-
should_cleanup = False
|
| 585 |
-
cleanup_reason = ""
|
| 586 |
-
|
| 587 |
-
# ํด ๊ธฐ๋ฐ ์ ๋ฆฌ
|
| 588 |
-
if self.turn_counters[session_id] >= self.cleanup_interval_turns:
|
| 589 |
-
should_cleanup = True
|
| 590 |
-
cleanup_reason = f"ํด ๊ธฐ๋ฐ ({self.turn_counters[session_id]} ํด)"
|
| 591 |
-
|
| 592 |
-
# ์๊ฐ ๊ธฐ๋ฐ ์ ๋ฆฌ
|
| 593 |
-
elif current_time - self.last_cleanup_time[session_id] >= self.cleanup_interval_time:
|
| 594 |
-
should_cleanup = True
|
| 595 |
-
cleanup_reason = f"์๊ฐ ๊ธฐ๋ฐ ({int(current_time - self.last_cleanup_time[session_id])}์ด)"
|
| 596 |
-
|
| 597 |
-
# ์ปจํ
์คํธ ๊ธธ์ด ๊ธฐ๋ฐ ์ ๋ฆฌ (๊ฐํ)
|
| 598 |
-
elif len(self.session_conversations.get(session_id, [])) > self.max_turns:
|
| 599 |
-
should_cleanup = True
|
| 600 |
-
cleanup_reason = f"๊ธธ์ด ๊ธฐ๋ฐ ({len(self.session_conversations.get(session_id, []))} > {self.max_turns})"
|
| 601 |
-
|
| 602 |
-
# ์๋ ์ ๋ฆฌ ์คํ
|
| 603 |
-
if should_cleanup:
|
| 604 |
-
logger.info(f"๐ ์ธ์
{session_id} ์๋ ์ ๋ฆฌ ์์: {cleanup_reason}")
|
| 605 |
-
self._execute_auto_cleanup(session_id)
|
| 606 |
-
|
| 607 |
-
# ์นด์ดํฐ ๋ฐ ์๊ฐ ๋ฆฌ์
|
| 608 |
-
self.turn_counters[session_id] = 0
|
| 609 |
-
self.last_cleanup_time[session_id] = current_time
|
| 610 |
-
|
| 611 |
-
def _execute_auto_cleanup(self, session_id: str = "default"):
|
| 612 |
-
"""์๋ ์ ๋ฆฌ ์คํ"""
|
| 613 |
-
if session_id not in self.session_conversations:
|
| 614 |
-
return
|
| 615 |
-
|
| 616 |
-
conversation_history = self.session_conversations[session_id]
|
| 617 |
-
original_length = len(conversation_history)
|
| 618 |
-
|
| 619 |
-
if original_length <= self.max_turns:
|
| 620 |
-
return
|
| 621 |
-
|
| 622 |
-
# ์ ๋ต๋ณ ์ ๋ฆฌ ์คํ
|
| 623 |
-
if self.cleanup_strategy == "smart":
|
| 624 |
-
self._smart_cleanup(session_id)
|
| 625 |
-
elif self.cleanup_strategy == "aggressive":
|
| 626 |
-
self._aggressive_cleanup(session_id)
|
| 627 |
-
elif self.cleanup_strategy == "conservative":
|
| 628 |
-
self._conservative_cleanup(session_id)
|
| 629 |
-
|
| 630 |
-
final_length = len(conversation_history)
|
| 631 |
-
removed_count = original_length - final_length
|
| 632 |
-
|
| 633 |
-
if removed_count > 0:
|
| 634 |
-
logger.info(f"โ
์ธ์
{session_id} ์๋ ์ ๋ฆฌ ์๋ฃ: {original_length} โ {final_length} ํด (์ ๊ฑฐ: {removed_count})")
|
| 635 |
-
|
| 636 |
-
def _smart_cleanup(self, session_id: str = "default"):
|
| 637 |
-
"""์ค๋งํธ ์ ๋ฆฌ: ์ค์ ๋ฉ์์ง ์ ์ง, ์ค๊ฐ ๋ฉ์์ง ์์ฝ"""
|
| 638 |
-
if session_id not in self.session_conversations:
|
| 639 |
-
return
|
| 640 |
-
|
| 641 |
-
conversation_history = self.session_conversations[session_id]
|
| 642 |
-
|
| 643 |
-
if len(conversation_history) <= self.max_turns:
|
| 644 |
-
return
|
| 645 |
-
|
| 646 |
-
# ์ค์ ๋ฉ์์ง ์ ๊ณ์ฐ (์์คํ
+ ์ต๊ทผ)
|
| 647 |
-
important_count = min(3, self.max_turns // 3)
|
| 648 |
-
recent_count = min(5, self.max_turns // 2)
|
| 649 |
-
|
| 650 |
-
# ์ค๊ฐ ๋ฉ์์ง๋ค ์ ๊ฑฐ
|
| 651 |
-
middle_start = important_count
|
| 652 |
-
middle_end = len(conversation_history) - recent_count
|
| 653 |
-
|
| 654 |
-
if middle_end > middle_start:
|
| 655 |
-
removed_turns = list(conversation_history)[middle_start:middle_end]
|
| 656 |
-
|
| 657 |
-
# ์์ฝ ๋ฉ์์ง ์์ฑ
|
| 658 |
-
summary_content = f"[์ด์ {len(removed_turns)}๊ฐ ๋ฉ์์ง ์์ฝ: {len(removed_turns)}๊ฐ ๋ํ ํด]"
|
| 659 |
-
|
| 660 |
-
# ์ค๊ฐ ๋ถ๋ถ ์ ๊ฑฐ
|
| 661 |
-
for _ in range(middle_end - middle_start):
|
| 662 |
-
conversation_history.pop(middle_start)
|
| 663 |
-
|
| 664 |
-
# ์์ฝ ๋ฉ์์ง ์ถ๊ฐ
|
| 665 |
-
summary_turn = ConversationTurn(
|
| 666 |
-
role="system",
|
| 667 |
-
content=summary_content,
|
| 668 |
-
timestamp=time.time(),
|
| 669 |
-
message_id=f"summary_{int(time.time() * 1000)}"
|
| 670 |
-
)
|
| 671 |
-
conversation_history.insert(middle_start, summary_turn)
|
| 672 |
-
|
| 673 |
-
def _aggressive_cleanup(self, session_id: str = "default"):
|
| 674 |
-
"""์ ๊ทน์ ์ ๋ฆฌ: ์ต๊ทผ ๋ฉ์์ง๋ง ์ ์ง"""
|
| 675 |
-
if session_id not in self.session_conversations:
|
| 676 |
-
return
|
| 677 |
-
|
| 678 |
-
conversation_history = self.session_conversations[session_id]
|
| 679 |
-
|
| 680 |
-
# ์ต๊ทผ max_turns ๊ฐ๋ง ์ ์ง
|
| 681 |
-
while len(conversation_history) > self.max_turns:
|
| 682 |
-
conversation_history.popleft()
|
| 683 |
-
|
| 684 |
-
def _conservative_cleanup(self, session_id: str = "default"):
|
| 685 |
-
"""๋ณด์์ ์ ๋ฆฌ: ์ ์ง์ ์ผ๋ก ์ ๋ฆฌ"""
|
| 686 |
-
if session_id not in self.session_conversations:
|
| 687 |
-
return
|
| 688 |
-
|
| 689 |
-
conversation_history = self.session_conversations[session_id]
|
| 690 |
-
|
| 691 |
-
# 20%์ฉ ์ ์ง์ ์ผ๋ก ์ ๊ฑฐ
|
| 692 |
-
target_length = int(len(conversation_history) * 0.8)
|
| 693 |
-
if target_length > self.max_turns:
|
| 694 |
-
while len(conversation_history) > target_length:
|
| 695 |
-
conversation_history.popleft()
|
| 696 |
-
|
| 697 |
-
# ์ ์ญ ์ปจํ
์คํธ ๊ด๋ฆฌ์ ์ธ์คํด์ค
|
| 698 |
-
context_manager = ContextManager()
|
| 699 |
-
|
| 700 |
-
def get_context_manager() -> ContextManager:
|
| 701 |
-
"""์ ์ญ ์ปจํ
์คํธ ๊ด๋ฆฌ์ ๋ฐํ"""
|
| 702 |
-
return context_manager
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
lily_llm_core/lora_manager_250822_0312.py
DELETED
|
@@ -1,524 +0,0 @@
|
|
| 1 |
-
#!/usr/bin/env python3
|
| 2 |
-
"""
|
| 3 |
-
LoRA/QLoRA ๊ด๋ฆฌ์ (LoRA Manager)
|
| 4 |
-
LoRA ์ด๋ํฐ๋ฅผ ๋ก๋ํ๊ณ ๊ด๋ฆฌํ๋ ์์คํ
|
| 5 |
-
"""
|
| 6 |
-
|
| 7 |
-
import logging
|
| 8 |
-
import os
|
| 9 |
-
import json
|
| 10 |
-
import torch
|
| 11 |
-
from typing import Dict, Any, Optional, List, Union
|
| 12 |
-
from pathlib import Path
|
| 13 |
-
import warnings
|
| 14 |
-
import time
|
| 15 |
-
|
| 16 |
-
# logger๋ฅผ ๋จผ์ ์ ์
|
| 17 |
-
logger = logging.getLogger(__name__)
|
| 18 |
-
|
| 19 |
-
# PEFT ๊ด๋ จ import (์ค์น๋์ง ์์ ๊ฒฝ์ฐ ๊ฒฝ๊ณ )
|
| 20 |
-
try:
|
| 21 |
-
logger.info("๐ PEFT ๋ผ์ด๋ธ๋ฌ๋ฆฌ import ์๋ ์ค...")
|
| 22 |
-
from peft import (
|
| 23 |
-
LoraConfig,
|
| 24 |
-
get_peft_model,
|
| 25 |
-
PeftModel,
|
| 26 |
-
TaskType,
|
| 27 |
-
prepare_model_for_kbit_training
|
| 28 |
-
)
|
| 29 |
-
from peft.utils import get_peft_model_state_dict
|
| 30 |
-
PEFT_AVAILABLE = True
|
| 31 |
-
logger.info("โ
PEFT ๋ผ์ด๋ธ๋ฌ๋ฆฌ import ์ฑ๊ณต")
|
| 32 |
-
except ImportError as e:
|
| 33 |
-
PEFT_AVAILABLE = False
|
| 34 |
-
logger.error(f"โ PEFT ๋ผ์ด๋ธ๋ฌ๋ฆฌ import ์คํจ: {e}")
|
| 35 |
-
logger.error(f"โ Python ๊ฒฝ๋ก: {os.environ.get('PYTHONPATH', 'Not set')}")
|
| 36 |
-
logger.error(f"โ ํ์ฌ ์์
๋๋ ํ ๋ฆฌ: {os.getcwd()}")
|
| 37 |
-
warnings.warn(f"PEFT ๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ฐ ์ค์น๋์ง ์์์ต๋๋ค. LoRA ๊ธฐ๋ฅ์ ์ฌ์ฉํ ์ ์์ต๋๋ค. ์ค๋ฅ: {e}")
|
| 38 |
-
|
| 39 |
-
# Transformers ๊ด๋ จ import
|
| 40 |
-
try:
|
| 41 |
-
logger.info("๐ Transformers ๋ผ์ด๋ธ๋ฌ๋ฆฌ import ์๋ ์ค...")
|
| 42 |
-
from transformers import (
|
| 43 |
-
AutoModelForCausalLM,
|
| 44 |
-
AutoTokenizer,
|
| 45 |
-
BitsAndBytesConfig,
|
| 46 |
-
TrainingArguments,
|
| 47 |
-
Trainer,
|
| 48 |
-
DataCollatorForLanguageModeling
|
| 49 |
-
)
|
| 50 |
-
TRANSFORMERS_AVAILABLE = True
|
| 51 |
-
logger.info("โ
Transformers ๋ผ์ด๋ธ๋ฌ๋ฆฌ import ์ฑ๊ณต")
|
| 52 |
-
except ImportError as e:
|
| 53 |
-
TRANSFORMERS_AVAILABLE = False
|
| 54 |
-
logger.error(f"โ Transformers ๋ผ์ด๋ธ๋ฌ๋ฆฌ import ์คํจ: {e}")
|
| 55 |
-
warnings.warn(f"Transformers ๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ฐ ์ค์น๋์ง ์์์ต๋๋ค. ์ค๋ฅ: {e}")
|
| 56 |
-
|
| 57 |
-
class LoRAManager:
|
| 58 |
-
"""LoRA/QLoRA ๋ชจ๋ธ ๊ด๋ฆฌ ํด๋์ค"""
|
| 59 |
-
|
| 60 |
-
def __init__(self, base_model_path: str = None, device: str = "auto"):
|
| 61 |
-
"""
|
| 62 |
-
Args:
|
| 63 |
-
base_model_path: ๊ธฐ๋ณธ ๋ชจ๋ธ ๊ฒฝ๋ก
|
| 64 |
-
device: ์ฌ์ฉํ ๋๋ฐ์ด์ค ('auto', 'cpu', 'cuda', 'mps')
|
| 65 |
-
"""
|
| 66 |
-
logger.info(f"๐ง LoRA ๊ด๋ฆฌ์ ์ด๊ธฐํ ์์: PEFT_AVAILABLE={PEFT_AVAILABLE}, TRANSFORMERS_AVAILABLE={TRANSFORMERS_AVAILABLE}")
|
| 67 |
-
|
| 68 |
-
if not PEFT_AVAILABLE:
|
| 69 |
-
logger.error("โ PEFT ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๋ค.")
|
| 70 |
-
logger.error("โ pip install peft๋ฅผ ์คํํ๋์ง ํ์ธํ์ธ์.")
|
| 71 |
-
logger.error("โ ๊ฐ์ํ๊ฒฝ์ด ํ์ฑํ๋์ด ์๋์ง ํ์ธํ์ธ์.")
|
| 72 |
-
raise ImportError("PEFT ๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ฐ ํ์ํฉ๋๋ค. pip install peft๋ฅผ ์คํํ์ธ์.")
|
| 73 |
-
|
| 74 |
-
if not TRANSFORMERS_AVAILABLE:
|
| 75 |
-
logger.error("โ Transformers ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๋ค.")
|
| 76 |
-
logger.error("โ pip install transformers๋ฅผ ์คํํ๋์ง ํ์ธํ์ธ์.")
|
| 77 |
-
raise ImportError("Transformers ๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ฐ ํ์ํฉ๋๋ค. pip install transformers๋ฅผ ์คํํ์ธ์.")
|
| 78 |
-
|
| 79 |
-
self.base_model_path = base_model_path
|
| 80 |
-
self.device = self._get_device(device)
|
| 81 |
-
|
| 82 |
-
# ๋ชจ๋ธ ๋ฐ ํ ํฌ๋์ด์
|
| 83 |
-
self.base_model = None
|
| 84 |
-
self.tokenizer = None
|
| 85 |
-
self.lora_model = None
|
| 86 |
-
|
| 87 |
-
# LoRA ์ค์
|
| 88 |
-
self.lora_config = None
|
| 89 |
-
self.current_adapter_name = None
|
| 90 |
-
|
| 91 |
-
# ์ด๋ํฐ ์ ์ฅ ๊ฒฝ๋ก
|
| 92 |
-
self.adapters_dir = Path("lora_adapters")
|
| 93 |
-
self.adapters_dir.mkdir(exist_ok=True)
|
| 94 |
-
|
| 95 |
-
# ๋ก๋๋ ์ด๋ํฐ ๋ชฉ๋ก
|
| 96 |
-
self.loaded_adapters = {}
|
| 97 |
-
|
| 98 |
-
logger.info(f"๐ง LoRA ๊ด๋ฆฌ์ ์ด๊ธฐํ: device={self.device}")
|
| 99 |
-
|
| 100 |
-
def _get_device(self, device: str) -> str:
|
| 101 |
-
"""์ฌ์ฉ ๊ฐ๋ฅํ ๋๋ฐ์ด์ค ํ์ธ"""
|
| 102 |
-
if device == "auto":
|
| 103 |
-
if torch.cuda.is_available():
|
| 104 |
-
return "cuda"
|
| 105 |
-
elif torch.backends.mps.is_available():
|
| 106 |
-
return "mps"
|
| 107 |
-
else:
|
| 108 |
-
return "cpu"
|
| 109 |
-
return device
|
| 110 |
-
|
| 111 |
-
def load_base_model(self, model_path: str = None, model_type: str = "causal_lm") -> bool:
|
| 112 |
-
"""๊ธฐ๋ณธ ๋ชจ๋ธ ๋ก๋"""
|
| 113 |
-
try:
|
| 114 |
-
model_path = model_path or self.base_model_path
|
| 115 |
-
if not model_path:
|
| 116 |
-
raise ValueError("๋ชจ๋ธ ๊ฒฝ๋ก๊ฐ ์ง์ ๋์ง ์์์ต๋๋ค.")
|
| 117 |
-
|
| 118 |
-
logger.info(f"๐ฅ ๊ธฐ๋ณธ ๋ชจ๋ธ ๋ก๋ฉ ์์: {model_path}")
|
| 119 |
-
|
| 120 |
-
# ํ ํฌ๋์ด์ ๋ก๋
|
| 121 |
-
self.tokenizer = AutoTokenizer.from_pretrained(
|
| 122 |
-
model_path,
|
| 123 |
-
trust_remote_code=True,
|
| 124 |
-
local_files_only=os.path.exists(model_path)
|
| 125 |
-
)
|
| 126 |
-
|
| 127 |
-
# ํจ๋ฉ ํ ํฐ ์ค์
|
| 128 |
-
if self.tokenizer.pad_token is None:
|
| 129 |
-
self.tokenizer.pad_token = self.tokenizer.eos_token
|
| 130 |
-
|
| 131 |
-
# ๋ชจ๋ธ ๋ก๋
|
| 132 |
-
if model_type == "causal_lm":
|
| 133 |
-
self.base_model = AutoModelForCausalLM.from_pretrained(
|
| 134 |
-
model_path,
|
| 135 |
-
trust_remote_code=True,
|
| 136 |
-
local_files_only=os.path.exists(model_path),
|
| 137 |
-
torch_dtype=torch.float16 if self.device == "cuda" else torch.float32,
|
| 138 |
-
device_map="auto" if self.device == "cuda" else None
|
| 139 |
-
)
|
| 140 |
-
else:
|
| 141 |
-
raise ValueError(f"์ง์ํ์ง ์๋ ๋ชจ๋ธ ํ์
: {model_type}")
|
| 142 |
-
|
| 143 |
-
# ๋๋ฐ์ด์ค๋ก ์ด๋
|
| 144 |
-
if self.device != "cuda": # cuda๋ device_map="auto" ์ฌ์ฉ
|
| 145 |
-
self.base_model = self.base_model.to(self.device)
|
| 146 |
-
|
| 147 |
-
self.base_model_path = model_path
|
| 148 |
-
logger.info(f"โ
๊ธฐ๋ณธ ๋ชจ๋ธ ๋ก๋ฉ ์๋ฃ: {model_path}")
|
| 149 |
-
return True
|
| 150 |
-
|
| 151 |
-
except Exception as e:
|
| 152 |
-
logger.error(f"โ ๊ธฐ๋ณธ ๋ชจ๋ธ ๋ก๋ฉ ์คํจ: {e}")
|
| 153 |
-
return False
|
| 154 |
-
|
| 155 |
-
def create_lora_config(self,
|
| 156 |
-
r: int = 16,
|
| 157 |
-
lora_alpha: int = 32,
|
| 158 |
-
target_modules: List[str] = None,
|
| 159 |
-
lora_dropout: float = 0.1,
|
| 160 |
-
bias: str = "none",
|
| 161 |
-
task_type: str = "CAUSAL_LM") -> LoraConfig:
|
| 162 |
-
"""LoRA ์ค์ ์์ฑ"""
|
| 163 |
-
if target_modules is None:
|
| 164 |
-
# ์ผ๋ฐ์ ์ธ ๋ชจ๋ธ ์ํคํ
์ฒ์ ๋ํ ๊ธฐ๋ณธ๊ฐ
|
| 165 |
-
target_modules = ["q_proj", "v_proj", "k_proj", "o_proj", "gate_proj", "up_proj", "down_proj"]
|
| 166 |
-
|
| 167 |
-
# TaskType ๋ณํ
|
| 168 |
-
logger.info(f"๐ [DEBUG] ์
๋ ฅ๋ task_type: {task_type}")
|
| 169 |
-
task_type_map = {
|
| 170 |
-
"CAUSAL_LM": TaskType.CAUSAL_LM,
|
| 171 |
-
"SEQ_2_SEQ_LM": TaskType.SEQ_2_SEQ_LM,
|
| 172 |
-
"SEQUENCE_CLASSIFICATION": TaskType.SEQUENCE_CLASSIFICATION,
|
| 173 |
-
"TOKEN_CLASSIFICATION": TaskType.TOKEN_CLASSIFICATION,
|
| 174 |
-
"QUESTION_ANSWERING": TaskType.QUESTION_ANSWERING
|
| 175 |
-
}
|
| 176 |
-
|
| 177 |
-
logger.info(f"๐ [DEBUG] ์ฌ์ฉ ๊ฐ๋ฅํ TaskType: {list(task_type_map.keys())}")
|
| 178 |
-
task_type_enum = task_type_map.get(task_type, TaskType.CAUSAL_LM)
|
| 179 |
-
logger.info(f"๐ [DEBUG] ์ ํ๋ TaskType: {task_type_enum}")
|
| 180 |
-
|
| 181 |
-
self.lora_config = LoraConfig(
|
| 182 |
-
r=r,
|
| 183 |
-
lora_alpha=lora_alpha,
|
| 184 |
-
target_modules=target_modules,
|
| 185 |
-
lora_dropout=lora_dropout,
|
| 186 |
-
bias=bias,
|
| 187 |
-
task_type=task_type_enum
|
| 188 |
-
)
|
| 189 |
-
|
| 190 |
-
logger.info(f"๐ง LoRA ์ค์ ์์ฑ: r={r}, alpha={lora_alpha}, target_modules={target_modules}")
|
| 191 |
-
return self.lora_config
|
| 192 |
-
|
| 193 |
-
def apply_lora_to_model(self, adapter_name: str = "default") -> bool:
|
| 194 |
-
"""LoRA๋ฅผ ๊ธฐ๋ณธ ๋ชจ๋ธ์ ์ ์ฉ"""
|
| 195 |
-
try:
|
| 196 |
-
if self.base_model is None:
|
| 197 |
-
raise ValueError("๊ธฐ๋ณธ ๋ชจ๋ธ์ด ๋ก๋๋์ง ์์์ต๋๋ค.")
|
| 198 |
-
|
| 199 |
-
if self.lora_config is None:
|
| 200 |
-
raise ValueError("LoRA ์ค์ ์ด ์์ฑ๋์ง ์์์ต๋๋ค.")
|
| 201 |
-
|
| 202 |
-
logger.info(f"๐ LoRA ์ด๋ํฐ ์ ์ฉ ์์: {adapter_name}")
|
| 203 |
-
|
| 204 |
-
# LoRA ๋ชจ๋ธ ์์ฑ
|
| 205 |
-
self.lora_model = get_peft_model(self.base_model, self.lora_config)
|
| 206 |
-
|
| 207 |
-
# ์ด๋ํฐ ์ด๋ฆ ์ค์
|
| 208 |
-
self.current_adapter_name = adapter_name
|
| 209 |
-
|
| 210 |
-
# ํ๋ จ ๋ชจ๋๋ก ์ค์
|
| 211 |
-
self.lora_model.train()
|
| 212 |
-
|
| 213 |
-
# ๋ชจ๋ธ ์ ๋ณด ์ถ๋ ฅ
|
| 214 |
-
self.lora_model.print_trainable_parameters()
|
| 215 |
-
|
| 216 |
-
logger.info(f"โ
LoRA ์ด๋ํฐ ์ ์ฉ ์๋ฃ: {adapter_name}")
|
| 217 |
-
return True
|
| 218 |
-
|
| 219 |
-
except Exception as e:
|
| 220 |
-
logger.error(f"โ LoRA ์ด๋ํฐ ์ ์ฉ ์คํจ: {e}")
|
| 221 |
-
return False
|
| 222 |
-
|
| 223 |
-
def load_lora_adapter(self, adapter_path: str, adapter_name: str = None) -> bool:
|
| 224 |
-
"""์ ์ฅ๋ LoRA ์ด๋ํฐ ๋ก๋"""
|
| 225 |
-
try:
|
| 226 |
-
if not os.path.exists(adapter_path):
|
| 227 |
-
raise FileNotFoundError(f"์ด๋ํฐ ๊ฒฝ๋ก๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค: {adapter_path}")
|
| 228 |
-
|
| 229 |
-
if adapter_name is None:
|
| 230 |
-
adapter_name = Path(adapter_path).stem
|
| 231 |
-
|
| 232 |
-
logger.info(f"๐ฅ LoRA ์ด๋ํฐ ๋ก๋ฉ ์์: {adapter_path}")
|
| 233 |
-
|
| 234 |
-
# ๊ธฐ๋ณธ ๋ชจ๋ธ์ด ๋ก๋๋์ง ์์ ๊ฒฝ์ฐ ๋ก๋
|
| 235 |
-
if self.base_model is None:
|
| 236 |
-
# ์ด๋ํฐ ์ค์ ํ์ผ์์ ๊ธฐ๋ณธ ๋ชจ๋ธ ๊ฒฝ๋ก ํ์ธ
|
| 237 |
-
config_path = os.path.join(adapter_path, "adapter_config.json")
|
| 238 |
-
if os.path.exists(config_path):
|
| 239 |
-
with open(config_path, 'r') as f:
|
| 240 |
-
config = json.load(f)
|
| 241 |
-
base_model_path = config.get("base_model_name_or_path")
|
| 242 |
-
if base_model_path:
|
| 243 |
-
self.load_base_model(base_model_path)
|
| 244 |
-
|
| 245 |
-
# LoRA ์ด๋ํฐ ๋ก๋
|
| 246 |
-
self.lora_model = PeftModel.from_pretrained(
|
| 247 |
-
self.base_model,
|
| 248 |
-
adapter_path,
|
| 249 |
-
torch_dtype=torch.float16 if self.device == "cuda" else torch.float32
|
| 250 |
-
)
|
| 251 |
-
|
| 252 |
-
# ๋๋ฐ์ด์ค๋ก ์ด๋
|
| 253 |
-
if self.device != "cuda":
|
| 254 |
-
self.lora_model = self.lora_model.to(self.device)
|
| 255 |
-
|
| 256 |
-
self.current_adapter_name = adapter_name
|
| 257 |
-
self.loaded_adapters[adapter_name] = adapter_path
|
| 258 |
-
|
| 259 |
-
logger.info(f"โ
LoRA ์ด๋ํฐ ๋ก๋ฉ ์๋ฃ: {adapter_name}")
|
| 260 |
-
return True
|
| 261 |
-
|
| 262 |
-
except Exception as e:
|
| 263 |
-
logger.error(f"โ LoRA ์ด๋ํฐ ๋ก๋ฉ ์คํจ: {e}")
|
| 264 |
-
return False
|
| 265 |
-
|
| 266 |
-
def save_lora_adapter(self, adapter_name: str = None, output_dir: str = None) -> bool:
|
| 267 |
-
"""LoRA ์ด๋ํฐ ์ ์ฅ"""
|
| 268 |
-
try:
|
| 269 |
-
if self.lora_model is None:
|
| 270 |
-
raise ValueError("LoRA ๋ชจ๋ธ์ด ๋ก๋๋์ง ์์์ต๋๋ค.")
|
| 271 |
-
|
| 272 |
-
adapter_name = adapter_name or self.current_adapter_name or "default"
|
| 273 |
-
output_dir = output_dir or str(self.adapters_dir / adapter_name)
|
| 274 |
-
|
| 275 |
-
logger.info(f"๐พ LoRA ์ด๋ํฐ ์ ์ฅ ์์: {adapter_name} -> {output_dir}")
|
| 276 |
-
|
| 277 |
-
# ์ด๋ํฐ ์ ์ฅ
|
| 278 |
-
self.lora_model.save_pretrained(output_dir)
|
| 279 |
-
|
| 280 |
-
# ํ ํฌ๋์ด์ ๋ ์ ์ฅ
|
| 281 |
-
if self.tokenizer:
|
| 282 |
-
self.tokenizer.save_pretrained(output_dir)
|
| 283 |
-
|
| 284 |
-
# ์ด๋ํฐ ์ ๋ณด ์ ์ฅ
|
| 285 |
-
adapter_info = {
|
| 286 |
-
"adapter_name": adapter_name,
|
| 287 |
-
"base_model": self.base_model_path,
|
| 288 |
-
"lora_config": self.lora_config.to_dict() if self.lora_config else None,
|
| 289 |
-
"created_at": str(torch.tensor(time.time())),
|
| 290 |
-
"device": self.device
|
| 291 |
-
}
|
| 292 |
-
|
| 293 |
-
with open(os.path.join(output_dir, "adapter_info.json"), 'w') as f:
|
| 294 |
-
json.dump(adapter_info, f, indent=2)
|
| 295 |
-
|
| 296 |
-
logger.info(f"โ
LoRA ์ด๋ํฐ ์ ์ฅ ์๋ฃ: {output_dir}")
|
| 297 |
-
return True
|
| 298 |
-
|
| 299 |
-
except Exception as e:
|
| 300 |
-
logger.error(f"โ LoRA ์ด๋ํฐ ์ ์ฅ ์คํจ: {e}")
|
| 301 |
-
return False
|
| 302 |
-
|
| 303 |
-
def merge_lora_with_base(self, output_path: str = None) -> bool:
|
| 304 |
-
"""LoRA ์ด๋ํฐ๋ฅผ ๊ธฐ๋ณธ ๋ชจ๋ธ๊ณผ ๋ณํฉ"""
|
| 305 |
-
try:
|
| 306 |
-
if self.lora_model is None:
|
| 307 |
-
raise ValueError("LoRA ๋ชจ๋ธ์ด ๋ก๋๋์ง ์์์ต๋๋ค.")
|
| 308 |
-
|
| 309 |
-
output_path = output_path or f"{self.base_model_path}_merged"
|
| 310 |
-
|
| 311 |
-
logger.info(f"๐ LoRA ์ด๋ํฐ ๋ณํฉ ์์: {output_path}")
|
| 312 |
-
|
| 313 |
-
# ๋ณํฉ๋ ๋ชจ๋ธ ์์ฑ
|
| 314 |
-
merged_model = self.lora_model.merge_and_unload()
|
| 315 |
-
|
| 316 |
-
# ๋ณํฉ๋ ๋ชจ๋ธ ์ ์ฅ
|
| 317 |
-
merged_model.save_pretrained(output_path)
|
| 318 |
-
|
| 319 |
-
# ํ ํฌ๋์ด์ ๋ ์ ์ฅ
|
| 320 |
-
if self.tokenizer:
|
| 321 |
-
self.tokenizer.save_pretrained(output_path)
|
| 322 |
-
|
| 323 |
-
logger.info(f"โ
LoRA ์ด๋ํฐ ๋ณํฉ ์๋ฃ: {output_path}")
|
| 324 |
-
return True
|
| 325 |
-
|
| 326 |
-
except Exception as e:
|
| 327 |
-
logger.error(f"โ LoRA ์ด๋ํฐ ๋ณํฉ ์คํจ: {e}")
|
| 328 |
-
return False
|
| 329 |
-
|
| 330 |
-
def list_available_adapters(self) -> List[Dict[str, Any]]:
|
| 331 |
-
"""์ฌ์ฉ ๊ฐ๋ฅํ ์ด๋ํฐ ๋ชฉ๋ก ๋ฐํ"""
|
| 332 |
-
adapters = []
|
| 333 |
-
|
| 334 |
-
for adapter_dir in self.adapters_dir.iterdir():
|
| 335 |
-
if adapter_dir.is_dir():
|
| 336 |
-
config_path = adapter_dir / "adapter_config.json"
|
| 337 |
-
info_path = adapter_dir / "adapter_info.json"
|
| 338 |
-
|
| 339 |
-
adapter_info = {
|
| 340 |
-
"name": adapter_dir.name,
|
| 341 |
-
"path": str(adapter_dir),
|
| 342 |
-
"config_exists": config_path.exists(),
|
| 343 |
-
"info_exists": info_path.exists()
|
| 344 |
-
}
|
| 345 |
-
|
| 346 |
-
# ์ด๋ํฐ ์ ๋ณด ๋ก๋
|
| 347 |
-
if info_path.exists():
|
| 348 |
-
try:
|
| 349 |
-
with open(info_path, 'r') as f:
|
| 350 |
-
info = json.load(f)
|
| 351 |
-
adapter_info.update(info)
|
| 352 |
-
except Exception as e:
|
| 353 |
-
logger.warning(f"์ด๋ํฐ ์ ๋ณด ๋ก๋ ์คํจ: {e}")
|
| 354 |
-
|
| 355 |
-
adapters.append(adapter_info)
|
| 356 |
-
|
| 357 |
-
return adapters
|
| 358 |
-
|
| 359 |
-
def get_adapter_stats(self) -> Dict[str, Any]:
|
| 360 |
-
"""์ด๋ํฐ ํต๊ณ ์ ๋ณด ๋ฐํ"""
|
| 361 |
-
if self.lora_model is None:
|
| 362 |
-
return {"error": "LoRA ๋ชจ๋ธ์ด ๋ก๋๋์ง ์์์ต๋๋ค."}
|
| 363 |
-
|
| 364 |
-
try:
|
| 365 |
-
# ํ๋ จ ๊ฐ๋ฅํ ํ๋ผ๋ฏธํฐ ์
|
| 366 |
-
trainable_params = 0
|
| 367 |
-
all_param = 0
|
| 368 |
-
|
| 369 |
-
for param in self.lora_model.parameters():
|
| 370 |
-
all_param += param.numel()
|
| 371 |
-
if param.requires_grad:
|
| 372 |
-
trainable_params += param.numel()
|
| 373 |
-
|
| 374 |
-
return {
|
| 375 |
-
"adapter_name": self.current_adapter_name,
|
| 376 |
-
"trainable_params": trainable_params,
|
| 377 |
-
"all_params": all_param,
|
| 378 |
-
"trainable_ratio": trainable_params / all_param if all_param > 0 else 0,
|
| 379 |
-
"device": self.device,
|
| 380 |
-
"model_type": type(self.lora_model).__name__
|
| 381 |
-
}
|
| 382 |
-
|
| 383 |
-
except Exception as e:
|
| 384 |
-
logger.error(f"์ด๋ํฐ ํต๊ณ ์์ง ์คํจ: {e}")
|
| 385 |
-
return {"error": str(e)}
|
| 386 |
-
|
| 387 |
-
def switch_adapter(self, adapter_name: str) -> bool:
|
| 388 |
-
"""๋ค๋ฅธ ์ด๋ํฐ๋ก ์ ํ"""
|
| 389 |
-
try:
|
| 390 |
-
if adapter_name not in self.loaded_adapters:
|
| 391 |
-
# ์ด๋ํฐ ๋ก๋
|
| 392 |
-
adapter_path = self.adapters_dir / adapter_name
|
| 393 |
-
if not adapter_path.exists():
|
| 394 |
-
raise FileNotFoundError(f"์ด๋ํฐ๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค: {adapter_name}")
|
| 395 |
-
|
| 396 |
-
return self.load_lora_adapter(str(adapter_path), adapter_name)
|
| 397 |
-
else:
|
| 398 |
-
# ์ด๋ฏธ ๋ก๋๋ ์ด๋ํฐ ์ฌ์ฉ
|
| 399 |
-
self.current_adapter_name = adapter_name
|
| 400 |
-
logger.info(f"๐ ์ด๋ํฐ ์ ํ: {adapter_name}")
|
| 401 |
-
return True
|
| 402 |
-
|
| 403 |
-
except Exception as e:
|
| 404 |
-
logger.error(f"โ ์ด๋ํฐ ์ ํ ์คํจ: {e}")
|
| 405 |
-
return False
|
| 406 |
-
|
| 407 |
-
def unload_adapter(self) -> bool:
|
| 408 |
-
"""LoRA ์ด๋ํฐ ์ธ๋ก๋"""
|
| 409 |
-
try:
|
| 410 |
-
if self.lora_model is None:
|
| 411 |
-
return True
|
| 412 |
-
|
| 413 |
-
logger.info("๐๏ธ LoRA ์ด๋ํฐ ์ธ๋ก๋ ์์")
|
| 414 |
-
|
| 415 |
-
# ์ด๋ํฐ ์ ๊ฑฐ
|
| 416 |
-
self.lora_model = None
|
| 417 |
-
self.current_adapter_name = None
|
| 418 |
-
self.lora_config = None
|
| 419 |
-
|
| 420 |
-
logger.info("โ
LoRA ์ด๋ํฐ ์ธ๋ก๋ ์๋ฃ")
|
| 421 |
-
return True
|
| 422 |
-
|
| 423 |
-
except Exception as e:
|
| 424 |
-
logger.error(f"โ LoRA ์ด๋ํฐ ์ธ๋ก๋ ์คํจ: {e}")
|
| 425 |
-
return False
|
| 426 |
-
|
| 427 |
-
def generate_text(self, prompt: str, max_length: int = 100, temperature: float = 0.7) -> str:
|
| 428 |
-
"""LoRA ๋ชจ๋ธ์ ์ฌ์ฉํ ํ
์คํธ ์์ฑ"""
|
| 429 |
-
try:
|
| 430 |
-
if self.lora_model is None:
|
| 431 |
-
raise ValueError("LoRA ๋ชจ๋ธ์ด ๋ก๋๋์ง ์์์ต๋๋ค.")
|
| 432 |
-
|
| 433 |
-
if self.tokenizer is None:
|
| 434 |
-
raise ValueError("ํ ํฌ๋์ด์ ๊ฐ ๋ก๋๋์ง ์์์ต๋๋ค.")
|
| 435 |
-
|
| 436 |
-
# ์
๋ ฅ ํ ํฌ๋์ด์ง
|
| 437 |
-
inputs = self.tokenizer(prompt, return_tensors="pt")
|
| 438 |
-
inputs = {k: v.to(self.device) for k, v in inputs.items()}
|
| 439 |
-
|
| 440 |
-
# ์ถ๋ก ๋ชจ๋๋ก ์ค์
|
| 441 |
-
self.lora_model.eval()
|
| 442 |
-
|
| 443 |
-
with torch.no_grad():
|
| 444 |
-
outputs = self.lora_model.generate(
|
| 445 |
-
**inputs,
|
| 446 |
-
max_new_tokens=max_length,
|
| 447 |
-
temperature=temperature,
|
| 448 |
-
do_sample=True,
|
| 449 |
-
pad_token_id=self.tokenizer.eos_token_id
|
| 450 |
-
)
|
| 451 |
-
|
| 452 |
-
# ์๋ต ๋์ฝ๋ฉ
|
| 453 |
-
response = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 454 |
-
|
| 455 |
-
# ํ๋กฌํํธ ์ ๊ฑฐ
|
| 456 |
-
if response.startswith(prompt):
|
| 457 |
-
response = response[len(prompt):].strip()
|
| 458 |
-
|
| 459 |
-
return response
|
| 460 |
-
|
| 461 |
-
except Exception as e:
|
| 462 |
-
logger.error(f"โ ํ
์คํธ ์์ฑ ์คํจ: {e}")
|
| 463 |
-
return f"ํ
์คํธ ์์ฑ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}"
|
| 464 |
-
|
| 465 |
-
def prepare_for_training(self, training_args: TrainingArguments = None) -> bool:
|
| 466 |
-
"""ํ๋ จ์ ์ํ ๋ชจ๋ธ ์ค๋น"""
|
| 467 |
-
try:
|
| 468 |
-
if self.lora_model is None:
|
| 469 |
-
raise ValueError("LoRA ๋ชจ๋ธ์ด ๋ก๋๋์ง ์์์ต๋๋ค.")
|
| 470 |
-
|
| 471 |
-
logger.info("๐ง ํ๋ จ์ ์ํ ๋ชจ๋ธ ์ค๋น ์์")
|
| 472 |
-
|
| 473 |
-
# ๊ธฐ๋ณธ ํ๋ จ ์ธ์
|
| 474 |
-
if training_args is None:
|
| 475 |
-
training_args = TrainingArguments(
|
| 476 |
-
output_dir="./lora_training_output",
|
| 477 |
-
num_train_epochs=3,
|
| 478 |
-
per_device_train_batch_size=4,
|
| 479 |
-
gradient_accumulation_steps=4,
|
| 480 |
-
learning_rate=2e-4,
|
| 481 |
-
warmup_steps=100,
|
| 482 |
-
logging_steps=10,
|
| 483 |
-
save_steps=500,
|
| 484 |
-
eval_steps=500,
|
| 485 |
-
evaluation_strategy="steps",
|
| 486 |
-
save_strategy="steps",
|
| 487 |
-
load_best_model_at_end=True,
|
| 488 |
-
metric_for_best_model="eval_loss",
|
| 489 |
-
greater_is_better=False,
|
| 490 |
-
fp16=torch.cuda.is_available(),
|
| 491 |
-
dataloader_pin_memory=False,
|
| 492 |
-
)
|
| 493 |
-
|
| 494 |
-
# ํ๋ จ ๋ชจ๋๋ก ์ค์
|
| 495 |
-
self.lora_model.train()
|
| 496 |
-
|
| 497 |
-
# ๊ทธ๋๋์ธํธ ์ฒดํฌํฌ์ธํ
ํ์ฑํ (๋ฉ๋ชจ๋ฆฌ ๏ฟฝ๏ฟฝ๏ฟฝ์ฝ)
|
| 498 |
-
self.lora_model.gradient_checkpointing_enable()
|
| 499 |
-
|
| 500 |
-
# ๊ทธ๋๋์ธํธ ํด๋ฆฌํ ์ค์
|
| 501 |
-
self.lora_model.enable_input_require_grads()
|
| 502 |
-
|
| 503 |
-
logger.info("โ
ํ๋ จ์ ์ํ ๋ชจ๋ธ ์ค๋น ์๋ฃ")
|
| 504 |
-
return True
|
| 505 |
-
|
| 506 |
-
except Exception as e:
|
| 507 |
-
logger.error(f"โ ํ๋ จ ์ค๋น ์คํจ: {e}")
|
| 508 |
-
return False
|
| 509 |
-
|
| 510 |
-
# ์ ์ญ LoRA ๊ด๋ฆฌ์ ์ธ์คํด์ค (์์ ํ ์์ฑ)
|
| 511 |
-
try:
|
| 512 |
-
if PEFT_AVAILABLE and TRANSFORMERS_AVAILABLE:
|
| 513 |
-
lora_manager = LoRAManager()
|
| 514 |
-
logger.info("โ
์ ์ญ LoRA ๊ด๋ฆฌ์ ์ธ์คํด์ค ์์ฑ ์๋ฃ")
|
| 515 |
-
else:
|
| 516 |
-
lora_manager = None
|
| 517 |
-
logger.warning("โ ๏ธ LoRA ๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ฐ ์ฌ์ฉ ๋ถ๊ฐ๋ฅํ์ฌ LoRA ๊ด๋ฆฌ์๋ฅผ ์์ฑํ์ง ์์์ต๋๋ค.")
|
| 518 |
-
except Exception as e:
|
| 519 |
-
lora_manager = None
|
| 520 |
-
logger.error(f"โ LoRA ๊ด๋ฆฌ์ ์ธ์คํด์ค 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