Merge pull request #771 from iorisa/feature/context

feat: replace CONTEXT with local context
This commit is contained in:
geekan 2024-01-18 19:17:32 +08:00 committed by GitHub
commit 16ec0ccc4c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
68 changed files with 354 additions and 356 deletions

View file

@ -47,7 +47,7 @@ WRITE ONLY ONE WORD, Engineer OR QaEngineer OR NoOne, IN THIS SECTION.
You should fill in necessary instruction, status, send to, and finally return all content between the --- segment line.
"""
CONTEXT = """
TEMPLATE_CONTEXT = """
## Development Code File Name
{code_file_name}
## Development Code
@ -130,7 +130,7 @@ class RunCode(Action):
logger.info(f"{outs=}")
logger.info(f"{errs=}")
context = CONTEXT.format(
context = TEMPLATE_CONTEXT.format(
code=self.i_context.code,
code_file_name=self.i_context.code_filename,
test_code=self.i_context.test_code,

View file

@ -84,7 +84,10 @@ class Config(CLIParams, YamlModel):
@classmethod
def from_home(cls, path):
"""Load config from ~/.metagpt/config.yaml"""
return Config.from_yaml_file(CONFIG_ROOT / path)
pathname = CONFIG_ROOT / path
if not pathname.exists():
return None
return Config.from_yaml_file(pathname)
@classmethod
def default(cls):

View file

@ -13,7 +13,7 @@ import aiofiles
import yaml
from pydantic import BaseModel, Field
from metagpt.context import CONTEXT
from metagpt.context import CONTEXT, Context
class Example(BaseModel):
@ -73,14 +73,14 @@ class SkillsDeclaration(BaseModel):
skill_data = yaml.safe_load(data)
return SkillsDeclaration(**skill_data)
def get_skill_list(self, entity_name: str = "Assistant") -> Dict:
def get_skill_list(self, entity_name: str = "Assistant", context: Context = CONTEXT) -> Dict:
"""Return the skill name based on the skill description."""
entity = self.entities.get(entity_name)
if not entity:
return {}
# List of skills that the agent chooses to activate.
agent_skills = CONTEXT.kwargs.agent_skills
agent_skills = context.kwargs.agent_skills
if not agent_skills:
return {}

View file

@ -84,7 +84,7 @@ class FireworksLLM(OpenAILLM):
def _update_costs(self, usage: CompletionUsage):
if self.config.calc_usage and usage:
try:
# use FireworksCostManager not CONTEXT.cost_manager
# use FireworksCostManager not context.cost_manager
self.cost_manager.update_cost(usage.prompt_tokens, usage.completion_tokens, self.model)
except Exception as e:
logger.error(f"updating costs failed!, exp: {e}")

View file

@ -48,7 +48,8 @@ class Assistant(Role):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.constraints = self.constraints.format(language=kwargs.get("language") or CONTEXT.kwargs.language)
language = kwargs.get("language") or self.context.kwargs.language or CONTEXT.kwargs.language
self.constraints = self.constraints.format(language=language)
async def think(self) -> bool:
"""Everything will be done part by part."""
@ -56,11 +57,11 @@ class Assistant(Role):
if not last_talk:
return False
if not self.skills:
skill_path = Path(CONTEXT.kwargs.SKILL_PATH) if CONTEXT.kwargs.SKILL_PATH else None
skill_path = Path(self.context.kwargs.SKILL_PATH) if self.context.kwargs.SKILL_PATH else None
self.skills = await SkillsDeclaration.load(skill_yaml_file_name=skill_path)
prompt = ""
skills = self.skills.get_skill_list()
skills = self.skills.get_skill_list(context=self.context)
for desc, name in skills.items():
prompt += f"If the text explicitly want you to {desc}, return `[SKILL]: {name}` brief and clear. For instance: [SKILL]: {name}\n"
prompt += 'Otherwise, return `[TALK]: {talk}` brief and clear. For instance: if {talk} is "xxxx" return [TALK]: xxxx\n\n'

View file

@ -261,7 +261,7 @@ class Role(SerializationMixin, ContextMixin, BaseModel):
self._reset()
for action in actions:
if not isinstance(action, Action):
i = action()
i = action(context=self.context)
else:
if self.is_human and not isinstance(action.llm, HumanProvider):
logger.warning(

View file

@ -201,7 +201,10 @@ class GitRepository:
new_path = self.workdir.parent / new_dir_name
if new_path.exists():
logger.info(f"Delete directory {str(new_path)}")
shutil.rmtree(new_path)
try:
shutil.rmtree(new_path)
except Exception as e:
logger.warning(f"rm {str(new_path)} error: {e}")
if new_path.exists(): # Recheck for windows os
logger.warning(f"Failed to delete directory {str(new_path)}")
return