mirror of
https://github.com/FoundationAgents/MetaGPT.git
synced 2026-06-08 15:05:17 +02:00
refine code
This commit is contained in:
parent
decde3290b
commit
97ee2f0c98
8 changed files with 112 additions and 91 deletions
Binary file not shown.
|
|
@ -13,7 +13,7 @@ from typing import Optional, Union
|
|||
from pydantic import BaseModel, ConfigDict, Field, model_validator
|
||||
|
||||
from metagpt.actions.action_node import ActionNode
|
||||
from metagpt.context import ContextMixin
|
||||
from metagpt.context_mixin import ContextMixin
|
||||
from metagpt.schema import (
|
||||
CodeSummarizeContext,
|
||||
CodingContext,
|
||||
|
|
|
|||
|
|
@ -94,84 +94,5 @@ class Context(BaseModel):
|
|||
return llm
|
||||
|
||||
|
||||
class ContextMixin(BaseModel):
|
||||
"""Mixin class for context and config"""
|
||||
|
||||
# Env/Role/Action will use this context as private context, or use self.context as public context
|
||||
_context: Optional[Context] = None
|
||||
# Env/Role/Action will use this config as private config, or use self.context.config as public config
|
||||
_config: Optional[Config] = None
|
||||
|
||||
# Env/Role/Action will use this llm as private llm, or use self.context._llm instance
|
||||
_llm: Optional[BaseLLM] = None
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
context: Optional[Context] = None,
|
||||
config: Optional[Config] = None,
|
||||
llm: Optional[BaseLLM] = None,
|
||||
**kwargs,
|
||||
):
|
||||
"""Initialize with config"""
|
||||
super().__init__(**kwargs)
|
||||
self.set_context(context)
|
||||
self.set_config(config)
|
||||
self.set_llm(llm)
|
||||
|
||||
def set(self, k, v, override=False):
|
||||
"""Set attribute"""
|
||||
if override or not self.__dict__.get(k):
|
||||
self.__dict__[k] = v
|
||||
|
||||
def set_context(self, context: Context, override=True):
|
||||
"""Set context"""
|
||||
self.set("_context", context, override)
|
||||
|
||||
def set_config(self, config: Config, override=False):
|
||||
"""Set config"""
|
||||
self.set("_config", config, override)
|
||||
|
||||
def set_llm(self, llm: BaseLLM, override=False):
|
||||
"""Set llm"""
|
||||
self.set("_llm", llm, override)
|
||||
|
||||
@property
|
||||
def config(self) -> Config:
|
||||
"""Role config: role config > context config"""
|
||||
if self._config:
|
||||
return self._config
|
||||
return self.context.config
|
||||
|
||||
@config.setter
|
||||
def config(self, config: Config) -> None:
|
||||
"""Set config"""
|
||||
self.set_config(config)
|
||||
|
||||
@property
|
||||
def context(self) -> Context:
|
||||
"""Role context: role context > context"""
|
||||
if self._context:
|
||||
return self._context
|
||||
return CONTEXT
|
||||
|
||||
@context.setter
|
||||
def context(self, context: Context) -> None:
|
||||
"""Set context"""
|
||||
self.set_context(context)
|
||||
|
||||
@property
|
||||
def llm(self) -> BaseLLM:
|
||||
"""Role llm: if not existed, init from role.config"""
|
||||
# print(f"class:{self.__class__.__name__}({self.name}), llm: {self._llm}, llm_config: {self._llm_config}")
|
||||
if not self._llm:
|
||||
self._llm = self.context.llm_with_cost_manager_from_llm_config(self.config.llm)
|
||||
return self._llm
|
||||
|
||||
@llm.setter
|
||||
def llm(self, llm: BaseLLM) -> None:
|
||||
"""Set llm"""
|
||||
self._llm = llm
|
||||
|
||||
|
||||
# Global context, not in Env
|
||||
CONTEXT = Context()
|
||||
|
|
|
|||
95
metagpt/context_mixin.py
Normal file
95
metagpt/context_mixin.py
Normal file
|
|
@ -0,0 +1,95 @@
|
|||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
@Time : 2024/1/11 17:25
|
||||
@Author : alexanderwu
|
||||
@File : context_mixin.py
|
||||
"""
|
||||
from typing import Optional
|
||||
|
||||
from pydantic import BaseModel, ConfigDict, Field
|
||||
|
||||
from metagpt.config2 import Config
|
||||
from metagpt.context import CONTEXT, Context
|
||||
from metagpt.provider.base_llm import BaseLLM
|
||||
|
||||
|
||||
class ContextMixin(BaseModel):
|
||||
"""Mixin class for context and config"""
|
||||
|
||||
model_config = ConfigDict(arbitrary_types_allowed=True)
|
||||
|
||||
# Env/Role/Action will use this context as private context, or use self.context as public context
|
||||
private_context: Optional[Context] = Field(default=None, exclude=True)
|
||||
# Env/Role/Action will use this config as private config, or use self.context.config as public config
|
||||
private_config: Optional[Config] = Field(default=None, exclude=True)
|
||||
|
||||
# Env/Role/Action will use this llm as private llm, or use self.context._llm instance
|
||||
private_llm: Optional[BaseLLM] = Field(default=None, exclude=True)
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
context: Optional[Context] = CONTEXT,
|
||||
config: Optional[Config] = None,
|
||||
llm: Optional[BaseLLM] = None,
|
||||
**kwargs,
|
||||
):
|
||||
"""Initialize with config"""
|
||||
super().__init__(**kwargs)
|
||||
self.set_context(context)
|
||||
self.set_config(config)
|
||||
self.set_llm(llm)
|
||||
|
||||
def set(self, k, v, override=False):
|
||||
"""Set attribute"""
|
||||
if override or not self.__dict__.get(k):
|
||||
self.__dict__[k] = v
|
||||
|
||||
def set_context(self, context: Context, override=True):
|
||||
"""Set context"""
|
||||
self.set("_context", context, override)
|
||||
|
||||
def set_config(self, config: Config, override=False):
|
||||
"""Set config"""
|
||||
self.set("_config", config, override)
|
||||
|
||||
def set_llm(self, llm: BaseLLM, override=False):
|
||||
"""Set llm"""
|
||||
self.set("_llm", llm, override)
|
||||
|
||||
@property
|
||||
def config(self) -> Config:
|
||||
"""Role config: role config > context config"""
|
||||
if self.private_config:
|
||||
return self.private_config
|
||||
return self.context.config
|
||||
|
||||
@config.setter
|
||||
def config(self, config: Config) -> None:
|
||||
"""Set config"""
|
||||
self.set_config(config)
|
||||
|
||||
@property
|
||||
def context(self) -> Context:
|
||||
"""Role context: role context > context"""
|
||||
if self.private_context:
|
||||
return self.private_context
|
||||
return CONTEXT
|
||||
|
||||
@context.setter
|
||||
def context(self, context: Context) -> None:
|
||||
"""Set context"""
|
||||
self.set_context(context)
|
||||
|
||||
@property
|
||||
def llm(self) -> BaseLLM:
|
||||
"""Role llm: if not existed, init from role.config"""
|
||||
# print(f"class:{self.__class__.__name__}({self.name}), llm: {self._llm}, llm_config: {self._llm_config}")
|
||||
if not self.private_llm:
|
||||
self.private_llm = self.context.llm_with_cost_manager_from_llm_config(self.config.llm)
|
||||
return self.private_llm
|
||||
|
||||
@llm.setter
|
||||
def llm(self, llm: BaseLLM) -> None:
|
||||
"""Set llm"""
|
||||
self.private_llm = llm
|
||||
|
|
@ -29,7 +29,6 @@ class LocalStore(BaseStore, ABC):
|
|||
def __init__(self, raw_data_path: Path, cache_dir: Path = None):
|
||||
if not raw_data_path:
|
||||
raise FileNotFoundError
|
||||
self.config = Config()
|
||||
self.raw_data_path = raw_data_path
|
||||
self.fname = self.raw_data_path.stem
|
||||
if not cache_dir:
|
||||
|
|
|
|||
|
|
@ -30,7 +30,7 @@ from pydantic import BaseModel, ConfigDict, Field, SerializeAsAny, model_validat
|
|||
from metagpt.actions import Action, ActionOutput
|
||||
from metagpt.actions.action_node import ActionNode
|
||||
from metagpt.actions.add_requirement import UserRequirement
|
||||
from metagpt.context import ContextMixin
|
||||
from metagpt.context_mixin import ContextMixin
|
||||
from metagpt.logs import logger
|
||||
from metagpt.memory import Memory
|
||||
from metagpt.provider import HumanProvider
|
||||
|
|
|
|||
File diff suppressed because one or more lines are too long
|
|
@ -9,7 +9,7 @@ from pydantic import BaseModel
|
|||
|
||||
from metagpt.config2 import Config
|
||||
from metagpt.configs.llm_config import LLMType
|
||||
from metagpt.context import ContextMixin
|
||||
from metagpt.context_mixin import ContextMixin
|
||||
from tests.metagpt.provider.mock_llm_config import (
|
||||
mock_llm_config,
|
||||
mock_llm_config_proxy,
|
||||
|
|
@ -53,12 +53,12 @@ def test_config_mixin_2():
|
|||
i = Config(llm=mock_llm_config)
|
||||
j = Config(llm=mock_llm_config_proxy)
|
||||
obj = ModelX(config=i)
|
||||
assert obj._config == i
|
||||
assert obj._config.llm == mock_llm_config
|
||||
assert obj.private_config == i
|
||||
assert obj.private_config.llm == mock_llm_config
|
||||
|
||||
obj.set_config(j)
|
||||
# obj already has a config, so it will not be set
|
||||
assert obj._config == i
|
||||
assert obj.private_config == i
|
||||
|
||||
|
||||
def test_config_mixin_3():
|
||||
|
|
@ -66,13 +66,13 @@ def test_config_mixin_3():
|
|||
i = Config(llm=mock_llm_config)
|
||||
j = Config(llm=mock_llm_config_proxy)
|
||||
obj = ModelY(config=i)
|
||||
assert obj._config == i
|
||||
assert obj._config.llm == mock_llm_config
|
||||
assert obj.private_config == i
|
||||
assert obj.private_config.llm == mock_llm_config
|
||||
|
||||
obj.set_config(j)
|
||||
# obj already has a config, so it will not be set
|
||||
assert obj._config == i
|
||||
assert obj._config.llm == mock_llm_config
|
||||
assert obj.private_config == i
|
||||
assert obj.private_config.llm == mock_llm_config
|
||||
|
||||
assert obj.a == "a"
|
||||
assert obj.b == "b"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue