Spaces:
Sleeping
Sleeping
File size: 5,251 Bytes
0a47069 6be7d0b e80a3bc 0a47069 0482473 0a47069 24c2c08 6be7d0b 0a47069 e80a3bc 0a47069 0482473 24c2c08 6be7d0b ec95760 0a47069 9009e60 0a47069 9009e60 0a47069 9009e60 0a47069 3eb99bc 24c2c08 e80a3bc 24c2c08 e80a3bc 24c2c08 e80a3bc 24c2c08 e80a3bc |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# 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 |