Skip to content

🌟 The Multi-Agent Framework: First AI Software Company, Towards Natural Language Programming

simplify some ser&desr code

better62926ddddaadd8d
diff --git a/metagpt/actions/action.py b/metagpt/actions/action.py
index 499b5e794..8b28ffd8e 100644
--- a/metagpt/actions/action.py
+++ b/metagpt/actions/action.py
@@ -54,6 +54,12 @@ class Action(BaseModel):
super().__init_subclass__(**kwargs)
action_subclass_registry[cls.__name__] = cls
+ def dict(self, *args, **kwargs) -> "DictStrAny":
+ obj_dict = super(Action, self).dict(*args, **kwargs)
+ if "llm" in obj_dict:
+ obj_dict.pop("llm")
+ return obj_dict
+
def set_prefix(self, prefix, profile):
"""Set prefix for later usage"""
self.prefix = prefix
@@ -66,20 +72,6 @@ class Action(BaseModel):
def __repr__(self):
return self.__str__()
- def serialize(self):
- return {
- "action_class": self.__class__.__name__,
- "module_name": self.__module__,
- "name": self.name
- }
-
- @classmethod
- def deserialize(cls, action_dict: dict) -> "Action":
- action_class_str = action_dict.pop("action_class")
- module_name = action_dict.pop("module_name")
- action_class = import_class(action_class_str, module_name)
- return action_class(**action_dict)
-
@classmethod
def ser_class(cls) -> dict:
""" serialize class type"""
diff --git a/metagpt/roles/role.py b/metagpt/roles/role.py
index 6be800789..59b0f9cd6 100644
--- a/metagpt/roles/role.py
+++ b/metagpt/roles/role.py
@@ -119,17 +119,33 @@ class RoleContext(BaseModel):
memory: Memory = Field(default_factory=Memory)
# long_term_memory: LongTermMemory = Field(default_factory=LongTermMemory)
state: int = Field(default=-1) # -1 indicates initial or termination state where todo is None
- todo: Action = Field(default=None)
- watch: set[str] = Field(default_factory=set)
- news: list[Type[Message]] = Field(default=[])
- react_mode: RoleReactMode = (
- RoleReactMode.REACT
- ) # see `Role._set_react_mode` for definitions of the following two attributes
+ todo: Action = Field(default=None, exclude=True)
+ watch: set[Type[Action]] = Field(default_factory=set)
+ news: list[Type[Message]] = Field(default=[], exclude=True) # TODO not used
+ react_mode: RoleReactMode = RoleReactMode.REACT # see `Role._set_react_mode` for definitions of the following two attributes
max_react_loop: int = 1
class Config:
arbitrary_types_allowed = True
-
+
+ def __init__(self, **kwargs):
+ watch_info = kwargs.get("watch", set())
+ watch = set()
+ for item in watch_info:
+ action = Action.deser_class(item)
+ watch.update([action])
+ kwargs["watch"] = watch
+ super(RoleContext, self).__init__(**kwargs)
+
+ def dict(self, *args, **kwargs) -> "DictStrAny":
+ obj_dict = super(RoleContext, self).dict(*args, **kwargs)
+ watch = obj_dict.get("watch", set())
+ watch_info = []
+ for item in watch:
+ watch_info.append(item.ser_class())
+ obj_dict["watch"] = watch_info
+ return obj_dict
+
def check(self, role_id: str):
# if hasattr(CONFIG, "long_term_memory") and CONFIG.long_term_memory:
# self.long_term_memory.recover_memory(role_id, self)
@@ -290,7 +306,7 @@ class Role(BaseModel):
for idx, action in enumerate(actions):
if not isinstance(action, Action):
## 默认初始化
- i = action(name="", llm=self._llm)
+ i = action(llm=self._llm)
else:
if self._setting.is_human and not isinstance(action.llm, HumanProvider):
logger.warning(
@@ -386,9 +402,14 @@ class Role(BaseModel):
def _get_prefix(self):
"""Get the role prefix"""
- if self._setting.desc:
- return self._setting.desc
- return PREFIX_TEMPLATE.format(**self._setting.dict())
+ if self.desc:
+ return self.desc
+ return PREFIX_TEMPLATE.format(**{
+ "profile": self.profile,
+ "name": self.name,
+ "goal": self.goal,
+ "constraints": self.constraints
+ })
async def _think(self) -> None:
"""Think about what to do and decide on the next action"""
diff --git a/metagpt/schema.py b/metagpt/schema.py
index a872481bb..15dfb579c 100644
--- a/metagpt/schema.py
+++ b/metagpt/schema.py
@@ -64,7 +64,7 @@ class Document(BaseModel):
filename: str = ""
content: str = ""
- def get_meta(self) -> "Document"":
+ def get_meta(self) -> "Document":
"""Get metadata of the document.
:return: A new Document instance with the same root path and filename.
@@ -164,17 +164,6 @@ class Message(BaseModel):
def __repr__(self):
return self.__str__()
- # def dict(self):
- # return {
- # "content": self.content,
- # "instruct_content": self.instruct_content,
- # "role": self.role,
- # "cause_by": self.cause_by,
- # "sent_from": self.sent_from,
- # "send_to": self.send_to,
- # "restricted_to": self.restricted_to
- # }
-
def to_dict(self) -> dict:
"""Return a dict containing `role` and `content` for the LLM call.l"""
return {"role": self.role, "content": self.content}