init st_game structure

This commit is contained in:
better629 2023-09-25 16:10:29 +08:00
parent 7da710eaaf
commit 075e250f14
17 changed files with 188 additions and 0 deletions

View file

@ -0,0 +1,5 @@
## Stanford Town Game
执行入口为:`python3 run_st_game.py "Host a activity"`
`idea`为用户给第一个Agent的用户心声并通过这个心声进行传播看最后多智能体是否达到举办、参加活动的目标。

View file

@ -0,0 +1,12 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Desc : summarize the content of agents' conversation
from metagpt.actions.action import Action
from metagpt.schema import Message
class SummarizeConv(Action):
def __init__(self, name="SummarizeConv", context: list[Message] = None, llm=None):
super().__init__(name, context, llm)

View file

@ -0,0 +1,11 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Desc :
from metagpt.actions import Action
class UserRequirement(Action):
async def run(self, *args, **kwargs):
raise NotImplementedError

View file

@ -0,0 +1,10 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Desc : maze environment
from metagpt.environment import Environment
class MazeEnvironment(Environment):
pass

View file

@ -0,0 +1,9 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Desc : associative_memory to store conversation、plan detail、reflection result and so on.
from metagpt.memory.memory import Memory
class AssociativeMemory(Memory):
pass

View file

@ -0,0 +1,3 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Desc : st' planning execution

View file

@ -0,0 +1,3 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Desc : st's reflection execution

View file

@ -0,0 +1,44 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Desc : Stanford Town role
"""
Do the steps following:
- perceive, receive environment(Maze) info
- retrieve, retrieve memories
- plan, do plan like long-term plan and interact with Maze
- reflect, do the High-level thinking based on memories and re-add into the memory
- execute, move or else in the Maze
"""
from pydantic import Field
from pathlib import Path
from metagpt.roles.role import Role, RoleContext
from ..memory.associative_memory import AssociativeMemory
class STRoleContext(RoleContext):
memory: AssociativeMemory = Field(default=AssociativeMemory)
class STRole(Role):
# add a role's property structure to store role's age and so on like GA's Scratch.
def __init__(self, name="", profile=""):
self._rc = STRoleContext()
def load_from(self, folder: Path):
"""
load role data from `storage/{simulation_name}/personas/{role_name}
"""
pass
def save_into(self, folder: Path):
"""
save role data from `storage/{simulation_name}/personas/{role_name}
"""
pass

View file

@ -0,0 +1,42 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Desc : entry of Stanford Town(ST/st) game
import asyncio
import fire
from stanford_town import StanfordTown
from roles.st_role import STRole
async def startup(idea: str,
investment: float = 30.0,
n_round: int = 500):
# get role names from `storage/{simulation_name}/reverie/meta.json` and then init roles
roles = []
# TODO
town = StanfordTown()
town.wakeup_roles(roles)
town.invest(investment)
town.start_project()
await town.run(n_round)
def main(idea: str,
investment: float = 30.0,
n_round: int = 500):
"""
idea works as an `inner voice` to the first agent.
"""
asyncio.run(startup(idea=idea,
investment=investment,
n_round=n_round))
if __name__ == "__main__":
fire.Fire(main)

View file

@ -0,0 +1,25 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Desc : StanfordTown to works like SoftwareCompany
from pydantic import Field
from metagpt.software_company import SoftwareCompany
from metagpt.roles.role import Role
from metagpt.schema import Message
from maze_environment import MazeEnvironment
from actions.user_requirement import UserRequirement
class StanfordTown(SoftwareCompany):
environment: MazeEnvironment = Field(default_factory=MazeEnvironment)
def wakeup_roles(self, roles: list[Role]):
self.environment.add_roles(roles)
def start_project(self, idea):
self.environment.publish_message(
Message(role="User", content=idea, cause_by=UserRequirement)
)

1
examples/st_game/storage/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
# path to store simulation data

View file

@ -0,0 +1,8 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Desc :
from pathlib import Path
ROOT_PATH = Path(__file__).parent.parent
STORAGE_PATH = ROOT_PATH.joinpath("storage")

View file

@ -0,0 +1,3 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Desc : data transform of mg <-> ga under storage

3
metagpt/plan/__init__.py Normal file
View file

@ -0,0 +1,3 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Desc : plan module

3
metagpt/plan/plan.py Normal file
View file

@ -0,0 +1,3 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Desc : base class of planning

View file

@ -0,0 +1,3 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Desc : reflection module

View file

@ -0,0 +1,3 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Desc : base class of reflection