serialize mgxenv

This commit is contained in:
seehi 2024-08-09 10:31:03 +08:00
parent 5f86247c0d
commit 98ac5fbce3
10 changed files with 185 additions and 30 deletions

View file

@ -28,6 +28,7 @@ import time
import traceback
from asyncio import iscoroutinefunction
from datetime import datetime
from functools import partial
from io import BytesIO
from pathlib import Path
from typing import Any, Callable, Dict, List, Literal, Optional, Tuple, Union
@ -577,13 +578,30 @@ def read_json_file(json_file: str, encoding="utf-8") -> list[Any]:
return data
def write_json_file(json_file: str, data: list, encoding: str = None, indent: int = 4):
def write_json_file(json_file: str, data: Any, encoding: str = None, indent: int = 4, use_fallback: bool = False):
folder_path = Path(json_file).parent
if not folder_path.exists():
folder_path.mkdir(parents=True, exist_ok=True)
# For debug, if use_fallback, unknown values will be logged instead of raising an exception.
def fallback(x: Any) -> str:
tip = f"PydanticSerializationError occurred while processing file '{json_file}'"
if inspect.ismethod(x):
logger.error(f"{tip}, Method: {x.__self__.__class__.__name__}.{x.__func__.__name__}")
elif inspect.isfunction(x):
logger.error(f"{tip}, Function: {x.__name__}")
elif hasattr(x, "__class__"):
logger.error(f"{tip}, Instance of: {x.__class__.__name__}")
elif hasattr(x, "__name__"):
logger.error(f"{tip}, Class or module: {x.__name__}")
else:
logger.error(f"{tip}, Unknown type: {type(x)}")
custom_default = partial(to_jsonable_python, fallback=fallback if use_fallback else None)
with open(json_file, "w", encoding=encoding) as fout:
json.dump(data, fout, ensure_ascii=False, indent=indent, default=to_jsonable_python)
json.dump(data, fout, ensure_ascii=False, indent=indent, default=custom_default)
def read_csv_to_list(curr_file: str, header=False, strip_trail=True):

View file

@ -4,8 +4,11 @@
import copy
import pickle
from typing import Callable, Optional, Type
from metagpt.utils.common import import_class
from pydantic import BaseModel
from metagpt.utils.common import import_class, read_json_file, write_json_file
def actionoutout_schema_to_mapping(schema: dict) -> dict:
@ -81,3 +84,36 @@ def deserialize_message(message_ser: str) -> "Message":
message.instruct_content = ic_new
return message
def serialize_model(model: BaseModel, file_path: str, remove_unserializable: Optional[Callable[[dict], None]] = None):
"""Serializes a Pydantic model to a JSON file.
Args:
model (BaseModel): The Pydantic model to serialize.
file_path (str): The path to the JSON file where the model will be saved.
remove_unserializable (Optional[Callable[[dict], None]]): Optional function to remove unserializable content from the serialized data.
"""
serialized_data = model.model_dump()
if remove_unserializable:
remove_unserializable(serialized_data)
write_json_file(file_path, serialized_data)
def deserialize_model(cls: Type[BaseModel], file_path: str) -> BaseModel:
"""Deserializes a JSON file to a Pydantic model.
Args:
cls (Type[BaseModel]): The Pydantic model class to deserialize into.
file_path (str): The path to the JSON file to read from.
Returns:
BaseModel: An instance of the Pydantic model.
"""
data: dict = read_json_file(file_path)
return cls(**data)