mirror of
https://github.com/FoundationAgents/MetaGPT.git
synced 2026-06-08 15:05:17 +02:00
fix role and format ut of serialize_deserialize
This commit is contained in:
parent
f2aa9f7046
commit
39e4aa98ab
9 changed files with 27 additions and 27 deletions
|
|
@ -6,16 +6,11 @@
|
|||
@File : role.py
|
||||
"""
|
||||
|
||||
import sys
|
||||
from enum import Enum
|
||||
import importlib
|
||||
from pathlib import Path
|
||||
from __future__ import annotations
|
||||
|
||||
from types import SimpleNamespace
|
||||
from typing import (
|
||||
Dict,
|
||||
Optional,
|
||||
Union,
|
||||
Iterable,
|
||||
Type
|
||||
)
|
||||
|
|
@ -30,6 +25,7 @@ from metagpt.llm import LLM
|
|||
from metagpt.logs import logger
|
||||
from metagpt.memory import Memory, LongTermMemory
|
||||
from metagpt.schema import Message
|
||||
from metagpt.provider.human_provider import HumanProvider
|
||||
from metagpt.utils.utils import read_json_file, write_json_file, import_class
|
||||
|
||||
PREFIX_TEMPLATE = """You are a {profile}, named {name}, your goal is {goal}, and the constraint is {constraints}. """
|
||||
|
|
@ -133,11 +129,11 @@ class Role(BaseModel):
|
|||
_rc: RoleContext = RoleContext()
|
||||
|
||||
_private_attributes = {
|
||||
"_setting': _setting,
|
||||
"_role_id': _role_id,
|
||||
'_states': [],
|
||||
'_actions': [],
|
||||
'_actions_type': [] # 用于记录和序列化
|
||||
"_setting": _setting,
|
||||
"_role_id": _role_id,
|
||||
"_states": [],
|
||||
"_actions": [],
|
||||
"_actions_type": [] # 用于记录和序列化
|
||||
}
|
||||
|
||||
class Config:
|
||||
|
|
@ -162,17 +158,6 @@ class Role(BaseModel):
|
|||
object.__setattr__(self, '_states', [])
|
||||
object.__setattr__(self, '_actions', [])
|
||||
|
||||
@staticmethod
|
||||
def _process_class(class_str, module_name):
|
||||
cleaned_string = re.sub(r"[<>']", "", class_str).replace("class ", "")
|
||||
package_name = "metagpt"
|
||||
file_name = cleaned_string.replace(package_name, "").replace("." + module_name, "")
|
||||
print(file_name)
|
||||
# print("\n", sys.modules)
|
||||
module_file = import_module(file_name, package=package_name)
|
||||
module = getattr(module_file, module_name)
|
||||
return module
|
||||
|
||||
def serialize(self, stg_path: Path):
|
||||
role_info_path = stg_path.joinpath("role_info.json")
|
||||
role_info = {
|
||||
|
|
|
|||
|
|
@ -7,12 +7,14 @@ import pytest
|
|||
from metagpt.actions import Action
|
||||
from metagpt.llm import LLM
|
||||
|
||||
|
||||
def test_action_serialize():
|
||||
action = Action()
|
||||
ser_action_dict = action.dict()
|
||||
assert "name" in ser_action_dict
|
||||
assert "llm" in ser_action_dict
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_action_deserialize():
|
||||
action = Action()
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ import pytest
|
|||
from metagpt.roles.architect import Architect
|
||||
from metagpt.actions.action import Action
|
||||
|
||||
|
||||
def test_architect_serialize():
|
||||
role = Architect()
|
||||
ser_role_dict = role.dict(by_alias=True)
|
||||
|
|
@ -14,6 +15,7 @@ def test_architect_serialize():
|
|||
assert "_states" in ser_role_dict
|
||||
assert "_actions" in ser_role_dict
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_architect_deserialize():
|
||||
role = Architect()
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ from metagpt.roles.product_manager import ProductManager
|
|||
from metagpt.actions.action import Action
|
||||
from metagpt.schema import Message
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_product_manager_deserialize():
|
||||
role = ProductManager()
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ import pytest
|
|||
from metagpt.roles.project_manager import ProjectManager
|
||||
from metagpt.actions.action import Action
|
||||
|
||||
|
||||
def test_project_manager_serialize():
|
||||
role = ProjectManager()
|
||||
ser_role_dict = role.dict(by_alias=True)
|
||||
|
|
@ -14,6 +15,7 @@ def test_project_manager_serialize():
|
|||
assert "_states" in ser_role_dict
|
||||
assert "_actions" in ser_role_dict
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_project_manager_deserialize():
|
||||
role = ProjectManager()
|
||||
|
|
|
|||
|
|
@ -34,7 +34,7 @@ async def test_engineer_deserialize():
|
|||
# also can be deserialized in this way:
|
||||
new_role = Engineer(**ser_role_dict)
|
||||
assert new_role.name == "Alex"
|
||||
assert new_role.use_code_review == True
|
||||
assert new_role.use_code_review is True
|
||||
assert len(new_role._actions) == 2
|
||||
assert isinstance(new_role._actions[0], Action)
|
||||
assert isinstance(new_role._actions[1], Action)
|
||||
|
|
|
|||
|
|
@ -24,5 +24,5 @@ async def test_action_deserialize():
|
|||
# new_action = WritePRD().deserialize(serialized_data)
|
||||
assert new_action.name == ""
|
||||
assert new_action.llm == LLM()
|
||||
assert len(await new_action.run([Message(content="write a cli snake game")]))>0
|
||||
|
||||
assert len(await new_action.run([Message(content="write a cli snake game")])) > 0
|
||||
|
||||
|
|
|
|||
|
|
@ -7,18 +7,21 @@ import pytest
|
|||
from metagpt.actions import WriteCode, WriteCodeReview
|
||||
from metagpt.llm import LLM
|
||||
|
||||
|
||||
def test_write_design_serialize():
|
||||
action = WriteCode()
|
||||
ser_action_dict = action.dict()
|
||||
assert ser_action_dict["name"] == "WriteCode"
|
||||
assert "llm" in ser_action_dict
|
||||
|
||||
|
||||
def test_write_task_serialize():
|
||||
action = WriteCodeReview()
|
||||
ser_action_dict = action.dict()
|
||||
assert ser_action_dict["name"] == "WriteCodeReview"
|
||||
assert "llm" in ser_action_dict
|
||||
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_write_code_deserialize():
|
||||
action = WriteCode()
|
||||
|
|
@ -29,6 +32,7 @@ async def test_write_code_deserialize():
|
|||
assert new_action.llm == LLM()
|
||||
await new_action.run(context="write a cli snake game", filename="test_code")
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_write_code_review_deserialize():
|
||||
action = WriteCodeReview()
|
||||
|
|
|
|||
|
|
@ -7,18 +7,21 @@ import pytest
|
|||
from metagpt.actions import WriteDesign, WriteTasks
|
||||
from metagpt.llm import LLM
|
||||
|
||||
|
||||
def test_write_design_serialize():
|
||||
action = WriteDesign()
|
||||
ser_action_dict = action.dict()
|
||||
assert "name" in ser_action_dict
|
||||
assert "llm" in ser_action_dict
|
||||
|
||||
|
||||
def test_write_task_serialize():
|
||||
action = WriteTasks()
|
||||
ser_action_dict = action.dict()
|
||||
assert "name" in ser_action_dict
|
||||
assert "llm" in ser_action_dict
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_write_design_deserialize():
|
||||
action = WriteDesign()
|
||||
|
|
@ -28,6 +31,7 @@ async def test_write_design_deserialize():
|
|||
assert new_action.llm == LLM()
|
||||
await new_action.run(context="write a cli snake game")
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_write_task_deserialize():
|
||||
action = WriteTasks()
|
||||
|
|
@ -36,4 +40,4 @@ async def test_write_task_deserialize():
|
|||
# new_action = WriteTasks().deserialize(serialized_data)
|
||||
assert new_action.name == "CreateTasks"
|
||||
assert new_action.llm == LLM()
|
||||
await new_action.run(context="write a cli snake game")
|
||||
await new_action.run(context="write a cli snake game")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue