mirror of
https://github.com/FoundationAgents/MetaGPT.git
synced 2026-06-26 15:49:42 +02:00
add general environment and refactor environment's w/r funcs
This commit is contained in:
parent
cb9793e69b
commit
6ebe8efc63
21 changed files with 252 additions and 147 deletions
3
metagpt/environment/__init__.py
Normal file
3
metagpt/environment/__init__.py
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
# @Desc :
|
||||
|
|
@ -17,8 +17,8 @@ from metagpt.schema import Message
|
|||
|
||||
class Environment(BaseModel):
|
||||
"""环境,承载一批角色,角色可以向环境发布消息,可以被其他角色观察到
|
||||
Environment, hosting a batch of roles, roles can publish messages to the environment, and can be observed by other roles
|
||||
|
||||
Environment, hosting a batch of roles, roles can publish messages to the environment,
|
||||
and can be observed by other roles
|
||||
"""
|
||||
|
||||
roles: dict[str, Role] = Field(default_factory=dict)
|
||||
32
metagpt/environment/general_environment.py
Normal file
32
metagpt/environment/general_environment.py
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
# @Desc :
|
||||
|
||||
from typing import Callable
|
||||
|
||||
from pydantic import Field
|
||||
|
||||
from metagpt.environment.environment import Environment
|
||||
|
||||
|
||||
class GeneralEnvironment(Environment):
|
||||
"""
|
||||
A GeneralEnvironment for interfacing with games, etc. It create a registration mechanism to register
|
||||
custom methods when operating with the particular environment.
|
||||
"""
|
||||
name: str = Field(default="")
|
||||
registered_funcs: dict[str, Callable] = Field(default={})
|
||||
|
||||
def register_func(self, func_name: str, func: Callable):
|
||||
if func_name not in self.registered_funcs:
|
||||
self.registered_funcs[func_name] = func
|
||||
|
||||
def call_func(self, func_name: str, *args, **kwargs):
|
||||
assert func_name in self.registered_funcs
|
||||
|
||||
func = self.registered_funcs.get(func_name)
|
||||
return func(*args, **kwargs)
|
||||
|
||||
@staticmethod
|
||||
def init_register_funcs(self):
|
||||
raise NotImplementedError()
|
||||
21
metagpt/environment/gym_environment.py
Normal file
21
metagpt/environment/gym_environment.py
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
# @Desc : RL environment about Gymnasium(forked from openai gym)
|
||||
|
||||
from typing import Callable
|
||||
|
||||
import gymnasium as gym
|
||||
|
||||
from metagpt.logs import logger
|
||||
from metagpt.environment.general_environment import GeneralEnvironment
|
||||
|
||||
|
||||
class GymEnvironment(GeneralEnvironment):
|
||||
|
||||
def init_register_funcs(self):
|
||||
env = gym.make(self.name)
|
||||
logger.info(f"init gym environment: {self.name}")
|
||||
self.register_func("reset", env.reset)
|
||||
self.register_func("sample_action", env.action_space.sample)
|
||||
self.register_func("step", env.step)
|
||||
self.register_func("close", env.close)
|
||||
|
|
@ -11,7 +11,6 @@ from typing import Iterable, Type
|
|||
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
# from metagpt.environment import Environment
|
||||
from metagpt.config import CONFIG
|
||||
from metagpt.actions import Action, ActionOutput
|
||||
from metagpt.llm import LLM
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ from pydantic import BaseModel, Field
|
|||
|
||||
from metagpt.actions import BossRequirement
|
||||
from metagpt.config import CONFIG
|
||||
from metagpt.environment import Environment
|
||||
from metagpt.environment.environment import Environment
|
||||
from metagpt.logs import logger
|
||||
from metagpt.roles import Role
|
||||
from metagpt.schema import Message
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue