alienet commited on
Commit
ebe03c4
·
1 Parent(s): 6fd9505
BookWorld.py CHANGED
@@ -47,7 +47,7 @@ class Server():
47
  map_file_path: str = config["map_file_path"] if "map_file_path" in config else ""
48
  role_file_dir: str = config["role_file_dir"] if "role_file_dir" in config else "./data/roles/"
49
  loc_file_path: str = config["loc_file_path"]
50
- self.intervention: str = "" if "intervention" in config else ""
51
  self.event = self.intervention
52
  self.script: str = config["script"] if "script" in config else ""
53
  self.language: str = config["language"] if "language" in config else "zh"
@@ -820,7 +820,7 @@ class Server():
820
  """
821
  if not self.if_save:
822
  return
823
- save_dir = f"./experiment_saves/{self.experiment_name}/{self.role_llm_name}/{self.start_time}"
824
  create_dir(save_dir)
825
  location_setted, goal_setted = False,False
826
  if stage in ["location","goal","action"]:
@@ -861,10 +861,10 @@ class Server():
861
  meta_info = load_json_file(os.path.join(save_dir, "./meta_info.json"))
862
  filename = os.path.join(save_dir, f"./server_info.json")
863
  states = load_json_file(filename)
864
- self.__setstate__(states)
865
- for role_code in self.role_codes:
866
- self.role_agents[role_code].load_from_file(save_dir)
867
- self.world_agent.load_from_file(save_dir)
868
  self.history_manager.load_from_file(save_dir)
869
 
870
  for record in self.history_manager.detailed_history:
@@ -902,6 +902,7 @@ class BookWorld():
902
  role_llm_name=role_llm_name,
903
  embedding_name=embedding_name)
904
  self.selected_scene = None
 
905
 
906
  def set_generator(self,
907
  rounds:int = 10,
@@ -981,6 +982,8 @@ class BookWorld():
981
  "uuid": message_id,
982
  "scene": self.server.cur_round
983
  }
 
 
984
  return message
985
 
986
  def get_settings_info(self):
@@ -1003,6 +1006,7 @@ class BookWorld():
1003
  location_name,location_description = self.server.world_agent.find_location_name(location_code),self.server.world_agent.locations_info[location_code]["description"]
1004
  status['location'] = {'name': location_name, 'description': location_description}
1005
  status['characters'] = self.get_characters_info()
 
1006
  return status
1007
 
1008
  def handle_message_edit(self,record_id,new_text):
@@ -1038,7 +1042,12 @@ class BookWorld():
1038
  logs = self.server.history_manager.get_complete_history()
1039
  story = self.server.world_agent.log2story(logs)
1040
  return story
1041
-
 
 
 
 
 
1042
 
1043
 
1044
  if __name__ == "__main__":
 
47
  map_file_path: str = config["map_file_path"] if "map_file_path" in config else ""
48
  role_file_dir: str = config["role_file_dir"] if "role_file_dir" in config else "./data/roles/"
49
  loc_file_path: str = config["loc_file_path"]
50
+ self.intervention: str = config["intervention"] if "intervention" in config else ""
51
  self.event = self.intervention
52
  self.script: str = config["script"] if "script" in config else ""
53
  self.language: str = config["language"] if "language" in config else "zh"
 
820
  """
821
  if not self.if_save:
822
  return
823
+ save_dir = f"./experiment_saves/{self.experiment_name}/{self.role_llm_name}_{self.start_time}"
824
  create_dir(save_dir)
825
  location_setted, goal_setted = False,False
826
  if stage in ["location","goal","action"]:
 
861
  meta_info = load_json_file(os.path.join(save_dir, "./meta_info.json"))
862
  filename = os.path.join(save_dir, f"./server_info.json")
863
  states = load_json_file(filename)
864
+ # self.__setstate__(states)
865
+ # for role_code in self.role_codes:
866
+ # self.role_agents[role_code].load_from_file(save_dir)
867
+ # self.world_agent.load_from_file(save_dir)
868
  self.history_manager.load_from_file(save_dir)
