Spaces:
Sleeping
Sleeping
File size: 28,661 Bytes
0a47069 e80a3bc 6b7a4d5 9df6a7e e80a3bc 5b8b298 ef82d45 c08ff6f f302085 1a3c349 c08ff6f 1a6fb12 033af6f 146c098 c08ff6f e80a3bc 2fe1b55 c08ff6f d57920f c08ff6f e80a3bc 64fce5c 9df6a7e d505821 9df6a7e d505821 9df6a7e d505821 9df6a7e d505821 9df6a7e d505821 9df6a7e d505821 9df6a7e 93a860a 0a47069 d57920f 93a860a d57920f 93a860a 146c098 93a860a 0a47069 9df6a7e 7b26e52 9df6a7e e80a3bc d57920f 9df6a7e a147abc ef82d45 ce3d575 aabeb2d ef82d45 cf1ebab 96692cf cf1ebab 96692cf cf1ebab 5b8b298 24c2c08 f302085 3eb99bc 24c2c08 3eb99bc cf1ebab 93a860a d28f7eb f302085 04f338f 8603187 1a6fb12 8603187 2fe1b55 8603187 9df6a7e 8603187 0b572d3 8603187 1a6fb12 04f338f 0b572d3 8603187 0b572d3 6be7d0b 146c098 785b76a 6be7d0b a88e1d8 6be7d0b 0a47069 146c098 e80a3bc ff9aa0c ec95760 146c098 ec95760 146c098 d57920f e80a3bc 0a47069 ef82d45 785b76a ef82d45 dde6bd9 785b76a dde6bd9 ef82d45 aabeb2d cf1ebab 785b76a ef82d45 ff9aa0c cf1ebab 785b76a cf1ebab 785b76a ef82d45 cf1ebab ff9aa0c cf1ebab 785b76a 9df6a7e cf1ebab 1a3c349 e80a3bc 785b76a cf1ebab 785b76a e80a3bc 64fce5c 96692cf 069b24e 3eb99bc cf1ebab 24c2c08 abe9cba 3eb99bc ba4c3a1 3eb99bc abe9cba ba4c3a1 abe9cba ba4c3a1 abe9cba ba4c3a1 abe9cba ba4c3a1 abe9cba ba4c3a1 abe9cba ba4c3a1 abe9cba ba4c3a1 24c2c08 abe9cba 3eb99bc d28f7eb abe9cba 24c2c08 dde6bd9 24c2c08 e80a3bc 24c2c08 e80a3bc f302085 cf1ebab f302085 71fb1a6 9df6a7e 71fb1a6 cf1ebab 9df6a7e cf1ebab d57920f 71fb1a6 e80a3bc 146c098 e80a3bc a147abc 798df17 e80a3bc 798df17 2fe1b55 798df17 2fe1b55 c08ff6f a147abc c08ff6f 2fe1b55 c08ff6f 2fe1b55 c08ff6f 2fe1b55 a147abc e80a3bc ec95760 a147abc ec95760 d57920f ec95760 2fe1b55 ec95760 2fe1b55 b5ff045 2fe1b55 a147abc 2fe1b55 ec95760 e80a3bc 6b7a4d5 9009e60 798df17 1a6fb12 798df17 8750468 2fe1b55 8750468 798df17 0b572d3 798df17 9009e60 033af6f 9009e60 033af6f 9009e60 033af6f 9009e60 033af6f 9009e60 033af6f 9009e60 033af6f 9009e60 033af6f 9009e60 033af6f 9009e60 033af6f 9009e60 033af6f 9009e60 033af6f 9009e60 033af6f 9009e60 033af6f 3b5daf4 146c098 3b5daf4 d57920f 146c098 d57920f 146c098 d57920f 146c098 d57920f 146c098 d57920f 146c098 |
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 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 |
# portfolio/npc_social_network/npc/npc_base.py
import random
import copy
import re
from typing import Optional, List
from datetime import datetime
from .npc_memory import Memory, MemoryStore
from .npc_emotion import EmotionManager
from .npc_behavior import BehaviorManager
from .emotion_config import (EMOTION_LIST, EMOTION_CATEGORY_MAP, EMOTION_DECAY_RATE
, PERSONALITY_TEMPLATE, EMOTION_RELATION_IMPACT
, POSITIVE_RELATION_EMOTIONS, NEGATIVE_RELATION_EMOTIONS
, COGNITIVE_RELATION_EMOTIONS)
from .personality_config import AGE_PROFILE, PERSONALITY_PROFILE
from .npc_relationship import RelationshipManager
from ..models.llm_helper import (query_llm_with_prompt, query_llm_for_emotion, summarize_memories
, analyze_gossip, classify_and_extract_plan_details
, evaluate_social_action_outcome
, generate_reflection_topic)
from .npc_memory_embedder import search_similar_memories, add_memory_to_index
from .npc_manager import get_korean_postposition
from .npc_planner import PlannerManager
from .. import simulation_core
from typing import TYPE_CHECKING, Tuple, List
if TYPE_CHECKING:
from .npc_manager import NPCManager
from ..manager.conversation_manager import Conversation
# Personality ๋ฐ์ดํฐ๋ฅผ ๊ด๋ฆฌํ๊ณ ์์ฝ ๊ธฐ๋ฅ์ ์ ๊ณตํ๋ ํด๋์ค ์ ์
class PersonalityManager:
"""
NPC์ ์ฑ๊ฒฉ ๋ฐ์ดํฐ๋ฅผ ๊ด๋ฆฌํ๊ณ ๊ด๋ จ ๊ธฐ๋ฅ์ ์ ๊ณตํ๋ ํด๋์ค
"""
def __init__(self, initial_traits=None):
self.traits = copy.deepcopy(initial_traits) if initial_traits else copy.deepcopy(PERSONALITY_TEMPLATE)
def get(self, key, default=None):
return self.traits.get(key, default)
def __getitem__(self, key):
return self.traits[key]
def __setitem__(self, key, value):
self.traits[key] = value
def keys(self):
return self.traits.keys()
def get_narrative_summary(self) -> str:
"""ํ์ฌ ์ฑ๊ฒฉ ์์น๋ฅผ ๋ฐํ์ผ๋ก ์์ ์ ์ธ ๋ฌ์ฌ๋ฅผ ๋์ ์ผ๋ก ์์ฑ"""
narratives = []
sensitive = self.traits.get("sensitive", 0.5)
stoic = self.traits.get("stoic", 0.5)
congitive_bias = self.traits.get("cognitive_bias", 0.5)
# ๊ฐ ์ฑ๊ฒฉ ์ถ์ ๋ํ ์์ ์ ๋ฌ์ฌ ์์ฑ
if sensitive > 0.7 and stoic < 0.3:
narratives.append("๊ฐ์์ฑ์ด ๋งค์ฐ ํ๋ถํ๊ณ ๊ฐ์ ์ ์ํฅ์ ์ฝ๊ฒ ๋ฐ๋ ํธ์
๋๋ค.")
elif stoic > 0.7 and sensitive < 0.3:
narratives.append("๊ฐ์ ์ ๊ฑฐ์ ๋๋ฌ๋ด์ง ์๋ ๊ณผ๋ฌตํ๊ณ ๋ฌด๋ํ ์ฑํฅ์
๋๋ค.")
else:
narratives.append("์ํฉ์ ๋ฐ๋ผ ๊ฐ์ ์ ํํํ ์ค ์๋ ๊ท ํ ์กํ ๋ชจ์ต์ ๋ณด์
๋๋ค.")
if congitive_bias > 0.7:
narratives.append("๋ํ๋ฅผ ํ ๋ ๋
ผ๋ฆฌ์ ์ด๊ณ ์ฌ์ค์ ๊ธฐ๋ฐํ์ฌ ํ๋จํ๋ ค๋ ๊ฒฝํฅ์ด ๊ฐํฉ๋๋ค.")
elif congitive_bias < 0.3:
narratives.append("์ง๊ด๊ณผ ๊ฐ์ ์ ๋ฐ๋ผ ์ํฉ์ ํ๋จํ๋ ๊ฒฝํฅ์ด ์์ต๋๋ค.")
if not narratives:
return "ํ๋ฒํ๊ณ ๊ท ํ ์กํ ์ฑ๊ฒฉ์
๋๋ค."
return "".join(narratives)
# NPC ํด๋์ค ์ ์
class NPC:
"""
NPC ํด๋์ค
NPC์ ๋ํ ๊ธฐ๋ณธ ์ ๋ณด๋ฅผ ๊ฐ์ฒดํ
"""
def __init__(self, name: str, korean_name: str, job: str,
personality: Optional[dict]=None):
self.name = name
self.korean_name = korean_name
self.job = job
self.manager: Optional['NPCManager'] = None
# npc ๊ธฐ์ต
self.memory_store = MemoryStore()
# ๊ฐ์ ์ํ ๋ฑ ๊ฐ์ ๊ด๋ฆฌ์ ์ด๊ธฐํ
self.personality = PersonalityManager(personality)
self.relationships = RelationshipManager(self)
self.reputation = {} # ๋ค๋ฅธ NPC์ ๋ํ ํํ ์ ์ฅ
self.emotion = EmotionManager(EMOTION_DECAY_RATE, self.personality)
self.knowledge: dict[str, dict] = {} # ๋ค๋ฅธ NPC์ ๋ํ ์ฌ์ค ์ ๋ณด๋ฅผ ์ ์ฅํ๋ '์ง์ ๋ฒ ์ด์ค'
# ํ๋ ๊ด๋ฆฌ์
self.behavior = BehaviorManager()
self.planner = PlannerManager(self)
# ๋ด๋ถ: float ๊ฐ์ ์์น ๊ด๋ฆฌ์ฉ ๋ฒํผ
self._emotion_buffer = self.emotion.get_buffer()
# baseline ๊ฐ์ ๊ฐ ๋ถ์ฌ
baseline_buffer = {}
for emo in self._emotion_buffer:
base_min = 0.05
base_max = 0.3
category = EMOTION_CATEGORY_MAP.get(emo, None)
bias_map = {
"core": "affect_bias", "social": "social_bias",
"cognitive": "cognitive_bias", "complex": "complex_bias"
}
bias = self.personality.get(bias_map.get(category, "sensitive"), 1.0)
baseline_value = base_min + (base_max - base_min) * bias
baseline_value *= self.personality.get("sensitive", 1.0) # ์ ์ฒด ๋ฏผ๊ฐ๋ ์ ์ฉ
baseline_buffer[emo] = round(baseline_value, 3)
# baseline buffer๋ฅผ EmotionManager์ ๋ฑ๋ก
self.emotion.set_baseline(baseline_buffer)
# emotion_buffer ์ด๊ธฐํ
self._emotion_buffer = self.emotion.get_buffer()
# ํ๋กํ ์ง์ โ ๊ธฐ๋ณธ์ "stable", ๋์ค์ NPC๋ง๋ค ๋ค๋ฅด๊ฒ ๋ถ์ฌ
profile_type = "stable"
profile = PERSONALITY_PROFILE[profile_type]
# Personality ๋ณํ ์๋ ๊ฐ๋ณ ์ค์
self.personality_change_rate = {
"sensitive": random.uniform(*profile["sensitive"]),
"stoic": random.uniform(*profile["stoic"]),
"cognitive_bias": random.uniform(*profile["cognitive_bias"]),
}
# Personality baseline ์ ์ฅ (์ด๊ธฐ๊ฐ ๋ณต์ฌ)
self.personality_baseline = copy.deepcopy(self.personality.traits)
# ๋์ด ์ถ๊ฐ (18 ~ 50์ธ ๊ธฐ๋ณธ ๋๋ค ์ด๊ธฐํ โ ๋์ค์ ์์ฑ ์ age ์ง์ ๊ฐ๋ฅ)
self.age = random.randint(18, 50)
# ์ด๊ธฐ personality_stage๋ update_personality_stage()์์ ์๋ ์ค์
self.personality_stage = None
self.update_personality_stage()
def generate_dialogue_turn(self, conversation: "Conversation", is_final_turn: bool=False) -> Tuple[str, str]:
"""๋ํ์ ํ์ฌ ํด์ ๋ํ ์๋ต๊ณผ ํ๋์ ์์ฑ (๊ธฐ์ต๊ณผ ๊ด๊ณ๋ฅผ ์ด ๋์)"""
from ..models.llm_helper import generate_dialogue_action
# 1. ์๊ฐํ๊ธฐ: '์ฒซ ๋ง๋'์ '๋๋ต'์ ๋ฐ๋ผ ๋ค๋ฅธ ์ ๋ณด๋ก ๊ธฐ์ต์ ํ์.
if not conversation.conversation_history: # ๋ํ ๊ธฐ๋ก์ด ์์ผ๋ฉด '์ฒซ ๋ง๋' ์ฐจ๋ก
query_text = conversation.topic
else: # ๋ํ ๊ธฐ๋ก์ด ์์ผ๋ฉด '๋ํ' ์ฐจ๋ก
query_text = "\n".join(conversation.conversation_history[-2:]) # ์ต๊ทผ 2ํด์ ๋ํ๋ก ๊ธฐ์ต ํ์
_, _, relevant_memories = search_similar_memories(self, query_text, top_k=3)
# 2. ๋์ฌ ์์ฑ: ๋ชจ๋ ์ ๋ณด๋ฅผ ์ข
ํฉํ์ฌ llm_helper์ ์์ฒญ
result = generate_dialogue_action(
speaker = self,
target = conversation.participants[1-conversation.turn_index],
conversation=conversation,
memories = relevant_memories,
is_final_turn = is_final_turn
)
dialogue = result.get("dialogue", "...")
action = result.get("action", "END").strip().upper()
# ํ๋ํ๊ธฐ (์ ๊ธ๊ณผ ํจ๊ป)
with simulation_core.simulation_lock:
emotion = query_llm_for_emotion(dialogue)
if emotion and emotion in EMOTION_LIST:
self.update_emotion(emotion, 1.0)
return dialogue, action
def remember(self, content: str, importance: int = 5, emotion: str = None,
strength: float = 1.0, memory_type:str = "Event",
context_tags: Optional[List[str]]=None) -> Memory:
"""
NPC๊ฐ ์๋ก์ด ๊ธฐ์ต์ ์ ์ฅํ๊ณ ๊ฐ์ ์ํ์ ๋ฐ์
"""
memory = Memory(
content=content,
importance = importance,
emotion=emotion or "neutral",
memory_type=memory_type,
context_tags=context_tags
)
self.memory_store.add_memory(memory)
# ์ค์๋๊ฐ 3 ์ด์์ธ ๊ธฐ์ต๋ง ๋ฒกํฐ ์ธ๋ฑ์ค์ ์ถ๊ฐํ์ฌ ํจ์จ์ฑ ํ๋ณด
if memory.importance >= 3:
add_memory_to_index(self, memory)
if emotion and emotion in EMOTION_LIST:
self.update_emotion(emotion, strength) # strength = importance / 5.0์ผ๋ก๋ ๊ฐ๋ฅ
# ๋ง์ฝ ๊ธฐ์ต์ด '์๋ฌธ'์ด๋ผ๋ฉด, ํํ์ ์
๋ฐ์ดํธ
if memory_type == "Gossip":
# (๊ฐ๋จํ ์์: LLM์ผ๋ก ์๋ฌธ์ ๋์๊ณผ ๊ธ/๋ถ์ ์ ํ์
ํด์ผ ๋ ์ ํํจ)
# ๊ธ์ ์๋ฌธ ์์: "๋ฐฅ์ด ์ฐฐ๋ฆฌ๋ฅผ ๋์๋ค"
# ๋ถ์ ์๋ฌธ ์์: "์๋ฆฐ์ด ์จ๋ฆฌ์ค์ ๋คํ๋ค"
self.process_gossip_and_update_reputation(memory)
return memory # ์์ฑ๋ ๊ธฐ์ต ๊ฐ์ฒด ๋ฐํ
def update_emotion(self, emotion:str, strength: float = 1.0):
"""
ํน์ ๊ฐ์ ์ ๊ฐ์ ์ํ์ ๋ฐ์
"""
self.emotion.update_emotion(emotion, strength)
self._emotion_buffer = self.emotion.get_buffer()
def decay_emotions(self):
"""
์๊ฐ์ด ์ง๋จ์ ๋ฐ๋ผ ๋ชจ๋ ๊ฐ์ ์ด ์์ํ ๊ฐ์
"""
self.emotion.decay_emotions()
self._emotion_buffer = self.emotion.get_buffer()
def recall_all_memories(self) -> List[str]:
"""
์ ์ฅ๋ ๋ชจ๋ ๊ธฐ์ต(๋จ๊ธฐ + ์ฅ๊ธฐ)์ ๋ฆฌ์คํธ๋ก ๋ฐํ
"""
return [m.content for m in self.memory_store.get_all_memories()]
def get_composite_emotion_state(self, top_n: int = 3) -> List[tuple]:
"""
ํ์ฌ ๊ฐ์ ์ํ ์ค ์์ N๊ฐ๋ฅผ ๋ฐํ
"""
buffer = self.emotion.get_buffer() # EmotionManager์์ ์ต์ ๊ฐ์ ์ํ ๋ณต์ฌ๋ณธ ๊ฐ์ ธ์ค๊ธฐ
sorted_emotions = sorted(buffer.items(), key=lambda x: x[1], reverse=True)
composite = [(emo,round(score, 2)) for emo, score in sorted_emotions[:top_n] if score > 0.1]
return composite # ์ [('fear', 2.0), ('anger'), 1.5]
def summarize_emotional_state(self) -> str:
"""
๊ฐ์ ์ํ ์์ฝ ํ
์คํธ ์์ฑ
"""
composite = self.get_composite_emotion_state()
dominant = self.emotion.get_dominant_emotion()
if not dominant:
return "ํ์ฌ ํน๋ณํ ๊ฐ์ ์์ด ํ์จํ ์ํ์
๋๋ค."
return f"ํ์ฌ {dominant} ๋ฑ์ ๊ฐ์ ์ ๋๋ผ๊ณ ์์ต๋๋ค.\n(์ฃผ์ ๊ฐ์ : {composite if composite else '์์'})"
def get_relationship_description(self, target_name: str) -> str:
"""
ํน์ ๋์๊ณผ์ ๊ด๊ณ๋ฅผ ์์ฐ์ด๋ก ๋ฐํ
"""
return self.relationships.get_relationship_summary(target_name)
def get_top_emotions(self, top_n=6):
"""
ํ์ฌ NPC ๊ฐ์ ์ํ ์์ N๊ฐ ๋ฐํ
"""
return self.emotion.get_top_emotions(top_n=top_n)
def update_personality(self):
"""
๊ฒฝํ์ ๋ฐ๋ผ ์ฑ๊ฒฉ์ ์ ์ง์ ์ผ๋ก ๋ณํ์ํด
- MemoryType ๋ณ ๊ฐ์ค์น + EmotionInfluence ๋ณ ๊ฐ์ค์น ๋ชจ๋ ๋ฐ์
"""
recent_memories = [m for m in self.memory_store.get_recent_memories(limit=10) if m.importance >= 5]
if not recent_memories:
return
total_influence = {"sensitive": 0.0, "stoic": 0.0, "cognitive_bias": 0.0}
# Personality ๋ณํ ๋ฐ์
for mem in recent_memories:
if not mem.emotion or mem.emotion not in EMOTION_RELATION_IMPACT: continue
# ๊ฐ ๊ฐ์ ์ ๊ณ ์ ํ ์ํฅ๋ ฅ(impact)์ ๊ฐ์ ธ์ด
emotion_impact = EMOTION_RELATION_IMPACT.get(mem.emotion, 0.0)
# 1. ๊ฐ์ ์ ๊ธ์ /๋ถ์ /์ธ์ง์ ์์ฑ์ ๋ฐ๋ฅธ ์ํฅ ๋ฐฉํฅ ๊ฒฐ์
influence_vector = {"sensitive": 0.0, "stoic": 0.0, "cognitive_bias": 0.0}
# ๊ฐ์ ์ํฅ๋ ฅ์ด ์์์ด๋ฉด(๊ธ์ ์ ๊ฐ์ ), ๋ฏผ๊ฐ๋์ ๋ด์ฑ์ ๋ฎ์ถค
if mem.emotion in POSITIVE_RELATION_EMOTIONS:
influence_vector["sensitive"] -= abs(emotion_impact) / 2.0 # ๊ธ์ ์ ๊ฒฝํ์ ๋ฏผ๊ฐ๋๋ฅผ ๋ฎ์ถค
influence_vector["stoic"] -= abs(emotion_impact) / 4.0 # ๊ธ์ ์ ์ธ ๊ฒฝํ์ ๋ด์ฑ์ ์ผ ํ์๋ฅผ ์ค์
# ๊ฐ์ ์ํฅ๋ ฅ์ด ์์์ด๋ฉด(๋ถ์ ์ ๊ฐ์ ), ๋ฏผ๊ฐ๋์ ๋ด์ฑ์ ๋์
elif mem.emotion in NEGATIVE_RELATION_EMOTIONS:
influence_vector["sensitive"] += abs(emotion_impact) / 2.0 # ๋ถ์ ์ ๊ฒฝํ์ ๋ฏผ๊ฐ๋๋ฅผ ๋์
influence_vector["stoic"] += abs(emotion_impact) / 4.0 # ๋ถ์ ์ ๊ฒฝํ์ ๋ด์ฑ์ ์ผ๋ก ๋ง๋ฆ
# ์ธ์ง์ ๊ฐ์ ์ cognitive_bias์ ์ํฅ
if mem.emotion in COGNITIVE_RELATION_EMOTIONS:
influence_vector["cognitive_bias"] += 0.4 # ์ธ์ง์ ๊ฒฝํ์ ๋
ผ๋ฆฌ์ ์ฌ๊ณ ๋ฅผ ๊ฐํ
# 2. ๋ณํ ๊ฐ๋ ๊ณ์ฐ (๊ธฐ์ต์ ์ค์๋์ ๋์ด์ ๋ฐ๋ผ)
age_factor = AGE_PROFILE.get(self.personality_stage, 1.0)
change_strength = (mem.importance / 10.0) * age_factor
# 3. ์ด๋ฒ ๊ธฐ์ต์ผ๋ก ์ธํ ์ต์ข
๋ณํ๋ ๋์
for trait in total_influence.keys():
total_influence[trait] += influence_vector[trait] * change_strength
# 4. ๋์ ๋ ๋ณํ๋์ ์ฑ๊ฒฉ์ ์ ์ฉ
for trait, total_change in total_influence.items():
if abs(total_change) > 0.001: # ์๋ฏธ์๋ ๋ณํ๋ง ์ ์ฉ
rate = self.personality_change_rate.get(trait, 0.003)
delta = total_change * rate
self.personality[trait] = max(0.0, min(1.0, self.personality[trait] + delta))
# 5. ๊ธฐ์ค์ ์ผ๋ก ํ๊ท (ํญ์์ฑ)
base_decay = 0.001
for trait in self.personality.keys():
decay_rate = base_decay * (1.0 + self.personality.get("stoic", 0.0) - self.personality.get("sensitive", 0.0))
decay_rate = max(0.0005, min(decay_rate, 0.005))
self.personality[trait] += (self.personality_baseline[trait] - self.personality[trait]) * decay_rate
print(f"[Personality Update] {self.korean_name} โ sensitive: {self.personality['sensitive']:.3f}, "
f"stoic: {self.personality['stoic']:.3f}, cognitive_bias: {self.personality['cognitive_bias']:.3f}", flush=True)
def decay_memories(self):
"""
์๊ฐ์ด ์ง๋จ์ ๋ฐ๋ผ ๊ธฐ์ต importance ๊ฐ์
- MemoryStore์ decay_memories์ self(NPC ๊ฐ์ฒด)๋ฅผ ์ ๋ฌ
"""
self.memory_store.decay_memories(npc=self)
def update_personality_stage(self):
"""
ํ์ฌ ๋์ด์ ๋ฐ๋ผ personality_stage ์๋ ์
๋ฐ์ดํธ
"""
if self.age < 3:
self.personality_stage = "infancy_and_toddlerhood"
elif self.age < 6:
self.personality_stage = "early_childhood"
elif self.age < 12:
self.personality_stage = "middle_childhood"
elif self.age < 18:
self.personality_stage = "adolescence"
elif self.age < 30:
self.personality_stage = "young_adulthood"
elif self.age < 65:
self.personality_stage = "middle_adulthood"
else:
self.personality_stage = "older_adulthood"
def get_memory_importance_scaling(self, memory_age_in_days: float) -> float:
"""
NPC์ ๋์ด์ ๋ฐ๋ผ memory_age_in_days ๊ธฐ๋ฐ scaling factor ๋ฐํ
- ์ ์ NPC โ ์ต๊ทผ ๊ธฐ์ต ๋ ๊ฐ์กฐ
- ๋
ธ๋
NPC โ ์ค๋๋ ๊ธฐ์ต ๋ ๊ฐ์กฐ
"""
if self.personality_stage in [
"infancy_and_toddlerhood", "early_childhood", "middle_childhood",
"adolescence", "young_adulthood"
]:
# ์ต๊ทผ ๊ธฐ์ต ๊ฐ์กฐ โ ์ค๋๋ ๊ธฐ์ต ์ํฅ ์ฝํ
return max(0.5, 1.5 - 0.05 * memory_age_in_days) # ์ต๋ 1.5, ์ ์ ๊ฐ์
elif self.personality_stage in [
"middle_adulthood", "older_adulthood"
]:
return min(1.5, 0.5 + 0.05 * memory_age_in_days) # ์ต์ 0.5, ์ ์ ์ฆ๊ฐ
else:
# fallback โ ์ ์ํ
return 1.0
def summarize_and_store_memories(self, memories: List[Memory]):
"""
๊ธฐ์ต ๋ฆฌ์คํธ๋ฅผ ์์ฝํ๊ณ , NPC ์ฅ๊ธฐ ๊ธฐ์ต์ผ๋ก ์ ์ฅ
"""
if not memories: return
summary = summarize_memories(memories)
if "[LLM Error]" in summary: return
self.remember(
content=f"[์์ฝ๋ ๊ธฐ์ต] {summary}",
importance=9,
emotion=self.emotion.get_dominant_emotion(),
memory_type="Summary"
)
def update_autonomous_behavior(self, time_context: str):
"""
NPC๊ฐ ์ค์ค๋ก ํ๋จํ์ฌ ํ๋์ ๊ฒฐ์ ํ๋ ์์จ ํ๋ ๋ก์ง
์ฃผ๊ธฐ์ ์ผ๋ก ํธ์ถ๋์ด ๋ชฉํ ์ค์ , ๊ณํ ์คํ ๋ฑ์ ๋ด๋น.
"""
# ํ๋ ์ด์ด๊ฐ ๋นํ์ฑํ ์ํ์ผ ๊ฒฝ์ฐ, ์์จ ํ๋์ ์คํํ์ง ์๋๋ก ํ๋ ์์ ์ฅ์น
if self.name == "player" and not self.manager.player_is_active:
return
# 1. ๊ณํ ์คํ ๋ก์ง์ NPC๊ฐ ์ง์ ์ฒ๋ฆฌ
if self.planner.has_active_plan():
step = self.planner.get_current_step()
if not step:
self.planner.clear_plan()
return
print(f"[{self.korean_name}์ ํ๋ ๊ณํ ์คํ] {self.planner.current_plan.current_step_index + 1}/{len(self.planner.current_plan.steps)}: {step}")
# LLM์ผ๋ก ๊ณํ์ ๋ถ์ํ์ฌ ํ๋์ ๊ฒฐ์
plan_details = classify_and_extract_plan_details(step)
action_type = plan_details.get("action_type")
target_korean_name = plan_details.get("target")
# ํ๋ ์คํ ์ '์กฐ๊ฑด ํ์ธ'๋จ๊ณ
can_execute = True
if target_korean_name == "ํ๋ ์ด์ด" and not self.manager.player_is_active:
# ๋ชฉํ ๋์์ด 'ํ๋ ์ด์ด'์ธ๋ฐ, ํ๋ ์ด์ด๊ฐ ๋นํ์ฑํ ์ํ์ผ ๊ฒฝ์ฐ
can_execute = False
simulation_core.add_log(f"[{self.korean_name}์ ์๊ฐ] ์ง๊ธ์ ํ๋ ์ด์ด๊ฐ ์์ผ๋, '{step}' ๊ณํ์ ๋์ค์ ๋ค์ ์๋ํด์ผ๊ฒ ๋ค.")
return
# ํ๋ ์คํ
if action_type == "TALK" and target_korean_name:
target_npc = self.manager.get_npc_by_korean_name(target_korean_name)
if target_npc and target_npc != self:
# ์ฃผ์ ๊ฐ ์๋ ๋ํ ์์
simulation_core.conversation_manager.start_conversation(self, target_npc, topic=plan_details.get("topic"))
elif action_type == "HELP" and target_korean_name:
target_npc = self.manager.get_npc_by_korean_name(target_korean_name)
target_postposition = get_korean_postposition(target_korean_name, "์ด", "๊ฐ")
target_postposition_Eul = get_korean_postposition(target_korean_name, "์", "๋ฅผ")
if target_npc and target_npc != self:
# '๋๊ธฐ' ํ๋์ ๊ฒฐ๊ณผ๋ฅผ ์๋ฎฌ๋ ์ด์
outcome = evaluate_social_action_outcome(self, target_npc, "HELP")
summary = outcome.get('outcome_summary')
# ๊ฒฐ๊ณผ๋ฅผ ๋ฐํ์ผ๋ก ์์ชฝ ๋ชจ๋์ ์ํ๋ฅผ ์
๋ฐ์ดํธ
with simulation_core.simulation_lock:
# ํ๋์(๋)์ ๊ธฐ์ต๊ณผ ๊ด๊ณ ์
๋ฐ์ดํธ
self.remember(content=f"{target_korean_name}{target_postposition_Eul} ๋์๋ค. ({summary})", importance=8, emotion=outcome.get('initiator_emotion'))
self.relationships.update_relationship(target_npc.name, outcome.get('initiator_emotion'), strength=3.0)
# ๋์์(์๋๋ฐฉ)์ ๊ธฐ์ต๊ณผ ๊ด๊ณ ์
๋ฐ์ดํธ
target_npc.remember(content=f"{self.korean_name}{target_postposition} ๋๋ฅผ ๋์์ฃผ์๋ค. ({summary})")
target_npc.relationships.update_relationship(self.name, outcome.get('target_emotion'), strength=3.0)
else: # SOLOT_ACTION ๋๋ ๊ธฐํ
# ๋ํ๊ฐ ์๋ ๋ค๋ฅธ ํ๋์ ๊ทธ๋๋ก ๊ธฐ์ต์๋ง ๊ธฐ๋ก
self.remember(
content=f"'{self.planner.current_goal.description}' ๋ชฉํ๋ฅผ ์ํด ํผ์ ํ๋ ํ๋ค: {step}",
importance = 6,
emotion="engagement",
memory_type="Behavior"
)
# ํ์ฌ ๋จ๊ณ ์๋ฃ ์ฒ๋ฆฌ
self.planner.complete_step()
# 2. ๊ณํ์ด ์์ผ๋ฉด ์๋ก์ด ๋ชฉํ๋ฅผ ์์ฑํ๋๋ก ์๋
else:
# ์๋ฌธ์ ํผ๋จ๋ฆฌ๋ ค๋ ์๋
gossip_memories = [m for m in self.memory_store.get_all_memories() if m.memory_type == "Gossip" and not m.is_shared]
if gossip_memories and random.random() < 0.2: # 20% ํ๋ฅ ๋ก ์๋ฌธ ์ ํ ์๋
gossip_to_spread = random.choice(gossip_memories)
# ์์ง ๋ํํ ์ ์๋ NPC๋ฅผ ํ๊ฒ์ผ๋ก ์ ์
potential_targets = [npc for npc in self.manager.get_interactive_npcs() if npc.name != self.name]
if potential_targets:
target_npc = random.choice(potential_targets)
# LLM์ ํ์ฉํ์ฌ ์๋ฌธ์ ํผ๋จ๋ฆฌ๋ ๋ํ ์์ฑ
gossip_dialogue = self.generate_dialogue(
user_input=f"'{target_npc.korean_name}'์๊ฒ '{gossip_to_spread.content}'์ ๋ํด ๋์ง์ ์ด์ผ๊ธฐํ๋ค.",
time_context=time_context,
target_npc=target_npc,
create_memory=False
)
if gossip_dialogue and "[LLM Error]" not in gossip_dialogue:
simulation_core.add_log(f"[์๋ฌธ ์ ํ] {self.korean_name} -> {target_npc.korean_name}: {gossip_dialogue}")
gossip_to_spread.is_shared = True
# 2. '์๋ฌธ ์ ํ'๋ผ๋ ํ๋์ ๋ํ ๋ช
ํํ ๊ธฐ์ต์ ๋ณ๋๋ก ์์ฑ
self.remember(
content=f"{target_npc.korean_name}์๊ฒ '{gossip_to_spread.content}'๋ผ๋ ์๋ฌธ์ ๋ํด ์ด์ผ๊ธฐํ๋ค.",
importance=5,
emotion="engagement", # '๋ดํฉ'๊ณผ ๊ฐ์ ์ฌํ์ ํ๋์ ๋ํ ๊ฐ์
memory_type="Gossip_Spreading" # '์๋ฌธ ์ ํ'๋ผ๋ ๋ณ๋์ ํ์
์ง์
)
# ์ผ์ ํ๋ฅ ๋ก ์๋ก์ด ๋ชฉํ๋ฅผ ์์ฑ (๋งค๋ฒ ์์ฑํ์ง ์๋๋ก)
if random.random() < 0.05: # 5% ํ๋ฅ
self.planner.generate_goal()
elif random.random() < 0.1: # 10% ํ๋ฅ ๋ก ๋ค๋ฅธ NPC์๊ฒ ๊ฐ๋ฒผ์ด ์ธ์ฌ ์๋
potential_targets = [npc for npc in self.manager.get_interactive_npcs() if npc != self]
if potential_targets:
weights = []
# ๊ฐ์ค์น ๊ณ์ฐ ๋ก์ง
for target in potential_targets:
# ๊ด๊ณ ์ ์๋ฅผ -100 ~ 100์์ 0~2 ๋ฒ์๋ก ๋ณํํ์ฌ ๊ธฐ๋ณธ ๊ฐ์ค์น 1์ ๋ํจ
# score 100 -> weight 6 / score 0 -> weight 1 / score -100 -> weight 0.1 (์ต์ ํ๋ฅ )
score = self.relationships.get_relationship_score(target.name)
weight = 1 + (score / 20.0)
weights.append(max(0.1, weight)) # ๊ฐ์ค์น๊ฐ 0์ด ๋์ง ์๋๋ก ์ต์๊ฐ ๋ณด์ฅ
# ๊ฐ์ค์น์ ๋ฐ๋ผ ๋ํ ์๋ 1๋ช
์ ํ
target_npc = random.choices(potential_targets, weights=weights, k=1)[0]
# ์ฃผ์ ๊ฐ ์๋ ๊ฐ๋ฒผ์ด ๋ํ ์์
simulation_core.conversation_manager.start_conversation(self, target_npc)
def create_symbolic_memory(self):
"""
์ต๊ทผ ๊ฒฝํ๊ณผ ์ฑ๊ฒฉ์ ๋ฐํ์ผ๋ก ๋์ ์ผ๋ก ์ฑ์ฐฐ ์ฃผ์ ๋ฅผ ์ ํ๊ณ , ๊ฐ์น๊ด์ ํ์ฑ
"""
simulation_core.add_log(f"[{self.korean_name}: ๊น์ ์๊ฐ์ ์ ๊น๋๋ค...]")
# 1. ์ต๊ทผ ๊ธฐ์ต 10๊ฐ๋ฅผ ๊ฐ์ ธ์ต๋๋ค.
recent_memories = self.memory_store.get_all_memories()[-10:]
if len(recent_memories) < 8: # ์ถฉ๋ถํ ๊ธฐ์ต์ด ์์ฟ์ ๋๋ง ์์ฑ
return
# 2. ์ฑ๊ฒฉ ์ ๋ณด๋ฅผ ํจ๊ป ์ ๋ฌํ์ฌ ์ฑ์ฐฐ ์ฃผ์ ์์ฑ
reflection_topic = generate_reflection_topic(
recent_memories,
self.personality.get_narrative_summary()
)
simulation_core.add_log(f"[{self.korean_name}์ ์ฑ์ฐฐ ์ฃผ์ ] {reflection_topic}")
# 3. ์์ฑ๋ ์ฃผ์ ์ ๊ฐ์ฅ ๊ด๋ จ ์๋ ๊ธฐ์ต๋ค์ ๋ค์ ๊ฒ์ํฉ๋๋ค.
_, _, relevant_memories = search_similar_memories(self, reflection_topic, top_k=10)
if not relevant_memories:
relevant_memories = recent_memories # ๊ด๋ จ ๊ธฐ์ต์ด ์์ผ๋ฉด ์ต๊ทผ ๊ธฐ์ต์ ๊ทธ๋๋ก ์ฌ์ฉ
# 4. ๊ด๋ จ ๊ธฐ์ต๋ค์ ์์ฝ
memory_summary = summarize_memories(relevant_memories)
# 5. ์์ฝ๋ ๊ธฐ์ต๊ณผ ์ฑ์ฐฐ ์ฃผ์ ๋ฅผ ๋ฐํ์ผ๋ก ์ต์ข
๊นจ๋ฌ์์ ์ป์ต๋๋ค.
prompt = f"""
# Instructions
- ๋น์ ์ '{self.korean_name}'์
๋๋ค.
- ์๋์ ์ ๋ณด๋ค์ ๋ฐํ์ผ๋ก, ๋น์ ์ ์ธ์์ ๋ํ ๊น์ ๊นจ๋ฌ์์ด๋ ์ ๋
์ ํ ๋ฌธ์ฅ์ '์์ง์ ์ธ ๊ธฐ์ต'์ผ๋ก ๋ง๋ค์ด์ฃผ์ธ์.
# Reflection Topic
"{reflection_topic}"
# Summary of Related Memories
{memory_summary}
# My Realization (a one-sentence lesson or core belief):
"""
symbolic_memory_content = query_llm_with_prompt(prompt).strip()
if symbolic_memory_content and "[LLM Error]" not in symbolic_memory_content:
self.remember(
content=symbolic_memory_content,
importance=10,
memory_type="Symbolic",
emotion="realization" # ๊นจ๋ฌ์์ด๋ผ๋ ์๋ก์ด ๊ฐ์
)
simulation_core.add_log(f"[{self.korean_name}์ ์๋ก์ด ๊นจ๋ฌ์] {symbolic_memory_content}")
def update_reputation(self, target_name: str, score_change: int):
"""ํน์ ๋์์ ๋ํ ํํ ์ ์๋ฅผ ๋ณ๊ฒฝ"""
if self.name == target_name: return # ์๊ธฐ ์์ ์ ๋ํ ํํx (์๊ธฐ ์์ ์ ํํ์ ๋ํ ๋ด์ฉ๋ ์ฑ๊ฒฉ ํ์ฑ์ ์ค์ํ ์ ์์ง ์๋?)
current_score = self.reputation.get(target_name, 0)
self.reputation[target_name] = current_score + score_change
target_npc = self.manager.get_npc_by_name(target_name)
target_korean_name = target_npc.korean_name if target_npc else target_name
print(f"[{self.korean_name}]์ {target_korean_name}์ ๋ํ ํํ: {self.reputation[target_name]} ({score_change:+.0f})")
def process_gossip_and_update_reputation(self, gossip_memory: Memory):
"""LLM์ ์ด์ฉํด ์๋ฌธ์ ๋ถ์ํ๊ณ ๊ด๋ จ ์ธ๋ฌผ๋ค์ ํํ์ ์
๋ฐ์ดํธ"""
analysis = analyze_gossip(gossip_memory.content)
if not analysis:
return
person1_korean_name = analysis.get("person1")
person2_korean_name = analysis.get("person2")
sentiment = analysis.get("sentiment")
if not all([person1_korean_name, person2_korean_name, sentiment]):
return
score_change = 0
if sentiment == "positive":
score_change = 2
elif sentiment == "negative":
score_change = -2
if score_change != 0:
# LLM์ด ๋ฐํํ ํ๊ธ ์ด๋ฆ์ผ๋ก NPC ๊ฐ์ฒด๋ฅผ ์ฐพ์ ์์ด ID๋ฅผ ๊ฐ์ ธ์จ๋ค.
p1_npc = self.manager.get_npc_by_korean_name(person1_korean_name)
p2_npc = self.manager.get_npc_by_korean_name(person2_korean_name)
# ์๋ฌธ์ ๋์์ด ๋ ๋ ์ฌ๋์ ๋ํ ๋์ ํํ์ ๋ณ๊ฒฝ
if p1_npc:
self.update_reputation(p1_npc.name, score_change)
if p2_npc:
self.update_reputation(p2_npc.name, score_change) |