Skip to content

Commit 2bb864d

Browse files
committed
Make emotion state resonance affect memory retrieval #2
1 parent c4a4381 commit 2bb864d

2 files changed

Lines changed: 35 additions & 4 deletions

File tree

main.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,11 @@ def _run_reasoning_cycle(
201201
flashback_memory: str,
202202
) -> RuntimeState:
203203
memory_query = user_message or trauma_memory or flashback_memory or visual_summary
204-
past_memories = hippocampus.retrieve_memory(memory_query)
204+
past_memories = hippocampus.retrieve_memory(
205+
memory_query,
206+
current_arousal=emotion_net.arousal,
207+
current_mood=emotion_net.mood,
208+
)
205209
recent_context = _format_recent_context(_load_recent_context())
206210
retrieved_memory_context = "\n".join(past_memories)
207211

memory.py

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,8 @@ class MemoryManager:
202202
# --- 기억 조회 관련 감정 영향도 ---
203203
RETRIEVAL_FETCH_MULTIPLIER = 4 # 유사도 후보를 조금 넓게 뽑은 뒤 감정 점수로 재정렬
204204
RETRIEVAL_SURPRISE_TRAUMA_WEIGHT = 0.2
205+
RETRIEVAL_MOOD_RESONANCE_WEIGHT = 0.08 # 현재 기분과 같은 방향의 정서 기억은 더 쉽게 떠오름 (기분 좋을땐 좋은 생각)
206+
RETRIEVAL_AROUSAL_RESONANCE_WEIGHT = 0.04 # 현재 각성도와 비슷한 강도의 기억은 약하게 공명
205207
# 아래 상수 합은 1로 맞추는 것을 권장 (KIND_RETRIEVAL_BONUS와 적정 스케일로 맞출 것)
206208
RETRIEVAL_SIMILARITY_WEIGHT = 0.62 # 의미적 관련성은 항상 1순위로 유지 (이성 판단)
207209
RETRIEVAL_AROUSAL_WEIGHT = 0.18 # 각성도가 높은 기억은 현재 판단에 더 잘 끼어듦
@@ -266,18 +268,37 @@ def store_memory(
266268
)
267269
)
268270

269-
def retrieve_memory(self, query: str) -> list[str]:
271+
def retrieve_memory(
272+
self,
273+
query: str,
274+
current_arousal: float = 0.0,
275+
current_mood: float = 0.0,
276+
) -> list[str]:
270277
clean_query = query.strip()
271278
if not clean_query:
272279
return []
273280

274281
query_embedding = self._embed_text(clean_query)
275282
retrieved_candidates: list[tuple[float, str]] = []
276283
retrieved_candidates.extend(
277-
self._query_collection(self.hot_storage, query_embedding, self.HOT_RESULTS_PER_QUERY, "VIVID")
284+
self._query_collection(
285+
self.hot_storage,
286+
query_embedding,
287+
self.HOT_RESULTS_PER_QUERY,
288+
"VIVID",
289+
current_arousal,
290+
current_mood,
291+
)
278292
)
279293
retrieved_candidates.extend(
280-
self._query_collection(self.cold_storage, query_embedding, self.COLD_RESULTS_PER_QUERY, "DISTANT")
294+
self._query_collection(
295+
self.cold_storage,
296+
query_embedding,
297+
self.COLD_RESULTS_PER_QUERY,
298+
"DISTANT",
299+
current_arousal,
300+
current_mood,
301+
)
281302
)
282303

283304
retrieved_candidates.sort(key=lambda item: item[0], reverse=True)
@@ -543,6 +564,8 @@ def _query_collection(
543564
query_embedding: list[float],
544565
result_count: int,
545566
label: str,
567+
current_arousal: float = 0.0,
568+
current_mood: float = 0.0,
546569
) -> list[tuple[float, str]]:
547570
collection_count = self._chroma_call(collection.count, fallback=0)
548571
if collection_count == 0:
@@ -582,11 +605,15 @@ def _query_collection(
582605
# 그런데 계속 관련 없는 기억이 답변을 오염함 → 나쁨. retrieval 스케일 조정 필요 (지나친 딴소리)
583606
# 혹은, 특정 threat/reward가 거의 항상 튀어나옴 → kind/arousal 가중치 과함 (지나친 과민 반응)
584607
similarity_score = 1.0 / (1.0 + max(0.0, float(distance)))
608+
mood_resonance = max(0.0, current_mood * valence)
609+
arousal_resonance = 1.0 - min(1.0, abs(current_arousal - arousal))
585610
affective_score = (
586611
self.RETRIEVAL_SIMILARITY_WEIGHT * similarity_score
587612
+ self.RETRIEVAL_AROUSAL_WEIGHT * arousal
588613
+ self.RETRIEVAL_SURPRISE_WEIGHT * surprise
589614
+ self.RETRIEVAL_VALENCE_WEIGHT * abs(valence)
615+
+ self.RETRIEVAL_MOOD_RESONANCE_WEIGHT * mood_resonance
616+
+ self.RETRIEVAL_AROUSAL_RESONANCE_WEIGHT * arousal_resonance
590617
+ self.KIND_RETRIEVAL_BONUS.get(kind, 0.0)
591618
)
592619

0 commit comments

Comments
 (0)