Spaces:
Sleeping
Sleeping
# portfolio/npc_social_network/npc/npc_memory.py | |
from datetime import datetime | |
from typing import TYPE_CHECKING, List, Optional | |
# ์ํ ์ฐธ์กฐ ๋ฐฉ์ง๋ฅผ ์ํด TYPE_CHECKING์ ์ฌ์ฉ | |
if TYPE_CHECKING: | |
from .npc_base import NPC | |
class Memory: | |
""" | |
ํ๋์ ๊ธฐ์ต ์ ๋ณด๋ฅผ ํํํ๋ ํด๋์ค | |
- content: ๊ธฐ์ต ๋ด์ฉ | |
- timestamp: ๊ธฐ์ตํ ์๊ฐ (๊ธฐ๋ณธ๊ฐ์ ํ์ฌ ์๊ฐ) | |
- importance: ์ค์๋ (1~10) | |
- emotion: ๊ฐ์ ํ๊ทธ (๊ธฐ์จ, ์ฌํ, ๋ถ๋ ธ, ์ค๋ฆฝ ๋ฑ) | |
- is_long_term: ์ฅ๊ธฐ ๊ธฐ์ต ์ฌ๋ถ | |
- behavior_trace: ๊ธฐ์ต์ ํตํ ํ๋ ์ถ์ | |
""" | |
def __init__(self, content: str, timestamp: datetime = None, importance: int = 1, | |
emotion: str = "neutral", behavior_trace: str = None, memory_type: str = "Event", | |
context_tags: Optional[List[str]] = None): | |
self.content = content | |
self.timestamp = timestamp or datetime.now() | |
self.importance = float(importance) | |
self.emotion = emotion | |
self.is_long_term = False | |
self.behavior_trace = behavior_trace | |
self.memory_type = memory_type | |
self.context_tags = context_tags if context_tags is not None else [] | |
self.is_shared = False | |
class MemoryStore: | |
""" | |
๊ธฐ์ต ์ ์ฅ์: ๋จ๊ธฐ/์ฅ๊ธฐ ๊ธฐ์ต์ ๊ตฌ๋ถํ์ฌ ์ ์ฅ | |
""" | |
def __init__(self): | |
self.short_term: List[Memory] = [] # ๋จ๊ธฐ ๊ธฐ์ต | |
self.long_term: List[Memory] = [] # ์ฅ๊ธฐ ๊ธฐ์ต | |
def add_memory(self, memory: Memory): | |
""" | |
์ค์๋๊ฐ ๋์ ๊ธฐ์ต์ ์ฅ๊ธฐ ๊ธฐ์ต์ผ๋ก, ๊ทธ๋ ์ง ์์ผ๋ฉด ๋จ๊ธฐ ๊ธฐ์ต์ผ๋ก ์ ์ฅ | |
- ์์ง ๊ธฐ์ต์ ํญ์ ์ต์์ ์ค์๋๋ฅผ ๊ฐ์ง๋ฉฐ ์ฅ๊ธฐ ๊ธฐ์ต์ผ๋ก ์ ์ฅ | |
""" | |
if memory.memory_type == "Symbolic": | |
memory.importance = 10.0 | |
memory.is_long_term = True | |
if memory.importance >=7 or memory.memory_type == "Symbolic": | |
memory.is_long_term = True | |
self.long_term.append(memory) | |
else: | |
self.short_term.append(memory) | |
def promote_memories(self, time_threshold_minutes: int = 60): | |
""" | |
์๊ฐ์ด ์ง๋ ๋จ๊ธฐ ๊ธฐ์ต ์ค ์ค์๋๊ฐ ์ผ์ ์ด์์ธ ๊ฒ๋ค์ ์ฅ๊ธฐ ๊ธฐ์ต์ผ๋ก ์น๊ฒฉ | |
""" | |
now = datetime.now() | |
promoted = [] | |
for mem in self.short_term: | |
elapsed = (now - mem.timestamp).total_seconds() / 60 | |
if elapsed > time_threshold_minutes and mem.importance >= 5: | |
mem.is_long_term = True | |
self.long_term.append(mem) | |
promoted.append(mem) | |
# ๋จ๊ธฐ ๊ธฐ์ต์์ ์ ๊ฑฐ | |
self.short_term = [m for m in self.short_term if m not in promoted] | |
def get_all_memories(self): | |
""" | |
๋จ๊ธฐ + ์ฅ๊ธฐ ๊ธฐ์ต ์ ์ฒด ๋ฐํ | |
""" | |
return self.long_term + self.short_term | |
def get_recent_memories(self, limit=10): | |
""" | |
์ต์ Memory n๊ฐ ๋ฐํ (๊ธฐ๋ณธ 10๊ฐ) | |
""" | |
all_memories = self.get_all_memories() | |
all_memories.sort(key=lambda m: m.timestamp, reverse=True) | |
return all_memories[:limit] | |
def decay_memories(self, npc: "NPC", min_importance=0.1): | |
""" | |
MemoryType์ ๋ฐ๋ผ decay ์ ์ฉ | |
- ๋จ๊ธฐ ๊ธฐ์ต์ decay_rate_short ๋งํผ importance ๊ฐ์ (๊ธฐ๋ณธ 5% ๊ฐ์) | |
- ์ฅ๊ธฐ ๊ธฐ์ต์ decay_rate_long ๋งํผ importance ๊ฐ์ (๊ธฐ๋ณธ 1% ๊ฐ์) | |
- ๊ธฐ์ต ํ์ ์ ๋ฐ๋ฅธ importance ์ฐจ๋ฑ ๊ฐ์ (๋ํ, ํ๋, ์ฌ๊ฑด, ๊ด๊ณ) | |
""" | |
from .emotion_config import EMOTION_RELATION_IMPACT | |
# ์ฒ๋ฆฌํ ๋ชจ๋ ๊ธฐ์ต์ ์์ ๋ฆฌ์คํธ์ ๋ณต์ฌ | |
all_memorise = self.get_all_memories() | |
for mem in all_memorise: | |
# 1. ๊ธฐ๋ณธ ๊ฐ์ ์จ ์ค์ | |
base_decay = 0.02 if mem.is_long_term else 0.05 | |
# 2. ๊ฐ์ ๊ฐ๋์ ๋ฐ๋ฅธ ์กฐ์ (๊ฐ์ ์ด ๊ฐํ ์๋ก ๋ ์์) | |
# ๊ฐ์ ์ํฅ๋ ์ ๋๊ฐ์ ์ฌ์ฉ (๊ธ์ /๋ถ์ ๋ชจ๋ ๊ฐํ ๊ฐ์ ์ผ๋ก ์ทจ๊ธ) | |
emotion_impact = abs(EMOTION_RELATION_IMPACT.get(mem.emotion, 0.0)) | |
# ๊ฐ์ ๊ฐ๋๊ฐ ๋์์๋ก ๊ฐ์ ์จ์ด ๋ฎ์์ง (0.5 ~ 1.5 ์ฌ์ด๋ก ์กฐ์ ) | |
emotion_modifier = 1.0 - (emotion_impact / 4.0) # IMPACT ์ต๋๊ฐ์ด 2.0์ด๋ฏ๋ก 4๋ก ๋๋ ์ ์ํฅ๋ ฅ ์กฐ์ | |
# 3. ์ฑ๊ฒฉ์ ๋ฐ๋ฅธ ์กฐ์ | |
personality = npc.personality | |
# ๋ด์ฑ์ (stoic)์ผ์๋ก ๊ฐ์ ์ ๊ณฑ์น์ผ๋ฉฐ ๋ ์ค๋ ๊ธฐ์ต (๊ฐ์ ์จ ๊ฐ์) | |
# ๋ฏผ๊ฐํ (sensitive)์๋ก ๊ฐ์ ์ ๊ธฐ์ต์ ๋ ์ค๋ ๊ฐ์งํจ (๊ฐ์ ์จ ๊ฐ์) | |
personality_modifier = 1.0 - personality.get("stoic", 0.5) * 0.5 -personality.get("sensitive", 0.5) * 0.5 | |
personality_modifier = max(0.1, personality_modifier) # 0 ๋๋ ์์๊ฐ ๋์ง ์๋๋ก ๋ฐฉ์ง | |
# ์ต์ข ๊ฐ์ ์จ ๊ณ์ฐ | |
final_decay_rate = base_decay * emotion_modifier * personality_modifier | |
final_decay_rate = max(0.005, min(final_decay_rate, 0.1)) | |
# ์ค์๋ ์ ๋ฐ์ดํธ | |
mem.importance -= final_decay_rate | |
if mem.importance < min_importance: | |
mem.importance = 0 |