mirror of
https://github.com/FoundationAgents/MetaGPT.git
synced 2026-05-24 14:15:17 +02:00
move the req_serialize from di/role_zero.py to context_builders/role_zero.py
This commit is contained in:
parent
361294d31d
commit
a2bb67a1f0
4 changed files with 28 additions and 25 deletions
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue