From 36e763519d092891acdc2556b80aaf669e1dd6b0 Mon Sep 17 00:00:00 2001 From: better629 Date: Sat, 7 Oct 2023 17:46:56 +0800 Subject: [PATCH] update temp_storage --- examples/st_game/README.md | 15 ++++++++++++++- examples/st_game/actions/run_reflect_action.py | 8 ++++---- examples/st_game/roles/st_role.py | 7 +++++-- examples/st_game/run_st_game.py | 6 +++++- examples/st_game/utils/const.py | 1 + examples/st_game/utils/mg_ga_transform.py | 10 +++++++++- 6 files changed, 38 insertions(+), 9 deletions(-) diff --git a/examples/st_game/README.md b/examples/st_game/README.md index 6b6661f42..3ef7d620b 100644 --- a/examples/st_game/README.md +++ b/examples/st_game/README.md @@ -1,9 +1,22 @@ ## Stanford Town Game +### 前置 +`examples/st_game/utils/const.py`配置的为当前项目项目的存储路径,为了方便GA的前端对接数据(避免改动它那块的代码),可将`const.py`下的 + +``` +STORAGE_PATH = ROOT_PATH.joinpath("storage") +TEMP_STORAGE_PATH = ROOT_PATH.joinpath("temp_storage") +# 更新为 +STORAGE_PATH = Path("{path/to/ga/storage}") +TEMP_STORAGE_PATH = Path("{path/to/ga/temp_storage}") +``` +这样可用实现不改变GA代码情况下,实现仿真数据的对接。不然得修改GA的代码来适配MG的输出路径。 + ### 后端服务启动 执行入口为:`python3 run_st_game.py "Host a open lunch party at 13:00 pm" "base_the_ville_isabella_maria_klaus" "test_sim" 10` `idea`为用户给第一个Agent的用户心声,并通过这个心声进行传播,看最后多智能体是否达到举办、参加活动的目标。 ### 前端服务启动 -进入`generative_agents/environment/frontend_server`,使用`~~python manage.py runserver~~`启动前端服务。 +进入`generative_agents/environment/frontend_server`,使用`python manage.py runserver`启动前端服务。 +访问`http://localhost:8000/simulator_home` 进入当前的仿真界面。 diff --git a/examples/st_game/actions/run_reflect_action.py b/examples/st_game/actions/run_reflect_action.py index d29b654f8..cd3d38664 100644 --- a/examples/st_game/actions/run_reflect_action.py +++ b/examples/st_game/actions/run_reflect_action.py @@ -129,10 +129,10 @@ class AgentEventTriple(STAction): def create_prompt_input(statements, role): if "(" in statements: statements = statements.split("(")[-1].split(")")[0] - prompt_input = [role.scratch.name, - statements, - role.scratch.name] - return prompt_input + prompt_input = [role.scratch.name, + statements, + role.scratch.name] + return prompt_input prompt_input = create_prompt_input(statements, role) prompt = self.generate_prompt_with_tmpl_filename(prompt_input, diff --git a/examples/st_game/roles/st_role.py b/examples/st_game/roles/st_role.py index fdde4f5e3..19afcc2d2 100644 --- a/examples/st_game/roles/st_role.py +++ b/examples/st_game/roles/st_role.py @@ -138,8 +138,8 @@ class STRole(Role): observed = self._rc.env.memory.get_by_actions(self._rc.watch) self._rc.news = self._rc.memory.remember(observed) if len(self._rc.news) == 1 and self._rc.news[0].cause_by == UserRequirement: - self.add_inner_voice(self._rc.news[0].content) logger.warning(f"Role: {self.name} add inner voice: {self._rc.news[0].content}") + self.add_inner_voice(self._rc.news[0].content) return 1 # always return 1 to execute role's `_react` @@ -151,6 +151,9 @@ class STRole(Role): thought = generate_inner_thought(whisper) + # init scratch curr_time with self.curr_time + self._rc.scratch.curr_time = self.curr_time + created = self._rc.scratch.curr_time if self._rc.scratch.curr_time else datetime.datetime.now() expiration = created + datetime.timedelta(days=30) run_event_triple = AgentEventTriple() @@ -484,7 +487,7 @@ class STRole(Role): else: ret = False time.sleep(1) - logger.warning(f"{self.sim_code}/environment/{self.step}.json not exist or parses failed," + logger.warning(f"{self.sim_code}/environment/{self.step}.json not exist or parses failed, " f"sleep 1s and re-check") return ret diff --git a/examples/st_game/run_st_game.py b/examples/st_game/run_st_game.py index 722308120..d36ff92ca 100644 --- a/examples/st_game/run_st_game.py +++ b/examples/st_game/run_st_game.py @@ -7,7 +7,7 @@ import fire from examples.st_game.stanford_town import StanfordTown from examples.st_game.roles.st_role import STRole -from examples.st_game.utils.mg_ga_transform import get_reverie_meta +from examples.st_game.utils.mg_ga_transform import get_reverie_meta, write_curr_sim_code, write_curr_step from examples.st_game.utils.const import STORAGE_PATH from examples.st_game.utils.utils import copy_folder @@ -39,6 +39,10 @@ async def startup(idea: str, role.load_from(role_stg_path) roles.append(role) + # init temp_storage + write_curr_sim_code({"sim_code": sim_code}) + write_curr_step({"step": reverie_meta.get("step", 0)}) + town = StanfordTown() town.wakeup_roles(roles) diff --git a/examples/st_game/utils/const.py b/examples/st_game/utils/const.py index 3dcc4e172..38e80f103 100644 --- a/examples/st_game/utils/const.py +++ b/examples/st_game/utils/const.py @@ -6,6 +6,7 @@ from pathlib import Path ROOT_PATH = Path(__file__).parent.parent STORAGE_PATH = ROOT_PATH.joinpath("storage") +TEMP_STORAGE_PATH = ROOT_PATH.joinpath("temp_storage") MAZE_ASSET_PATH = ROOT_PATH.joinpath("static_dirs/assets/the_ville") PROMPTS_DIR = ROOT_PATH.joinpath("prompts") diff --git a/examples/st_game/utils/mg_ga_transform.py b/examples/st_game/utils/mg_ga_transform.py index 911d626a7..9b445fb54 100644 --- a/examples/st_game/utils/mg_ga_transform.py +++ b/examples/st_game/utils/mg_ga_transform.py @@ -6,7 +6,7 @@ import json from metagpt.logs import logger -from examples.st_game.utils.const import STORAGE_PATH +from examples.st_game.utils.const import STORAGE_PATH, TEMP_STORAGE_PATH from examples.st_game.utils.utils import read_json_file, write_json_file @@ -43,3 +43,11 @@ def get_role_environment(sim_code: str, role_name: str, step: int = 0) -> dict: role_env = environment.get(role_name, None) return role_env + + +def write_curr_sim_code(curr_sim_code: dict): + write_json_file(TEMP_STORAGE_PATH.joinpath("curr_sim_code.json"), curr_sim_code) + + +def write_curr_step(curr_step: dict): + write_json_file(TEMP_STORAGE_PATH.joinpath("curr_step.json"), curr_step)