feat: remove Context.options

This commit is contained in:
莘权 马 2024-01-12 17:43:14 +08:00
parent 1e523f6840
commit b858cc7d83
3 changed files with 11 additions and 15 deletions

View file

@ -8,6 +8,7 @@
from typing import Optional
from metagpt.actions import Action
from metagpt.context import Context
from metagpt.logs import logger
@ -23,7 +24,7 @@ class WriteTeachingPlanPart(Action):
statement_patterns = TeachingPlanBlock.TOPIC_STATEMENTS.get(self.topic, [])
statements = []
for p in statement_patterns:
s = self.format_value(p, options=self.context.options)
s = self.format_value(p, context=self.context)
statements.append(s)
formatter = (
TeachingPlanBlock.PROMPT_TITLE_TEMPLATE
@ -67,13 +68,16 @@ class WriteTeachingPlanPart(Action):
return self.topic
@staticmethod
def format_value(value, options):
def format_value(value, context: Context):
"""Fill parameters inside `value` with `options`."""
if not isinstance(value, str):
return value
if "{" not in value:
return value
options = context.config.model_dump()
for k, v in context.kwargs:
options[k] = v # None value is allowed to override and disable the value from config.
opts = {k: v for k, v in options.items() if v is not None}
try:
return value.format(**opts)

View file

@ -64,14 +64,6 @@ class Context(BaseModel):
_llm: Optional[BaseLLM] = None
@property
def options(self):
"""Return all key-values"""
opts = self.config.model_dump()
for k, v in self.kwargs:
opts[k] = v # None value is allowed to override and disable the value from config.
return opts
def new_environ(self):
"""Return a new os.environ object"""
env = os.environ.copy()

View file

@ -31,11 +31,11 @@ class Teacher(Role):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.name = WriteTeachingPlanPart.format_value(self.name, self.context.options)
self.profile = WriteTeachingPlanPart.format_value(self.profile, self.context.options)
self.goal = WriteTeachingPlanPart.format_value(self.goal, self.context.options)
self.constraints = WriteTeachingPlanPart.format_value(self.constraints, self.context.options)
self.desc = WriteTeachingPlanPart.format_value(self.desc, self.context.options)
self.name = WriteTeachingPlanPart.format_value(self.name, self.context)
self.profile = WriteTeachingPlanPart.format_value(self.profile, self.context)
self.goal = WriteTeachingPlanPart.format_value(self.goal, self.context)
self.constraints = WriteTeachingPlanPart.format_value(self.constraints, self.context)
self.desc = WriteTeachingPlanPart.format_value(self.desc, self.context)
async def _think(self) -> bool:
"""Everything will be done part by part."""