-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathclasses.py
1003 lines (805 loc) · 34.1 KB
/
classes.py
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
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import asyncio
from dataclasses import dataclass, field
from yamldataclassconfig.config import YamlDataClassConfig
from pathlib import Path
import numpy as np
from utils import (
get_last_modified_file,
get_oldest_modified_file,
bcolors,
int_to_month,
Input,
save_yaml_from_data,
request_openai,
load_yaml_to_dataclass,
to_datetime,
from_datetime,
hour_to_iso,
minute_to_iso,
second_to_iso,
populate_dataclass_with_dicts,
yaml_from_str,
YamlDumperDoubleQuotes,
dataclass_to_dict_copy,
load_yaml,
get_previous_to_last_modified_file,
is_year_leap,
check_yaml_update_npc,
check_yaml_new_npc,
hour_to_daytime,
base64str_to_img,
batch_image_generation,
batch_completion,
# debug,
)
import validators
from prompt_toolkit import prompt
from prompts import (
create_npc_request,
create_global_goals,
create_social_connections,
world_new_state,
npc_new_state,
generate_npc_image
)
from typing import List, Union, Any
import yaml
import openai
import random
from resources_paths import DATA_PATH, GAMES_PATH, YAML_TEMPLATES_PATH, INIT_WORLDS_PATH
import sys
import os
from logging import debug
@dataclass
class Settings(YamlDataClassConfig):
LLM_model: str = ""
text_to_image_model: str = ""
text_to_image_size: str = ""
text_to_image_quality: str = ""
text_to_image_n: int = 1
text_to_image_generate_world: bool = False
text_to_image_generate_npcs: bool = False
openai_verbose: bool = False
llm_request_tries_num: int = -1
npc_history_steps: int = 0
npc_attributes_names: list[str] = field(default_factory=list)
max_attribute_delta: int = 0
npc_num_global_goals: int = 0
max_npc_social_connections: int = 0
number_of_npcs: int = 0
world_attributes_names: list[str] = field(default_factory=list)
world_time_names: list[str] = field(default_factory=list)
world_history_steps: int = 0
@dataclass
class World(YamlDataClassConfig):
name: str = "" # name of the world
attributes: dict[str, Any] = field(default_factory=dict) # attributes of the world
time: dict[str, Any] = field(default_factory=dict) # time of the world
tick_type: str = "" # years, months, days, hours, minutes, seconds, etc
tick_rate: int = 0 # how much time of tick_type passes in the world per tick
current_tick: int = 0 # indicates how many ticks passed
current_state_prompt: str = ""
@dataclass
class Npc(YamlDataClassConfig):
name: str = ""
global_goal: str = ""
attributes: dict[str, Any] = field(default_factory=dict)
social_connections: list[str] = field(default_factory=list)
current_state_prompt: str = ""
class Game:
def __init__(self):
# Settings
self.settings: Settings = Settings()
self.settings.load(path="./settings.yaml")
# Paths
self.game_path: Path = ""
self.cur_world_path: Path = ""
self.cur_npcs_path: Path = ""
self.cur_global_goals_path: Path = ""
# Game
self.game_name: str = ""
self.existing_games = [x.name for x in GAMES_PATH.iterdir() if x.is_dir()]
self.existing_init_worlds = [
x.name for x in INIT_WORLDS_PATH.iterdir() if x.is_file()
]
# World
self.cur_world: World = World()
self.world_prompt: str = ""
self.world_general_description: str = ""
# Npcs
self.npcs: List[Npc] = []
self.global_goals: list = []
def input_handler(self, user_input: Input):
if user_input == Input.init_game:
self.init_game()
elif user_input == Input.init_world:
self.init_world()
elif user_input == Input.progress_world:
self.progress_world()
elif user_input == Input.info_world:
self.print_info_world()
elif user_input == Input.info_npcs:
self.print_info_npcs()
elif user_input == Input.info_everything:
self.print_info_all()
elif user_input == Input.quit_game:
self.quit_game()
return
def prompt_next_action(self):
next_action = prompt(
"What would you like to do next? ",
bottom_toolbar=f"(W)ait {self.cur_world.tick_rate} {self.cur_world.tick_type}s/(A)ll info/World (i)nfo/(N)PCs info/(Q)uit",
validator=validators.NotInListValidator(
["W", "w", "A", "a", "I", "i", "N", "n", "Q", "q"]
),
validate_while_typing=True,
)
if next_action in ["W", "w"]:
next_action = Input.progress_world
elif next_action in ["A", "a"]:
next_action = Input.info_everything
elif next_action in ["I", "i"]:
next_action = Input.info_world
elif next_action in ["N", "n"]:
next_action = Input.info_npcs
elif next_action in ["Q", "q"]:
next_action = Input.quit_game
self.input_handler(next_action)
return
async def progress_world(self):
await self.tick_increment()
await self.update_world()
await self.update_npcs()
if self.settings.text_to_image_model:
await self.generate_images()
self.save_world()
self.save_npcs()
return self
async def update_world(self):
world_new_state_request = world_new_state.format(
init_state=self.world_general_description,
previous_state=get_previous_to_last_modified_file(self.cur_world_path),
current_state=self.cur_world.current_state_prompt,
attributes=self.cur_world.attributes,
date=self.current_date_to_str(),
tick_rate=self.cur_world.tick_rate,
tick_type=self.cur_world.tick_type,
)
debug(world_new_state_request)
new_world_state = await request_openai(
model=self.settings.LLM_model,
prompt=world_new_state_request,
tries_num=self.settings.llm_request_tries_num,
response_processors=[yaml_from_str],
verbose=self.settings.openai_verbose,
api_key=os.environ.get("OPENAI_API_KEY"),
)
self.cur_world.current_state_prompt = new_world_state["world_new_state"]
for attribute_key in self.cur_world.attributes.keys():
new_attribute_value = new_world_state["attributes"].get(attribute_key, None)
if new_attribute_value:
self.cur_world.attributes[attribute_key] = new_attribute_value
return
async def update_npcs(self):
update_npc_prompts = []
for npc in self.npcs:
update_npc_prompts.append(self.get_update_npc_prompt(npc))
openai_kwargs = {
"model": self.settings.LLM_model,
"tries_num": self.settings.llm_request_tries_num,
"response_processors": [yaml_from_str, check_yaml_update_npc],
"verbose": self.settings.openai_verbose,
"api_key": os.environ.get("OPENAI_API_KEY"),
}
npcs_new_data = await batch_completion(update_npc_prompts, openai_kwargs=openai_kwargs)
for npc, npc_new_data in zip(self.npcs, npcs_new_data):
npc.current_state_prompt = npc_new_data["npc_new_state"]
for attribute_key in npc.attributes.keys():
new_attribute_value = npc_new_data["attributes"].get(attribute_key, 0)
npc.attributes[attribute_key] += new_attribute_value
return
def get_update_npc_prompt(self, current_npc: Npc):
keys_to_delete = [
key
for key in self.npcs[0].__dict__.keys()
if key not in ["name", "current_state_prompt"]
]
other_npcs = [
dataclass_to_dict_copy(npc, keys_to_delete)
for npc in self.npcs
if npc != current_npc and npc.name in current_npc.social_connections
]
npc_new_state_request = npc_new_state.format(
world_general_description=self.world_general_description,
world_current_state=self.cur_world.current_state_prompt,
world_attributes=self.cur_world.attributes,
date=self.current_date_to_str(),
current_npc=yaml.dump(
current_npc, sort_keys=False, Dumper=YamlDumperDoubleQuotes
),
other_npcs=other_npcs,
tick_rate=self.cur_world.tick_rate,
tick_type=self.cur_world.tick_type,
max_attribute_delta=self.settings.max_attribute_delta,
)
return npc_new_state_request
async def tick_increment(self):
self.cur_time = to_datetime(
self.cur_world.time["current_year"],
self.cur_world.time["current_month"],
self.cur_world.time["current_day"],
self.cur_world.time["current_hour"],
self.cur_world.time["current_minute"],
self.cur_world.time["current_second"],
)
self.cur_world.current_tick += 1
if self.cur_world.tick_type in ("day", "year"):
timeformat = self.cur_world.tick_type[0].capitalize()
else:
timeformat = self.cur_world.tick_type[0]
if not self.cur_world.tick_type == "year":
time_delta = np.timedelta64(self.cur_world.tick_rate, timeformat)
else:
if is_year_leap(self.cur_time.astype("datetime64[Y]").astype(int)):
days = 366
else:
days = 365
time_delta = np.timedelta64(self.cur_world.tick_rate * days, "D")
new_time = self.cur_time + time_delta
(
self.cur_world.time["current_year"],
self.cur_world.time["current_month"],
self.cur_world.time["current_day"],
self.cur_world.time["current_hour"],
self.cur_world.time["current_minute"],
self.cur_world.time["current_second"],
self.cur_world.time["current_era"],
) = from_datetime(new_time)
debug(
f"{bcolors.OKGREEN}{self.cur_world.tick_rate} {self.cur_world.tick_type}s have passed...{bcolors.ENDC}"
)
self.print_current_time()
return
def init_game(self):
new_or_load = prompt(
f"You want to start a game. (L)oad the game or create a (n)ew one? (l/n)",
validator=validators.NotInListValidator(
["N", "n", "new", "L", "l", "load"]
),
validate_while_typing=True,
)
# New Game
if new_or_load.lower() in ["n", "new"]:
self.new_game()
# Load Game
elif new_or_load.lower() in ["l", "load"]:
self.load_game()
self.existing_worlds = [
x.name for x in Path(self.game_path / "worlds").iterdir() if x.is_dir()
]
return
def new_game(self, game_name: str = None):
if not game_name:
self.game_name = prompt(
f"Input the new game name: ",
validator=validators.IsInListValidator(self.existing_games),
)
else:
self.game_name = game_name
self.game_path = GAMES_PATH / self.game_name
Path(self.game_path / "worlds").mkdir(parents=True, exist_ok=True)
debug(f"{bcolors.OKGREEN}The game: {self.game_name} is created{bcolors.ENDC}")
return
def load_game(self):
if self.existing_games:
# self.game_name = prompt(
# f"Choose the game to load: {', '.join(self.existing_games)} :",
# validator=validators.NotInListValidator(self.existing_games),
# )
self.game_path = GAMES_PATH / self.game_name
self.existing_worlds = [
x.name for x in Path(self.game_path / "worlds").iterdir() if x.is_dir()
]
debug(
f"{bcolors.OKGREEN}The game: {self.game_name} is loaded{bcolors.ENDC}"
)
else:
debug(
f"{bcolors.FAIL}No existing games found. Create a new game.{bcolors.ENDC}"
)
self.input_handler(Input.init_game)
return
async def init_world(self, world_data: dict = None):
if world_data:
new_or_load = "n"
else:
new_or_load = "l"
# new_or_load = prompt(
# f"(L)oad existing world or create a (n)ew one? (l/n)",
# validator=validators.NotInListValidator(
# ["N", "n", "new", "L", "l", "load"]
# ),
# validate_while_typing=True,
# )
# New World
if new_or_load.lower() in ["n", "new"]:
await self.new_world(world_data)
self.cur_npcs_path: Path = self.cur_world_path / "npcs"
self.cur_global_goals_path: Path = self.cur_npcs_path / "global_goals.yaml"
await self.new_global_goals()
await self.new_npcs()
await self.new_npcs_social_connections()
if self.settings.text_to_image_model:
await self.generate_images()
# Load World
elif new_or_load.lower() in ["l", "load"]:
self.load_world()
self.load_npcs()
return self
async def new_world(self, world_data: dict = None):
# Add the world attributes and time attributes to the current world from the settings
populate_dataclass_with_dicts(
self.cur_world,
[self.settings.world_attributes_names, self.settings.world_time_names],
)
if world_data:
await self.new_world_from_ui(world_data)
else:
template_or_input = prompt(
f"Create a new world from the predefined (t)emplate or (i)nput world settings manually? (t/i)",
validator=validators.NotInListValidator(
["t", "template", "i", "input"]
),
validate_while_typing=True,
)
if template_or_input.lower() in ["t", "template"]:
self.new_world_from_template()
elif template_or_input.lower() in ["i", "input"]:
self.new_world_from_input()
return
async def new_world_from_ui(self, world_data: dict):
self.cur_world.name = world_data["name"]
debug(self.cur_world.name)
debug(type(self.cur_world.name))
for attribute_key in self.cur_world.attributes.keys():
new_attribute_value = world_data["attributes"].get(attribute_key, None)
if new_attribute_value is not None:
self.cur_world.attributes[attribute_key] = new_attribute_value
self.cur_world.time = world_data["time"]
self.cur_world.tick_type = world_data["tick_type"]
self.cur_world.tick_rate = world_data["tick_rate"]
self.cur_world.current_state_prompt = world_data["current_state_prompt"]
self.cur_world_path = (
GAMES_PATH / self.game_name / "worlds" / self.cur_world.name
)
self.cur_world_path.mkdir(parents=True, exist_ok=True)
self.world_general_description = self.cur_world.current_state_prompt
self.save_world()
debug(
f"{bcolors.OKGREEN}The world {self.cur_world.name} is created from UI{bcolors.ENDC}"
)
return
def settings_from_ui(self, settings_data: dict):
for key in self.settings.__dict__.keys():
new_settings_value = settings_data.get(key, None)
if new_settings_value:
setattr(self.settings, key, new_settings_value)
self.save_settings_from_ui()
return
def save_settings_from_ui(self):
self.game_path.mkdir(parents=True, exist_ok=True)
save_path = self.game_path / f"settings.yaml"
save_yaml_from_data(save_path, self.settings)
return
def new_world_from_template(self):
if not self.is_in_existing_items(self.existing_init_worlds, "world"):
self.input_handler(Input.init_world)
return
world_template_name = prompt(
f"Choose the world template to load: {', '.join(self.existing_init_worlds)} ",
validator=validators.NotInListValidator(self.existing_init_worlds),
)
world_template_path = INIT_WORLDS_PATH / world_template_name
load_yaml_to_dataclass(self.cur_world, world_template_path)
if self.cur_world.name in self.existing_worlds:
self.cur_world.name = prompt(
f"World with name {self.cur_world.name} already exists. Choose a new name:",
validator=validators.IsInListValidator(self.existing_worlds),
)
self.cur_world_path = (
GAMES_PATH / self.game_name / "worlds" / self.cur_world.name
)
self.cur_world_path.mkdir(parents=True, exist_ok=True)
self.world_general_description = self.cur_world.current_state_prompt
self.save_world()
debug(
f"{bcolors.OKGREEN}The world {self.cur_world.name} is created from template {world_template_name}{bcolors.ENDC}"
)
return
def new_world_from_input(self):
self.cur_world.name = prompt(
"Input the name of the world: ",
validator=validators.IsInListValidator(self.existing_worlds),
)
self.cur_world.attributes["temperature"] = float(
prompt(
"Input the temperature (in Celsius) of the world at current tick: ",
validator=validators.is_float,
validate_while_typing=True,
)
)
self.settings.number_of_npcs = int(
prompt(
"Input the number of npcs you want to create: ",
validator=validators.is_number,
validate_while_typing=True,
)
)
self.cur_world.attributes["current_era"] = prompt(
"Input the current era (BC/AD) of the world: ",
validator=validators.is_era,
validate_while_typing=True,
)
self.cur_world.attributes["current_year"] = int(
prompt(
"Input the current year of the world: ",
validator=validators.is_number,
validate_while_typing=True,
)
)
self.cur_world.attributes["current_month"] = int(
prompt(
"Input the current month of the world: ",
validator=validators.is_month,
validate_while_typing=True,
)
)
self.cur_world.attributes["current_day"] = int(
prompt(
"Input the current day of the world: ",
validator=validators.is_day,
validate_while_typing=True,
)
)
self.cur_world.attributes["current_hour"] = int(
prompt(
"Input the current hour of the world (24h format): ",
validator=validators.is_hour,
validate_while_typing=True,
)
)
self.cur_world.attributes["current_minute"] = int(
prompt(
"Input the current minute of the world: ",
validator=validators.is_minute,
validate_while_typing=True,
)
)
self.cur_world.attributes["current_second"] = int(
prompt(
"Input the current second of the world: ",
validator=validators.is_second,
validate_while_typing=True,
)
)
self.cur_world.tick_type = prompt(
"Input tick type (days/hours/minutes/seconds): ",
validator=validators.is_tick_type,
validate_while_typing=True,
)
self.cur_world.tick_rate = int(
prompt(
"Input the tick rate (how much time of tick_type passes in the world per tick): ",
validator=validators.is_number,
validate_while_typing=True,
)
)
self.cur_world.current_tick = 0
self.cur_world.current_state_prompt = prompt(
"Input the description of the current World state: "
)
self.world_general_description = self.cur_world.current_state_prompt
self.cur_world_path = (
GAMES_PATH / self.game_name / "worlds" / self.cur_world.name
)
self.save_world()
debug(
f"{bcolors.OKGREEN}The world {self.cur_world.name} is loaded from input{bcolors.ENDC}"
)
return
def load_world(self):
if not self.is_in_existing_items(self.existing_worlds, "world"):
self.input_handler(Input.init_world)
return
# world_name_to_load = prompt(
# f"Choose the world to load: {', '.join(self.existing_worlds)} ",
# validator=validators.NotInListValidator(self.existing_worlds),
# )
self.cur_world_path = Path(self.game_path / "worlds" / os.listdir(self.game_path / "worlds")[0])
last_modified_world_yaml = get_last_modified_file(self.cur_world_path)
load_yaml_to_dataclass(self.cur_world, last_modified_world_yaml)
oldest_modified_world_yaml = get_oldest_modified_file(self.cur_world_path)
with open(oldest_modified_world_yaml) as f:
oldest_modified_world = yaml.safe_load(f)
self.world_general_description = oldest_modified_world["current_state_prompt"]
debug(
f"{bcolors.OKGREEN}The world: {self.cur_world.name} is loaded{bcolors.ENDC}"
)
return
async def new_npcs(self):
if not self.cur_npcs_path.exists():
self.cur_npcs_path.mkdir(parents=True, exist_ok=True)
new_npc_prompts = []
# Create NPC placeholders and prompts
for npc_num in range(self.settings.number_of_npcs):
debug(
f"{bcolors.OKCYAN}Generating NPC {npc_num+1}/{self.settings.number_of_npcs}...{bcolors.ENDC}"
)
new_npc = Npc()
self.npcs.append(new_npc)
populate_dataclass_with_dicts(new_npc, [self.settings.npc_attributes_names])
new_npc_prompt = create_npc_request.format(
world_general_description=self.world_general_description,
world_current_attributes=self.cur_world.attributes,
npc_yaml_template=yaml.dump(
new_npc, sort_keys=False, Dumper=YamlDumperDoubleQuotes
),
global_goal=random.choice(self.global_goals),
)
new_npc_prompts.append(new_npc_prompt)
openai_kwargs = {
"model": self.settings.LLM_model,
"tries_num": self.settings.llm_request_tries_num,
"response_processors": [yaml_from_str, check_yaml_new_npc],
"verbose": self.settings.openai_verbose,
"api_key": os.environ.get("OPENAI_API_KEY"),
}
# NPC batch generation
npcs_data = await batch_completion(new_npc_prompts, openai_kwargs=openai_kwargs)
# Save NPCs to yaml and self.npcs
for npc, new_npc_data in zip(self.npcs, npcs_data):
self.save_npc(new_npc_data)
new_npc_yaml_path = (
self.cur_npcs_path
/ new_npc_data["name"]
/ f"npc_tick_{self.cur_world.current_tick}.yaml"
)
load_yaml_to_dataclass(npc, new_npc_yaml_path)
debug(
f"{bcolors.OKGREEN}NPC {new_npc.name} generated successfully{bcolors.ENDC}"
)
return
def load_npcs(self):
self.cur_npcs_path = self.cur_world_path / "npcs"
for npc_path in self.cur_npcs_path.iterdir():
# Skip global_goals.yaml file
if npc_path.name == "global_goals.yaml":
continue
new_npc = Npc()
last_modified_npc_yaml = get_last_modified_file(npc_path)
load_yaml_to_dataclass(new_npc, last_modified_npc_yaml)
self.npcs.append(new_npc)
async def new_global_goals(self):
debug(f"{bcolors.OKCYAN}Generating global goals for NPCs...{bcolors.ENDC}")
# Duplicate, refactor later
with open(YAML_TEMPLATES_PATH / "npc.yaml", "r") as f:
npc_yaml_template = yaml.safe_load(f)
for attribute in self.settings.npc_attributes_names:
npc_yaml_template["attributes"][attribute] = 0
with open(YAML_TEMPLATES_PATH / "global_goals.yaml", "r") as f:
npc_global_goals_template = yaml.safe_load(f)
create_global_goals_prompt = create_global_goals.format(
world_general_description=self.world_general_description,
npc_yaml_template=yaml.dump(
npc_yaml_template, sort_keys=False, Dumper=YamlDumperDoubleQuotes
),
num_global_goals=self.settings.npc_num_global_goals,
global_goals_yaml_template=yaml.dump(
npc_global_goals_template,
sort_keys=False,
Dumper=YamlDumperDoubleQuotes,
),
)
self.global_goals = await request_openai(
model=self.settings.LLM_model,
prompt=create_global_goals_prompt,
tries_num=self.settings.llm_request_tries_num,
response_processors=[yaml_from_str],
verbose=self.settings.openai_verbose,
api_key=os.environ.get("OPENAI_API_KEY"),
)
self.save_global_goals()
debug(f"{bcolors.OKGREEN}Global goals generated successfully{bcolors.ENDC}")
return
async def new_npcs_social_connections(self):
debug(
f"{bcolors.OKCYAN}Generating social connections between NPCs...{bcolors.ENDC}"
)
# If there is only one npc, skip social connections generation
if len(self.npcs) < 2:
self.npcs[0].social_connections = []
self.save_npc(self.npcs[0])
return
keys_to_delete = [
key
for key in self.npcs[0].__dict__.keys()
if key not in ["name", "global_goal", "current_state_prompt"]
]
social_connections_prompts = []
for current_npc in self.npcs:
corresponding_other_npcs = []
for other_npc in self.npcs:
if current_npc == other_npc:
continue
npc_dict = dataclass_to_dict_copy(other_npc, keys_to_delete)
corresponding_other_npcs.append(npc_dict)
created_social_connections_prompt = create_social_connections.format(
world_general_description=self.cur_world.current_state_prompt,
current_npc_name=current_npc.name,
current_npc_state=current_npc.current_state_prompt,
max_npc_social_connections=self.settings.max_npc_social_connections,
npc_social_connection_yaml_template=load_yaml(
YAML_TEMPLATES_PATH / "npc_social_connections.yaml"
),
other_npcs=[
yaml.dump(npc, sort_keys=False, Dumper=YamlDumperDoubleQuotes)
for npc in corresponding_other_npcs
],
)
social_connections_prompts.append(created_social_connections_prompt)
openai_kwargs = {
"model": self.settings.LLM_model,
"tries_num": self.settings.llm_request_tries_num,
"response_processors": [yaml_from_str],
"verbose": self.settings.openai_verbose,
"api_key": os.environ.get("OPENAI_API_KEY"),
}
npcs_social_connections = await batch_completion(social_connections_prompts, openai_kwargs=openai_kwargs)
for current_npc, current_npc_social_connections in zip(self.npcs, npcs_social_connections):
current_npc.social_connections = current_npc_social_connections
self.save_npc(current_npc)
def save_world(self):
self.cur_world_path.mkdir(parents=True, exist_ok=True)
save_path = (
self.cur_world_path / f"world_tick_{self.cur_world.current_tick}.yaml"
)
save_yaml_from_data(save_path, self.cur_world)
return
def save_global_goals(self):
self.cur_global_goals_path.parent.mkdir(parents=True, exist_ok=True)
save_yaml_from_data(self.cur_global_goals_path, self.global_goals)
return
def save_npcs(self):
for npc in self.npcs:
self.save_npc(npc)
return
def save_npc(self, npc_data: dict | Npc):
if isinstance(npc_data, dict):
npc_name = npc_data["name"]
else:
npc_name = npc_data.name
npc_dir = self.cur_npcs_path / npc_name
npc_dir.mkdir(parents=True, exist_ok=True)
save_yaml_from_data(
npc_dir / f"npc_tick_{self.cur_world.current_tick}.yaml", npc_data
)
return
def is_in_existing_items(self, existing_items: List | None, item_name: str):
in_existing_items = False
if not existing_items:
debug(
f"{bcolors.FAIL}No existing {item_name}s found. Create a new {item_name}.{bcolors.ENDC}"
)
in_existing_items = False
else:
in_existing_items = True
return in_existing_items
def print_info_world(self):
self.print_current_time()
npc_names = [npc.name for npc in self.npcs]
debug(
f"\n"
f"{bcolors.OKBLUE}World name: {bcolors.ENDC}{self.cur_world.name}\n"
f"{bcolors.OKBLUE}Current state: {bcolors.ENDC}{self.cur_world.current_state_prompt}\n"
f"{bcolors.OKBLUE}Time: {bcolors.ENDC}{self.cur_world.time}\n"
f"{bcolors.OKBLUE}Attributes: {bcolors.ENDC}{self.cur_world.attributes}\n"
f"{bcolors.OKBLUE}Current tick: {bcolors.ENDC}{self.cur_world.current_tick}\n"
f"{bcolors.OKBLUE}Tick type: {bcolors.ENDC}{self.cur_world.tick_type}\n"
f"{bcolors.OKBLUE}Tick rate: {bcolors.ENDC}{self.cur_world.tick_rate}\n"
f"{bcolors.OKBLUE}NPCs: {bcolors.ENDC}{npc_names}"
f"\n"
)
return
def print_info_npcs(self):
for npc in self.npcs:
debug(
f"\n"
f"{bcolors.OKBLUE}NPC name: {bcolors.ENDC}{npc.name}\n"
f"{bcolors.OKBLUE}Current state: {bcolors.ENDC}{npc.current_state_prompt}\n"
f"{bcolors.OKBLUE}Global goal: {bcolors.ENDC}{npc.global_goal}\n"
f"{bcolors.OKBLUE}Attributes: {bcolors.ENDC}{npc.attributes}\n"
f"\n"
)
return
def print_info_all(self):
self.print_info_world()
self.print_info_npcs()
return
def current_time_to_str(self):
current_time = (
f"{hour_to_iso(self.cur_world.time['current_hour'])}:{minute_to_iso(self.cur_world.time['current_minute'])}:"
f"{second_to_iso(self.cur_world.time['current_second'])}"
)
return current_time
def current_date_to_str(self):
current_date = (
f"{self.cur_world.time['current_day']} {int_to_month(self.cur_world.time['current_month'])},"
f"{self.cur_world.time['current_year']} {self.cur_world.time['current_era']}. "
)
return current_date
def print_current_time(self):
current_time = (
f"{bcolors.OKBLUE}"
f"Current time:{bcolors.ENDC} {self.current_time_to_str()}, {self.current_date_to_str()}"
)
debug(current_time)
return
def quit_game(self):
debug(f"{bcolors.OKCYAN}Quitting game...{bcolors.ENDC}")
sys.exit(0)
async def generate_images(self):
image_paths = []
img_prompts = []
if self.settings.text_to_image_generate_world:
image_paths.append(str(self.cur_world_path / f"world_tick_{self.cur_world.current_tick}.jpg"))
img_prompts.append(self.cur_world.current_state_prompt)
if self.settings.text_to_image_generate_npcs:
for npc in self.npcs:
npc_image_prompt = generate_npc_image.format(
npc_name=npc.name,
npc_current_state_prompt=npc.current_state_prompt,
world_current_state_prompt=self.cur_world.current_state_prompt,
daytime=hour_to_daytime(self.cur_world.time["current_hour"]),
date=self.current_date_to_str(),
temperature=self.cur_world.attributes["temperature"],
)
image_paths.append(str(self.cur_npcs_path / npc.name / f"npc_tick_{self.cur_world.current_tick}.jpg"))
img_prompts.append(npc_image_prompt)
openai_kwargs = {
"model_name": self.settings.text_to_image_model,
"tries_num": self.settings.llm_request_tries_num,
"response_processors": [],
"verbose": self.settings.openai_verbose,
"API_key": os.environ.get("OPENAI_API_KEY"),
"model_type": "image",
"img_size": self.settings.text_to_image_size,
"img_quality": self.settings.text_to_image_quality,
"img_n": self.settings.text_to_image_n,
"response_format": "b64_json"
}
await batch_image_generation(image_paths, img_prompts, openai_kwargs)
if __name__ == "__main__":
# with open(YAML_TEMPLATES_PATH / "npc.yaml", "r") as f:
# npc_yaml_template = yaml.safe_load(f)
# t = ["happiness", "health", "hunger", "love", "rested", "stress", "wealth"]
# for i in t:
# npc_yaml_template["attributes"][i] = 0
# cur_time = to_datetime(2021, 1, 1, 0, 0, 0)
# debug(cur_time.astype("datetime64[Y]"))
# time_delta = np.timedelta64(1, "Y")
# time_delta_d = np.timedelta64(365, "D")
# debug(type(time_delta.astype("datetime64[Y]").astype(int)))
# new_time = cur_time + time_delta_d
# debug(new_time)
# t = yaml_from_str(
# """
# npc_new_state: "Ragnar the Warrior spends the day on a successful raid against bandits, showcasing his improved sword fighting skills. However, he suffers from a minor injury, decreasing his health by 1, while also gaining 1 happiness and 2 stress from the adrenaline rush of battle."
# attributes:
# health: -1
# happiness: 1
# stress: 2"""
# )
# print(t.get("npc_new_state"))
settings = Settings()