From a2bb67a1f0cf829e3856a1a128edb03fd25377d5 Mon Sep 17 00:00:00 2001 From: seehi <6580@pm.me> Date: Mon, 8 Jul 2024 15:24:18 +0800 Subject: [PATCH] move the req_serialize from di/role_zero.py to context_builders/role_zero.py --- .../exp_pool/context_builders/role_zero.py | 24 +++++++++++++++++ metagpt/exp_pool/decorator.py | 1 - metagpt/exp_pool/manager.py | 1 + metagpt/roles/di/role_zero.py | 27 +++---------------- 4 files changed, 28 insertions(+), 25 deletions(-) diff --git a/metagpt/exp_pool/context_builders/role_zero.py b/metagpt/exp_pool/context_builders/role_zero.py index 60f71ef59..e9ab83d90 100644 --- a/metagpt/exp_pool/context_builders/role_zero.py +++ b/metagpt/exp_pool/context_builders/role_zero.py @@ -1,4 +1,6 @@ """RoleZero context builder.""" +import copy +import json from metagpt.exp_pool.context_builders.base import BaseContextBuilder @@ -24,3 +26,25 @@ class RoleZeroContextBuilder(BaseContextBuilder): def replace_example_content(self, text: str, new_example_content: str) -> str: return self.replace_content_between_markers(text, "# Example", "# Instruction", new_example_content) + + @staticmethod + def req_serialize(req: list[dict]) -> str: + """Serialize the request for database storage, ensuring it is a string. + + This function deep copies the request and modifies the content of the last element + to remove unnecessary sections, making the request more concise. + """ + + req_copy = copy.deepcopy(req) + + last_content = req_copy[-1]["content"] + last_content = RoleZeroContextBuilder.replace_content_between_markers( + last_content, "# Data Structure", "# Current Plan", "" + ) + last_content = RoleZeroContextBuilder.replace_content_between_markers( + last_content, "# Example", "# Instruction", "" + ) + + req_copy[-1]["content"] = last_content + + return json.dumps(req_copy) diff --git a/metagpt/exp_pool/decorator.py b/metagpt/exp_pool/decorator.py index c518bb7ea..10f3355f9 100644 --- a/metagpt/exp_pool/decorator.py +++ b/metagpt/exp_pool/decorator.py @@ -56,7 +56,6 @@ def exp_cache( @functools.wraps(func) async def get_or_create(args: Any, kwargs: Any) -> ReturnType: - logger.info("exp_cache is enabled.") handler = ExpCacheHandler( func=func, args=args, diff --git a/metagpt/exp_pool/manager.py b/metagpt/exp_pool/manager.py index 276b1e8e3..23198eb02 100644 --- a/metagpt/exp_pool/manager.py +++ b/metagpt/exp_pool/manager.py @@ -49,6 +49,7 @@ class ExperienceManager(BaseModel): self.init_exp_pool() + logger.debug(f"exp_pool config: {self.config.exp_pool}") return self @handle_exception diff --git a/metagpt/roles/di/role_zero.py b/metagpt/roles/di/role_zero.py index 2c2c81551..103a77911 100644 --- a/metagpt/roles/di/role_zero.py +++ b/metagpt/roles/di/role_zero.py @@ -1,6 +1,5 @@ from __future__ import annotations -import copy import inspect import json import re @@ -166,32 +165,12 @@ class RoleZero(Role): return True - @exp_cache(context_builder=RoleZeroContextBuilder(), req_serialize=lambda req: RoleZero._req_serialize(req)) + @exp_cache( + context_builder=RoleZeroContextBuilder(), req_serialize=lambda req: RoleZeroContextBuilder.req_serialize(req) + ) async def llm_cached_aask(self, *, req: list[dict], system_msgs: list[str]) -> str: return await self.llm.aask(req, system_msgs=system_msgs) - @staticmethod - def _req_serialize(req: list[dict]) -> str: - """Serialize the request for database storage, ensuring it is a string. - - This function deep copies the request and modifies the content of the last element - to remove unnecessary sections, making the request more concise. - """ - - req_copy = copy.deepcopy(req) - - last_content = req_copy[-1]["content"] - last_content = RoleZeroContextBuilder.replace_content_between_markers( - last_content, "# Data Structure", "# Current Plan", "" - ) - last_content = RoleZeroContextBuilder.replace_content_between_markers( - last_content, "# Example", "# Instruction", "" - ) - - req_copy[-1]["content"] = last_content - - return json.dumps(req_copy) - async def _act(self) -> Message: if self.use_fixed_sop: return await super()._act()