3
# @Desc : the implement of serialization and deserialization
8
from metagpt.utils.common import import_class
11
def actionoutout_schema_to_mapping(schema: dict) -> dict:
13
directly traverse the `properties` in the first level.
14
schema structure likes
20
"Original Requirements":{
21
"title":"Original Requirements",
26
"Original Requirements",
32
for field, property in schema["properties"].items():
33
if property["type"] == "string":
34
mapping[field] = (str, ...)
35
elif property["type"] == "array" and property["items"]["type"] == "string":
36
mapping[field] = (list[str], ...)
37
elif property["type"] == "array" and property["items"]["type"] == "array":
38
# here only consider the `list[list[str]]` situation
39
mapping[field] = (list[list[str]], ...)
43
def actionoutput_mapping_to_str(mapping: dict) -> dict:
45
for key, value in mapping.items():
46
new_mapping[key] = str(value)
50
def actionoutput_str_to_mapping(mapping: dict) -> dict:
52
for key, value in mapping.items():
53
if value == "(<class 'str'>, Ellipsis)":
54
new_mapping[key] = (str, ...)
56
new_mapping[key] = eval(value) # `"'(list[str], Ellipsis)"` to `(list[str], ...)`
60
def serialize_message(message: "Message"):
61
message_cp = copy.deepcopy(message) # avoid `instruct_content` value update by reference
62
ic = message_cp.instruct_content
64
# model create by pydantic create_model like `pydantic.main.prd`, can't pickle.dump directly
65
schema = ic.model_json_schema()
66
mapping = actionoutout_schema_to_mapping(schema)
68
message_cp.instruct_content = {"class": schema["title"], "mapping": mapping, "value": ic.model_dump()}
69
msg_ser = pickle.dumps(message_cp)
74
def deserialize_message(message_ser: str) -> "Message":
75
message = pickle.loads(message_ser)
76
if message.instruct_content:
77
ic = message.instruct_content
78
actionnode_class = import_class("ActionNode", "metagpt.actions.action_node") # avoid circular import
79
ic_obj = actionnode_class.create_model_class(class_name=ic["class"], mapping=ic["mapping"])
80
ic_new = ic_obj(**ic["value"])
81
message.instruct_content = ic_new