add handle_exception to ensure robustness

This commit is contained in:
seehi 2024-06-05 23:26:09 +08:00
parent c78cddd102
commit d148a3217b
2 changed files with 15 additions and 2 deletions

View file

@ -10,6 +10,7 @@ from metagpt.exp_pool.manager import ExperienceManager, exp_manager
from metagpt.exp_pool.schema import Experience, Metric, QueryType, Score
from metagpt.exp_pool.scorers import ExperienceScorer, SimpleScorer
from metagpt.utils.async_helper import NestAsyncio
from metagpt.utils.exceptions import handle_exception
ReturnType = TypeVar("ReturnType")
@ -50,8 +51,7 @@ def exp_cache(
return exp
await handler.execute_function()
await handler.evaluate_experience()
handler.save_experience()
await handler.process_experience()
return handler._result
@ -87,6 +87,16 @@ class ExpCacheHandler(BaseModel):
"""Execute the function, and save the result."""
self._result = await self._execute_function()
@handle_exception
async def process_experience(self):
"""Process experience.
Evaluates and saves experience.
Use `handle_exception` to ensure robustness, do not stop subsequent operations.
"""
await self.evaluate_experience()
self.save_experience()
async def evaluate_experience(self):
"""Evaluate the experience, and save the score."""

View file

@ -8,6 +8,7 @@ from metagpt.config2 import Config, config
from metagpt.exp_pool.schema import MAX_SCORE, Experience, QueryType
from metagpt.rag.engines import SimpleEngine
from metagpt.rag.schema import ChromaRetrieverConfig, LLMRankerConfig
from metagpt.utils.exceptions import handle_exception
class ExperienceManager(BaseModel):
@ -34,6 +35,7 @@ class ExperienceManager(BaseModel):
)
return self
@handle_exception
def create_exp(self, exp: Experience):
"""Adds an experience to the storage if writing is enabled.
@ -45,6 +47,7 @@ class ExperienceManager(BaseModel):
self.storage.add_objs([exp])
@handle_exception(default_return=[])
async def query_exps(self, req: str, tag: str = "", query_type: QueryType = QueryType.SEMANTIC) -> list[Experience]:
"""Retrieves and filters experiences.