869
 
870
  for record in self.history_manager.detailed_history:
 
902
  role_llm_name=role_llm_name,
903
  embedding_name=embedding_name)
904
  self.selected_scene = None
905
+ self.idx = 0
906
 
907
  def set_generator(self,
908
  rounds:int = 10,
 
982
  "uuid": message_id,
983
  "scene": self.server.cur_round
984
  }
985
+ self.server.history_manager.add_message(message)
986
+
987
  return message
988
 
989
  def get_settings_info(self):
 
1006
  location_name,location_description = self.server.world_agent.find_location_name(location_code),self.server.world_agent.locations_info[location_code]["description"]
1007
  status['location'] = {'name': location_name, 'description': location_description}
1008
  status['characters'] = self.get_characters_info()
1009
+ self.server.history_manager.add_status(status)
1010
  return status
1011
 
1012
  def handle_message_edit(self,record_id,new_text):
 
1042
  logs = self.server.history_manager.get_complete_history()
1043
  story = self.server.world_agent.log2story(logs)
1044
  return story
1045
+
1046
+ def load_next_message_status(self,):
1047
+ message = self.server.history_manager.message_history[self.idx]
1048
+ status = self.server.history_manager.status_history[self.idx]
1049
+ self.idx += 1
1050
+ return message, status
1051
 
1052
 
1053
  if __name__ == "__main__":
app.py CHANGED
@@ -1,3 +1,4 @@
 
1
  from fastapi import FastAPI, WebSocket, WebSocketDisconnect, HTTPException, Request
2
  from fastapi.staticfiles import StaticFiles
3
  from fastapi.responses import HTMLResponse, FileResponse
@@ -7,13 +8,17 @@ import asyncio
7
  import os
8
  from pathlib import Path
9
  from datetime import datetime
10
- from bw_utils import is_image, load_json_file
11
  from BookWorld import BookWorld
12
  os.chdir(os.path.dirname(os.path.abspath(__file__)))
13
 
14
  app = FastAPI()
15
  default_icon_path = './frontend/assets/images/default-icon.jpg'
16
  config = load_json_file('config.json')
 
 
 
 
17
  for key in config:
18
  if "API_KEY" in key and config[key]:
19
  os.environ[key] = config[key]
@@ -91,7 +96,7 @@ class ConnectionManager:
91
  'data': status
92
  })
93
  # 添加延迟,控制消息发送频率
94
- await asyncio.sleep(0.2) # 可以调整这个值
95
  else:
96
  break
97
  except asyncio.CancelledError:
@@ -102,20 +107,24 @@ class ConnectionManager:
102
 
103
  async def get_initial_data(self):
104
  """获取初始化数据"""
105
- return {
106
  'characters': self.bw.get_characters_info(),
107
  'map': self.bw.get_map_info(),
108
  'settings': self.bw.get_settings_info(),
109
  'status': self.bw.get_current_status(),
110
- 'history_messages':self.bw.get_history_messages(save_dir = config["save_dir"]),
 
111
  }
 
 
112
 
113
  async def get_next_message(self):
114
  """从BookWorld获取下一条消息"""
115
- message = self.bw.generate_next_message()
116
- if not os.path.exists(message["icon"]) or not is_image(message["icon"]):
117
- message["icon"] = default_icon_path
118
- status = self.bw.get_current_status()
 
119
  return message,status
120
 
121
  manager = ConnectionManager()
@@ -173,6 +182,10 @@ async def load_preset(request: Request):
173
  role_llm_name=config["role_llm_name"],
174
  embedding_name=config["embedding_model_name"]
175
  )
 
 
 
 
176
  manager.bw.set_generator(
177
  rounds=config["rounds"],
178
  save_dir=config["save_dir"],
 
1
+ from email import message
2
  from fastapi import FastAPI, WebSocket, WebSocketDisconnect, HTTPException, Request
3
  from fastapi.staticfiles import StaticFiles
4
  from fastapi.responses import HTMLResponse, FileResponse
 
8
  import os
9
  from pathlib import Path
10
  from datetime import datetime
11
+ from bw_utils import get_grandchild_folders, is_image, load_json_file
12
  from BookWorld import BookWorld
13
  os.chdir(os.path.dirname(os.path.abspath(__file__)))
14
 
15
  app = FastAPI()
16
  default_icon_path = './frontend/assets/images/default-icon.jpg'
17
  config = load_json_file('config.json')
18
+ experiment_name = config["preset_path"].split("/")[-1].split(".")[0]
19
+ save_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'experiment_saves', experiment_name)
20
+ config["save_dir"] = os.path.join(save_dir, get_grandchild_folders(save_dir)[0])
21
+
22
  for key in config:
23
  if "API_KEY" in key and config[key]:
24
  os.environ[key] = config[key]
 
96
  'data': status
97
  })
98
  # 添加延迟,控制消息发送频率
99
+ await asyncio.sleep(2) # 可以调整这个值
100
  else:
101
  break
102
  except asyncio.CancelledError:
 
107
 
108
  async def get_initial_data(self):
109
  """获取初始化数据"""
110
+ data = {
111
  'characters': self.bw.get_characters_info(),
112
  'map': self.bw.get_map_info(),
113
  'settings': self.bw.get_settings_info(),
114
  'status': self.bw.get_current_status(),
115
+ # 'history_messages':self.bw.get_history_messages(save_dir = config["save_dir"]),
116
+ 'history_messages':[],
117
  }
118
+ self.bw.get_history_messages(save_dir = config["save_dir"])
119
+ return data
120
 
121
  async def get_next_message(self):
122
  """从BookWorld获取下一条消息"""
123
+ # message = self.bw.generate_next_message()
124
+ # if not os.path.exists(message["icon"]) or not is_image(message["icon"]):
125
+ # message["icon"] = default_icon_path
126
+ # status = self.bw.get_current_status()
127
+ message,status = self.bw.load_next_message_status()
128
  return message,status
129
 
130
  manager = ConnectionManager()
 
182
  role_llm_name=config["role_llm_name"],
183
  embedding_name=config["embedding_model_name"]
184
  )
