From 84d37954b70326a45e71f8ee39584cedbdb85afe Mon Sep 17 00:00:00 2001 From: better629 Date: Mon, 4 Mar 2024 11:18:06 +0800 Subject: [PATCH] add temp_storage_path --- examples/st_game/README.md | 10 +++++-- examples/st_game/README_CN.md | 10 +++++-- .../st_game/actions/gen_action_details.py | 2 +- examples/st_game/actions/st_action.py | 2 +- examples/st_game/memory/agent_memory.py | 6 ++-- examples/st_game/plan/st_plan.py | 4 ++- examples/st_game/run_st_game.py | 28 +++++++++++++++---- examples/st_game/stanford_town.py | 5 ++-- .../tests/actions/test_gen_action_details.py | 4 ++- .../st_game/tests/plan/test_conversation.py | 3 +- examples/st_game/tests/roles/test_st_role.py | 5 ++-- examples/st_game/tests/test_reflect.py | 2 +- examples/st_game/utils/mg_ga_transform.py | 12 +++++--- 13 files changed, 66 insertions(+), 27 deletions(-) diff --git a/examples/st_game/README.md b/examples/st_game/README.md index cc4ada230..ea6547f42 100644 --- a/examples/st_game/README.md +++ b/examples/st_game/README.md @@ -1,7 +1,11 @@ ## Stanford Town Game ### Pre-Description -The path configured in `examples/st_game/utils/const.py` is the storage path of the current project. In order to facilitate GA(generative_agents)'s frontend docking data (to avoid changing its code), you can change the path under `const.py` like beflow +The path configured in `examples/st_game/utils/const.py` is the storage path of the current project. In order to facilitate GA( [generative_agents](https://github.com/joonspk-research/generative_agents) )'s frontend docking data (to avoid changing its code), you can set the value `temp_storage_path` to `temp_storage` of `generative_agents` when start `run_st_game.py`. like + +`python3 run_st_game.py --temp_storage_path path/to/ga/temp_storage xxx` + +Or change the path under `const.py` like beflow ``` STORAGE_PATH = ROOT_PATH.joinpath("storage") @@ -19,7 +23,9 @@ ### Backend service startup `idea` is the user's voice to the first Agent, and it is disseminated through this voice to see whether the final multi-agents achieve the goal of hosting or participating in the event. ### Frontend service startup -Enter `generative_agents/environment/frontend_server` and use `python manage.py runserver` to start the front-end service. +Enter project folder `generative_agents` + +Enter `environment/frontend_server` and use `python3 manage.py runserver` to start the front-end service. Visit `http://localhost:8000/simulator_home` to enter the current simulation interface. ## Appreciation diff --git a/examples/st_game/README_CN.md b/examples/st_game/README_CN.md index 9780b3ed8..4c4c2a41b 100644 --- a/examples/st_game/README_CN.md +++ b/examples/st_game/README_CN.md @@ -1,7 +1,11 @@ ## Stanford Town Game ### 前置 -`examples/st_game/utils/const.py`配置的路径为当前项目的存储路径,为了方便GA(generative_agents)的前端对接数据(避免改动它那块的代码),可将`const.py`下的 +`examples/st_game/utils/const.py`配置的路径为当前项目的存储路径,为了方便GA( [generative_agents](https://github.com/joonspk-research/generative_agents) )的前端对接数据(避免改动它那块的代码),可在启动`run_st_game.py`加上`temp_storage_path`指向`generative_agents`对应的`temp_storage`路径。比如 + +`python3 run_st_game.py --temp_storage_path path/to/ga/temp_storage xxx` + +或将`const.py`下的 ``` STORAGE_PATH = ROOT_PATH.joinpath("storage") @@ -18,7 +22,9 @@ ### 后端服务启动 `idea`为用户给第一个Agent的用户心声,并通过这个心声进行传播,看最后多智能体是否达到举办、参加活动的目标。 ### 前端服务启动 -进入`generative_agents/environment/frontend_server`,使用`python manage.py runserver`启动前端服务。 +进入`generative_agents`项目目录 + +进入`environment/frontend_server`,使用`python3 manage.py runserver`启动前端服务。 访问`http://localhost:8000/simulator_home` 进入当前的仿真界面。 ## Appreciation diff --git a/examples/st_game/actions/gen_action_details.py b/examples/st_game/actions/gen_action_details.py index aacc6852a..92a53087a 100644 --- a/examples/st_game/actions/gen_action_details.py +++ b/examples/st_game/actions/gen_action_details.py @@ -154,7 +154,7 @@ class GenActionArena(STAction): prompt_input = create_prompt_input(role, act_desp, act_world, act_sector) prompt = self.generate_prompt_with_tmpl_filename(prompt_input, prompt_template) self.fail_default_resp = self._func_fail_default_resp() - print('prompt ', prompt) + print("prompt ", prompt) output = await self._run_gpt35_max_tokens(prompt, max_tokens=15) logger.info(f"Role: {role.name} Action: {self.cls_name} output: {output}") return output diff --git a/examples/st_game/actions/st_action.py b/examples/st_game/actions/st_action.py index 0796b5c8d..81f66c080 100644 --- a/examples/st_game/actions/st_action.py +++ b/examples/st_game/actions/st_action.py @@ -58,7 +58,7 @@ class STAction(Action): async def _aask(self, prompt: str) -> str: return await self.llm.aask(prompt) - + async def _run_gpt35_max_tokens(self, prompt: str, max_tokens: int = 50, retry: int = 3): for idx in range(retry): try: diff --git a/examples/st_game/memory/agent_memory.py b/examples/st_game/memory/agent_memory.py index a66a40efd..d4f346fe3 100644 --- a/examples/st_game/memory/agent_memory.py +++ b/examples/st_game/memory/agent_memory.py @@ -219,7 +219,7 @@ class AgentMemory(Memory): poignancy=poignancy, keywords=keywords, filling=filling, - cause_by=cause_by + cause_by=cause_by, ) keywords = [i.lower() for i in keywords] @@ -267,7 +267,7 @@ class AgentMemory(Memory): embedding_key=embedding_pair[0], poignancy=poignancy, keywords=keywords, - filling=filling + filling=filling, ) keywords = [i.lower() for i in keywords] @@ -317,7 +317,7 @@ class AgentMemory(Memory): embedding_key=embedding_pair[0], poignancy=poignancy, keywords=keywords, - filling=filling + filling=filling, ) keywords = [i.lower() for i in keywords] diff --git a/examples/st_game/plan/st_plan.py b/examples/st_game/plan/st_plan.py index 017b3e1fb..080d87de1 100644 --- a/examples/st_game/plan/st_plan.py +++ b/examples/st_game/plan/st_plan.py @@ -608,7 +608,9 @@ async def _determine_action(role: "STRole"): # We decompose if the next action is longer than an hour, and fits the # criteria described in determine_decomp. if determine_decomp(act_desp, act_dura): - role.scratch.f_daily_schedule[curr_index : curr_index + 1] = await TaskDecomp().run(role, act_desp, act_dura) + role.scratch.f_daily_schedule[curr_index : curr_index + 1] = await TaskDecomp().run( + role, act_desp, act_dura + ) if curr_index_60 + 1 < len(role.scratch.f_daily_schedule): act_desp, act_dura = role.scratch.f_daily_schedule[curr_index_60 + 1] if act_dura >= 60: diff --git a/examples/st_game/run_st_game.py b/examples/st_game/run_st_game.py index a62767f07..523cfabaa 100644 --- a/examples/st_game/run_st_game.py +++ b/examples/st_game/run_st_game.py @@ -3,6 +3,7 @@ # @Desc : entry of Stanford Town(ST/st) game import asyncio +from typing import Optional import fire @@ -18,7 +19,9 @@ from examples.st_game.utils.utils import copy_folder from metagpt.logs import logger -async def startup(idea: str, fork_sim_code: str, sim_code: str, investment: float = 30.0, n_round: int = 500): +async def startup( + idea: str, fork_sim_code: str, sim_code: str, temp_storage_path: str, investment: float = 30.0, n_round: int = 500 +): town = StanfordTown() logger.info("StanfordTown init environment") @@ -45,8 +48,8 @@ async def startup(idea: str, fork_sim_code: str, sim_code: str, investment: floa roles.append(role) # init temp_storage - write_curr_sim_code({"sim_code": sim_code}) - write_curr_step({"step": reverie_meta.get("step", 0)}) + write_curr_sim_code({"sim_code": sim_code}, temp_storage_path) + write_curr_step({"step": reverie_meta.get("step", 0)}, temp_storage_path) await town.hire(roles) @@ -56,18 +59,33 @@ async def startup(idea: str, fork_sim_code: str, sim_code: str, investment: floa await town.run(n_round) -def main(idea: str, fork_sim_code: str, sim_code: str, investment: float = 30.0, n_round: int = 500): +def main( + idea: str, + fork_sim_code: str, + sim_code: str, + temp_storage_path: Optional[str] = None, + investment: float = 30.0, + n_round: int = 500, +): """ Args: idea: idea works as an `inner voice` to the first agent. fork_sim_code: old simulation name to start with sim_code: new simulation name to save simulation result + temp_storage_path: generative_agents storage path inside `environment/frontend_server` to investment: the investment of running agents n_round: rounds to run agents """ asyncio.run( - startup(idea=idea, fork_sim_code=fork_sim_code, sim_code=sim_code, investment=investment, n_round=n_round) + startup( + idea=idea, + fork_sim_code=fork_sim_code, + sim_code=sim_code, + temp_storage_path=temp_storage_path, + investment=investment, + n_round=n_round, + ) ) diff --git a/examples/st_game/stanford_town.py b/examples/st_game/stanford_town.py index c05a1287e..799113512 100644 --- a/examples/st_game/stanford_town.py +++ b/examples/st_game/stanford_town.py @@ -6,14 +6,13 @@ from typing import Any from pydantic import Field +from examples.st_game.roles.st_role import STRole +from examples.st_game.utils.const import MAZE_ASSET_PATH from metagpt.context import Context from metagpt.environment.stanford_town_env.stanford_town_env import StanfordTownEnv from metagpt.logs import logger from metagpt.team import Team -from examples.st_game.roles.st_role import STRole -from examples.st_game.utils.const import MAZE_ASSET_PATH - class StanfordTown(Team): env: StanfordTownEnv = Field(default=None) diff --git a/examples/st_game/tests/actions/test_gen_action_details.py b/examples/st_game/tests/actions/test_gen_action_details.py index a82478607..3edf9b116 100644 --- a/examples/st_game/tests/actions/test_gen_action_details.py +++ b/examples/st_game/tests/actions/test_gen_action_details.py @@ -31,7 +31,9 @@ async def test_gen_action_details(): act_desp = "sleeping" act_dura = "120" - access_tile = await role.rc.env.observe(EnvAPIAbstract(api_name="access_tile", kwargs={"tile": role.scratch.curr_tile})) + access_tile = await role.rc.env.observe( + EnvAPIAbstract(api_name="access_tile", kwargs={"tile": role.scratch.curr_tile}) + ) act_world = access_tile["world"] assert act_world == "the Ville" diff --git a/examples/st_game/tests/plan/test_conversation.py b/examples/st_game/tests/plan/test_conversation.py index caf2e69bd..3a158fc5f 100644 --- a/examples/st_game/tests/plan/test_conversation.py +++ b/examples/st_game/tests/plan/test_conversation.py @@ -3,11 +3,12 @@ # @Desc : unittest of roles conversation from typing import Tuple + import pytest from examples.st_game.plan.converse import agent_conversation from examples.st_game.roles.st_role import STRole -from examples.st_game.utils.const import STORAGE_PATH, MAZE_ASSET_PATH +from examples.st_game.utils.const import MAZE_ASSET_PATH, STORAGE_PATH from examples.st_game.utils.mg_ga_transform import get_reverie_meta from examples.st_game.utils.utils import copy_folder from metagpt.environment.stanford_town_env.stanford_town_env import StanfordTownEnv diff --git a/examples/st_game/tests/roles/test_st_role.py b/examples/st_game/tests/roles/test_st_role.py index 5efc19a93..c0844d10c 100644 --- a/examples/st_game/tests/roles/test_st_role.py +++ b/examples/st_game/tests/roles/test_st_role.py @@ -13,8 +13,9 @@ from metagpt.environment.stanford_town_env.stanford_town_env import StanfordTown @pytest.mark.asyncio async def test_observe(): role = STRole( - sim_code="base_the_ville_isabella_maria_klaus", start_time="February 13, 2023", - curr_time="February 13, 2023, 00:00:00" + sim_code="base_the_ville_isabella_maria_klaus", + start_time="February 13, 2023", + curr_time="February 13, 2023, 00:00:00", ) role.set_env(StanfordTownEnv(maze_asset_path=MAZE_ASSET_PATH)) await role.init_curr_tile() diff --git a/examples/st_game/tests/test_reflect.py b/examples/st_game/tests/test_reflect.py index 310104aee..6672b0545 100644 --- a/examples/st_game/tests/test_reflect.py +++ b/examples/st_game/tests/test_reflect.py @@ -9,8 +9,8 @@ from examples.st_game.actions.run_reflect_action import ( AgentFocusPt, AgentInsightAndGuidance, ) -from examples.st_game.utils.const import MAZE_ASSET_PATH from examples.st_game.roles.st_role import STRole +from examples.st_game.utils.const import MAZE_ASSET_PATH from metagpt.environment.stanford_town_env.stanford_town_env import StanfordTownEnv diff --git a/examples/st_game/utils/mg_ga_transform.py b/examples/st_game/utils/mg_ga_transform.py index a9e2e1f24..9ff57a3b1 100644 --- a/examples/st_game/utils/mg_ga_transform.py +++ b/examples/st_game/utils/mg_ga_transform.py @@ -2,6 +2,8 @@ # -*- coding: utf-8 -*- # @Desc : data transform of mg <-> ga under storage +from pathlib import Path +from typing import Optional from examples.st_game.utils.const import STORAGE_PATH, TEMP_STORAGE_PATH from metagpt.logs import logger @@ -53,9 +55,11 @@ def get_role_environment(sim_code: str, role_name: str, step: int = 0) -> dict: 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_sim_code(curr_sim_code: dict, temp_storage_path: Optional[Path] = None): + temp_storage_path = Path(temp_storage_path) or TEMP_STORAGE_PATH + 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) +def write_curr_step(curr_step: dict, temp_storage_path: Optional[Path] = None): + temp_storage_path = Path(temp_storage_path) or TEMP_STORAGE_PATH + write_json_file(temp_storage_path.joinpath("curr_step.json"), curr_step)