185
+ config["preset_path"] = preset_path
186
+ experiment_name = preset_path.split("/")[-1].split(".")[0]
187
+ save_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'experiment_saves', experiment_name)
188
+ config["save_dir"] = os.path.join(save_dir, get_grandchild_folders(save_dir)[0])
189
  manager.bw.set_generator(
190
  rounds=config["rounds"],
191
  save_dir=config["save_dir"],
config.json CHANGED
@@ -1,13 +1,13 @@
1
  {
2
- "role_llm_name": "gemini-2.0-flash",
3
- "world_llm_name": "gemini-2.0-flash",
4
  "embedding_model_name":"bge-m3",
5
- "preset_path":"./experiment_presets/experiment_icefire.json",
6
- "if_save": 0,
7
  "scene_mode": 1,
8
- "rounds": 10,
9
  "save_dir": "",
10
- "mode": "free",
11
 
12
  "OPENAI_API_KEY":"",
13
  "GEMINI_API_KEY":"",
 
1
  {
2
+ "role_llm_name": "claude-3.5-sonnet",
3
+ "world_llm_name": "claude-3.5-sonnet",
4
  "embedding_model_name":"bge-m3",
5
+ "preset_path":"./experiment_presets/experiment_icefire_bloody_wedding.json",
6
+ "if_save": 1,
7
  "scene_mode": 1,
8
+ "rounds": 5,
9
  "save_dir": "",
10
+ "mode": "script",
11
 
12
  "OPENAI_API_KEY":"",
13
  "GEMINI_API_KEY":"",
data/maps/example_map.csv CHANGED
@@ -1,4 +1,3 @@
1
- ,laboratory,university,eldridge_corporation
2
- laboratory,0,2,5
3
- university,2,0,4
4
- eldridge_corporation,5,4,0
 
1
+ ,university,eldridge_corporation
2
+ university,0,4
3
+ eldridge_corporation,4,0
 
experiment_presets/example_free.json CHANGED
@@ -4,7 +4,7 @@
4
  "map_file_path":"./data/maps/example_map.csv",
5
  "loc_file_path":"./data/locations/example_locations.json",
6
  "role_file_dir":"./data/roles/",
7
- "role_agent_codes":["Lacia-en","Trek-en"],
8
  "intervention":"One day, an enigmatic signal from an unknown source reached Lacia and Trek...",
9
  "script":"",
10
  "source":"example_world",
 
4
  "map_file_path":"./data/maps/example_map.csv",
5
  "loc_file_path":"./data/locations/example_locations.json",
6
  "role_file_dir":"./data/roles/",
7
+ "role_agent_codes":["Lacia-en","Trek-en","Falko-en"],
8
  "intervention":"One day, an enigmatic signal from an unknown source reached Lacia and Trek...",
9
  "script":"",
10
  "source":"example_world",
experiment_presets/example_script.json CHANGED
@@ -4,7 +4,7 @@
4
  "map_file_path":"./data/maps/example_map.csv",
5
  "loc_file_path":"./data/locations/example_locations.json",
6
  "role_file_dir":"./data/roles/",
7
- "role_agent_codes":["Lacia-en","Trek-en"],
8
  "intervention":"",
9
  "script":"One day, an enigmatic signal from an unknown source reached Lacia and Trek. The pattern resembled Trek’s early consciousness digitization code—but far more evolved. Tracing its origin to lunar orbit, the two found themselves divided: Lacia urged caution, fearing the dangers of an unknown intelligence, while Trek saw it as proof that human evolution had already begun elsewhere. As they followed the signal, ideological tension grew—one seeking to contain it, the other longing to embrace it.",
10
  "source":"example_world",
 
4
  "map_file_path":"./data/maps/example_map.csv",
5
  "loc_file_path":"./data/locations/example_locations.json",
6
  "role_file_dir":"./data/roles/",
7
+ "role_agent_codes":["Lacia-en","Trek-en","Falko-en"],
8
  "intervention":"",
9
  "script":"One day, an enigmatic signal from an unknown source reached Lacia and Trek. The pattern resembled Trek’s early consciousness digitization code—but far more evolved. Tracing its origin to lunar orbit, the two found themselves divided: Lacia urged caution, fearing the dangers of an unknown intelligence, while Trek saw it as proof that human evolution had already begun elsewhere. As they followed the signal, ideological tension grew—one seeking to contain it, the other longing to embrace it.",
10
  "source":"example_world",
modules/history_manager.py CHANGED
@@ -8,6 +8,8 @@ import os
8
  class HistoryManager:
9
  def __init__(self):
10
  self.detailed_history = []
 
 
11
 
12
  def add_record(self, record):
13
  """添加一个事件记录
@@ -25,6 +27,12 @@ class HistoryManager:
25
  """
26
  self.detailed_history.append(record)
27
 
 
 
 
 
 
 
28
  def modify_record(self, record_id: str, detail: str):
29
  """修改特定记录"""
30
  for record in self.detailed_history:
 
8
  class HistoryManager:
9
  def __init__(self):
10
  self.detailed_history = []
11
+ self.status_history = []
12
+ self.message_history = []
13
 
14
  def add_record(self, record):
15
  """添加一个事件记录
 
27
  """
28
  self.detailed_history.append(record)
29
 
30
+ def add_status(self, status):
31
+ self.status_history.append(status)
32
+
33
+ def add_message(self, message):
34
+ self.message_history.append(message)
35
+
36
  def modify_record(self, record_id: str, detail: str):
37
  """修改特定记录"""
38
  for record in self.detailed_history: