🌟 The Multi-Agent Framework: First AI Software Company, Towards Natural Language Programming
remove global config
shenchucheng2968c181c1fc
diff --git a/examples/agent_creator.py b/examples/agent_creator.pyindex bd58840ce..34160d398 100644--- a/examples/agent_creator.py+++ b/examples/agent_creator.py@@ -6,12 +6,13 @@ Author: garylin2099import refrom metagpt.actions import Action-from metagpt.config2 import config+from metagpt.config2 import Configfrom metagpt.const import METAGPT_ROOTfrom metagpt.logs import loggerfrom metagpt.roles import Rolefrom metagpt.schema import Message+config = Config.default()EXAMPLE_CODE_FILE = METAGPT_ROOT / "examples/build_customized_agent.py"MULTI_ACTION_AGENT_CODE_EXAMPLE = EXAMPLE_CODE_FILE.read_text()diff --git a/metagpt/actions/rebuild_class_view.py b/metagpt/actions/rebuild_class_view.pyindex ff030ec87..64f003f91 100644--- a/metagpt/actions/rebuild_class_view.py+++ b/metagpt/actions/rebuild_class_view.py@@ -14,7 +14,6 @@ from typing import Optional, Set, Tupleimport aiofilesfrom metagpt.actions import Action-from metagpt.config2 import configfrom metagpt.const import (AGGREGATION,COMPOSITION,@@ -40,7 +39,7 @@ class RebuildClassView(Action):graph_db: Optional[GraphRepository] = None- async def run(self, with_messages=None, format=config.prompt_schema):+ async def run(self, with_messages=None, format=None):"""Implementation of `Action`'s `run` method.@@ -48,6 +47,7 @@ class RebuildClassView(Action):with_messages (Optional[Type]): An optional argument specifying messages to react to.format (str): The format for the prompt schema."""+ format = format if format else self.config.prompt_schemagraph_repo_pathname = self.context.git_repo.workdir / GRAPH_REPO_FILE_REPO / self.context.git_repo.workdir.nameself.graph_db = await DiGraphRepository.load_from(str(graph_repo_pathname.with_suffix(".json")))repo_parser = RepoParser(base_directory=Path(self.i_context))diff --git a/metagpt/actions/rebuild_sequence_view.py b/metagpt/actions/rebuild_sequence_view.pyindex fd356d58f..e23487511 100644--- a/metagpt/actions/rebuild_sequence_view.py+++ b/metagpt/actions/rebuild_sequence_view.py@@ -18,7 +18,6 @@ from pydantic import BaseModelfrom tenacity import retry, stop_after_attempt, wait_random_exponentialfrom metagpt.actions import Action-from metagpt.config2 import configfrom metagpt.const import GRAPH_REPO_FILE_REPOfrom metagpt.logs import loggerfrom metagpt.repo_parser import CodeBlockInfo, DotClassInfo@@ -84,7 +83,7 @@ class RebuildSequenceView(Action):graph_db: Optional[GraphRepository] = None- async def run(self, with_messages=None, format=config.prompt_schema):+ async def run(self, with_messages=None, format=None):"""Implementation of `Action`'s `run` method.@@ -92,6 +91,7 @@ class RebuildSequenceView(Action):with_messages (Optional[Type]): An optional argument specifying messages to react to.format (str): The format for the prompt schema."""+ format = format if format else self.config.prompt_schemagraph_repo_pathname = self.context.git_repo.workdir / GRAPH_REPO_FILE_REPO / self.context.git_repo.workdir.nameself.graph_db = await DiGraphRepository.load_from(str(graph_repo_pathname.with_suffix(".json")))if not self.i_context:diff --git a/metagpt/actions/research.py b/metagpt/actions/research.pyindex 5e670520c..98edfddb0 100644--- a/metagpt/actions/research.py+++ b/metagpt/actions/research.py@@ -8,7 +8,6 @@ from typing import Any, Callable, Coroutine, Optional, Unionfrom pydantic import TypeAdapter, model_validatorfrom metagpt.actions import Action-from metagpt.config2 import configfrom metagpt.logs import loggerfrom metagpt.tools.search_engine import SearchEnginefrom metagpt.tools.web_browser_engine import WebBrowserEngine@@ -134,8 +133,8 @@ class CollectLinks(Action):if len(remove) == 0:break- model_name = config.llm.model- prompt = reduce_message_length(gen_msg(), model_name, system_text, config.llm.max_token)+ model_name = self.config.llm.model+ prompt = reduce_message_length(gen_msg(), model_name, system_text, self.config.llm.max_token)logger.debug(prompt)queries = await self._aask(prompt, [system_text])try:diff --git a/metagpt/actions/talk_action.py b/metagpt/actions/talk_action.pyindex 81f66f9a1..3fec32783 100644--- a/metagpt/actions/talk_action.py+++ b/metagpt/actions/talk_action.py@@ -9,7 +9,6 @@from typing import Optionalfrom metagpt.actions import Action-from metagpt.config2 import configfrom metagpt.logs import loggerfrom metagpt.schema import Message@@ -26,7 +25,7 @@ class TalkAction(Action):@propertydef language(self):- return self.context.kwargs.language or config.language+ return self.context.kwargs.language or self.config.language@propertydef prompt(self):diff --git a/metagpt/config2.py b/metagpt/config2.pyindex 6588a6036..8ed9d3f6b 100644--- a/metagpt/config2.py+++ b/metagpt/config2.py@@ -97,20 +97,21 @@ class Config(CLIParams, YamlModel):return Config.from_yaml_file(pathname)@classmethod- def default(cls):+ def default(cls, reload: bool = False):"""Load default config- Priority: env < default_config_paths- Inside default_config_paths, the latter one overwrites the former one"""- default_config_paths: List[Path] = [+ default_config_paths = (METAGPT_ROOT / "config/config2.yaml",CONFIG_ROOT / "config2.yaml",- ]-- dicts = [dict(os.environ)]- dicts += [Config.read_yaml(path) for path in default_config_paths]- final = merge_dict(dicts)- return Config(**final)+ )+ if reload or default_config_paths not in _CONFIG_CACHE:+ dicts = [dict(os.environ)]+ dicts += [Config.read_yaml(path) for path in default_config_paths]+ final = merge_dict(dicts)+ _CONFIG_CACHE[default_config_paths] = Config(**final)+ return _CONFIG_CACHE[default_config_paths]@classmethoddef from_llm_config(cls, llm_config: dict):@@ -160,4 +161,4 @@ def merge_dict(dicts: Iterable[Dict]) -> Dict:return result-config = Config.default()+_CONFIG_CACHE = {}diff --git a/metagpt/configs/browser_config.py b/metagpt/configs/browser_config.pyindex 2f8024f44..fafbaeeb8 100644--- a/metagpt/configs/browser_config.py+++ b/metagpt/configs/browser_config.py@@ -5,12 +5,23 @@@Author : alexanderwu@File : browser_config.py"""+from enum import Enumfrom typing import Literal-from metagpt.tools import WebBrowserEngineTypefrom metagpt.utils.yaml_model import YamlModel+class WebBrowserEngineType(Enum):+ PLAYWRIGHT = "playwright"+ SELENIUM = "selenium"+ CUSTOM = "custom"++ @classmethod+ def __missing__(cls, key):+ """Default type conversion"""+ return cls.CUSTOM++class BrowserConfig(YamlModel):"""Config for Browser"""diff --git a/metagpt/configs/search_config.py b/metagpt/configs/search_config.pyindex 7b50fb6d3..2c773b685 100644--- a/metagpt/configs/search_config.py+++ b/metagpt/configs/search_config.py@@ -5,14 +5,23 @@@Author : alexanderwu@File : search_config.py"""+from enum import Enumfrom typing import Callable, Optionalfrom pydantic import ConfigDict, Field-from metagpt.tools import SearchEngineTypefrom metagpt.utils.yaml_model import YamlModel+class SearchEngineType(Enum):+ SERPAPI_GOOGLE = "serpapi"+ SERPER_GOOGLE = "serper"+ DIRECT_GOOGLE = "google"+ DUCK_DUCK_GO = "ddg"+ CUSTOM_ENGINE = "custom"+ BING = "bing"++class SearchConfig(YamlModel):"""Config for Search"""diff --git a/metagpt/context.py b/metagpt/context.pyindex 384e8da48..0769f78eb 100644--- a/metagpt/context.py+++ b/metagpt/context.py@@ -10,7 +10,7 @@ from __future__ import annotationsimport osfrom typing import Any, Dict, Optional-from pydantic import BaseModel, ConfigDict+from pydantic import BaseModel, ConfigDict, Fieldfrom metagpt.config2 import Configfrom metagpt.configs.llm_config import LLMConfig, LLMType@@ -61,7 +61,7 @@ class Context(BaseModel):model_config = ConfigDict(arbitrary_types_allowed=True)kwargs: AttrDict = AttrDict()- config: Config = Config.default()+ config: Config = Field(default_factory=Config.default)cost_manager: CostManager = CostManager()diff --git a/metagpt/environment/minecraft/minecraft_env.py b/metagpt/environment/minecraft/minecraft_env.pyindex 0f39c9ccd..2bf39095c 100644--- a/metagpt/environment/minecraft/minecraft_env.py+++ b/metagpt/environment/minecraft/minecraft_env.py@@ -11,7 +11,7 @@ from typing import Any, Iterablefrom llama_index.vector_stores.chroma import ChromaVectorStorefrom pydantic import ConfigDict, Field-from metagpt.config2 import config as CONFIG+from metagpt.config2 import Configfrom metagpt.environment.base_env import Environmentfrom metagpt.environment.minecraft.const import MC_CKPT_DIRfrom metagpt.environment.minecraft.minecraft_ext_env import MinecraftExtEnv@@ -82,7 +82,7 @@ class MinecraftEnv(Environment, MinecraftExtEnv):persist_dir=f"{MC_CKPT_DIR}/skill/vectordb",)- if CONFIG.resume:+ if Config.default().resume:logger.info(f"Loading Action Developer from {MC_CKPT_DIR}/action")self.chest_memory = read_json_file(f"{MC_CKPT_DIR}/action/chest_memory.json")diff --git a/metagpt/exp_pool/decorator.py b/metagpt/exp_pool/decorator.pyindex 777d55ca9..888e61743 100644--- a/metagpt/exp_pool/decorator.py+++ b/metagpt/exp_pool/decorator.py@@ -6,7 +6,7 @@ from typing import Any, Callable, Optional, TypeVarfrom pydantic import BaseModel, ConfigDict, model_validator-from metagpt.config2 import config+from metagpt.config2 import Configfrom metagpt.exp_pool.context_builders import BaseContextBuilder, SimpleContextBuilderfrom metagpt.exp_pool.manager import ExperienceManager, exp_managerfrom metagpt.exp_pool.perfect_judges import BasePerfectJudge, SimplePerfectJudge@@ -50,11 +50,14 @@ def exp_cache("""def decorator(func: Callable[..., ReturnType]) -> Callable[..., ReturnType]:- if not config.exp_pool.enabled:- return func-@functools.wraps(func)async def get_or_create(args: Any, kwargs: Any) -> ReturnType:+ config = Config.default()++ if not config.exp_pool.enabled:+ rsp = func(*args, **kwargs)+ return await rsp if asyncio.iscoroutine(rsp) else rsp+handler = ExpCacheHandler(func=func,args=args,diff --git a/metagpt/exp_pool/manager.py b/metagpt/exp_pool/manager.pyindex b6ae9c0a3..253d45508 100644--- a/metagpt/exp_pool/manager.py+++ b/metagpt/exp_pool/manager.py@@ -2,9 +2,9 @@from typing import TYPE_CHECKING, Any-from pydantic import BaseModel, ConfigDict+from pydantic import BaseModel, ConfigDict, Field-from metagpt.config2 import Config, config+from metagpt.config2 import Configfrom metagpt.exp_pool.schema import (DEFAULT_COLLECTION_NAME,DEFAULT_SIMILARITY_TOP_K,@@ -29,7 +29,7 @@ class ExperienceManager(BaseModel):model_config = ConfigDict(arbitrary_types_allowed=True)- config: Config = config+ config: Config = Field(default_factory=Config.default)_storage: Any = None_vector_store: Any = Nonediff --git a/metagpt/ext/stanford_town/actions/st_action.py b/metagpt/ext/stanford_town/actions/st_action.pyindex 321676374..48cda353c 100644--- a/metagpt/ext/stanford_town/actions/st_action.py+++ b/metagpt/ext/stanford_town/actions/st_action.py@@ -8,7 +8,6 @@ from pathlib import Pathfrom typing import Any, Optional, Unionfrom metagpt.actions.action import Action-from metagpt.config2 import configfrom metagpt.ext.stanford_town.utils.const import PROMPTS_DIRfrom metagpt.logs import logger@@ -62,13 +61,13 @@ class STAction(Action):async def _run_gpt35_max_tokens(self, prompt: str, max_tokens: int = 50, retry: int = 3):for idx in range(retry):try:- tmp_max_tokens_rsp = getattr(config.llm, "max_token", 1500)- setattr(config.llm, "max_token", max_tokens)+ tmp_max_tokens_rsp = getattr(self.config.llm, "max_token", 1500)+ setattr(self.config.llm, "max_token", max_tokens)self.llm.use_system_prompt = False # to make it behave like a non-chat completionsllm_resp = await self._aask(prompt)- setattr(config.llm, "max_token", tmp_max_tokens_rsp)+ setattr(self.config.llm, "max_token", tmp_max_tokens_rsp)logger.info(f"Action: {self.cls_name} llm _run_gpt35_max_tokens raw resp: {llm_resp}")if self._func_validate(llm_resp, prompt):return self._func_cleanup(llm_resp, prompt)diff --git a/metagpt/ext/stanford_town/utils/utils.py b/metagpt/ext/stanford_town/utils/utils.pyindex 3aa0e80e8..e09cce8fe 100644--- a/metagpt/ext/stanford_town/utils/utils.py+++ b/metagpt/ext/stanford_town/utils/utils.py@@ -13,7 +13,7 @@ from typing import Unionfrom openai import OpenAI-from metagpt.config2 import config+from metagpt.config2 import Configfrom metagpt.logs import logger@@ -48,6 +48,7 @@ def read_csv_to_list(curr_file: str, header=False, strip_trail=True):def get_embedding(text, model: str = "text-embedding-ada-002"):+ config = Config.default()text = text.replace("\n", " ")if not text:text = "this is blank"diff --git a/metagpt/learn/text_to_embedding.py b/metagpt/learn/text_to_embedding.pyindex f859ab638..2b4adda80 100644--- a/metagpt/learn/text_to_embedding.py+++ b/metagpt/learn/text_to_embedding.py@@ -6,12 +6,13 @@@File : text_to_embedding.py@Desc : Text-to-Embedding skill, which provides text-to-embedding functionality."""-import metagpt.config2+from typing import Optional+from metagpt.config2 import Configfrom metagpt.tools.openai_text_to_embedding import oas3_openai_text_to_embedding-async def text_to_embedding(text, model="text-embedding-ada-002", config: Config = metagpt.config2.config):+async def text_to_embedding(text, model="text-embedding-ada-002", config: Optional[Config] = None):"""Text to embedding:param text: The text used for embedding.@@ -19,6 +20,7 @@ async def text_to_embedding(text, model="text-embedding-ada-002", config: Config:param config: OpenAI config with API key, For more details, checkout: `https://platform.openai.com/account/api-keys`:return: A json object of :class:`ResultEmbedding` class if successful, otherwise `{}`."""+ config = config if config else Config.default()openai_api_key = config.get_openai_llm().api_keyproxy = config.get_openai_llm().proxyreturn await oas3_openai_text_to_embedding(text, model=model, openai_api_key=openai_api_key, proxy=proxy)diff --git a/metagpt/learn/text_to_image.py b/metagpt/learn/text_to_image.pyindex 163859fc0..9bfed532b 100644--- a/metagpt/learn/text_to_image.py+++ b/metagpt/learn/text_to_image.py@@ -7,8 +7,8 @@@Desc : Text-to-Image skill, which provides text-to-image functionality."""import base64+from typing import Optional-import metagpt.config2from metagpt.config2 import Configfrom metagpt.const import BASE64_FORMATfrom metagpt.llm import LLM@@ -17,7 +17,7 @@ from metagpt.tools.openai_text_to_image import oas3_openai_text_to_imagefrom metagpt.utils.s3 import S3-async def text_to_image(text, size_type: str = "512x512", config: Config = metagpt.config2.config):+async def text_to_image(text, size_type: str = "512x512", config: Optional[Config] = None):"""Text to image:param text: The text used for image conversion.@@ -25,6 +25,7 @@ async def text_to_image(text, size_type: str = "512x512", config: Config = metag:param config: Config:return: The image data is returned in Base64 encoding."""+ config = config if config else Config.default()image_declaration = "data:image/png;base64,"model_url = config.metagpt_tti_urldiff --git a/metagpt/learn/text_to_speech.py b/metagpt/learn/text_to_speech.pyindex 8dbd6d243..9d3dba685 100644--- a/metagpt/learn/text_to_speech.py+++ b/metagpt/learn/text_to_speech.py@@ -6,7 +6,8 @@@File : text_to_speech.py@Desc : Text-to-Speech skill, which provides text-to-speech functionality"""-import metagpt.config2+from typing import Optional+from metagpt.config2 import Configfrom metagpt.const import BASE64_FORMATfrom metagpt.tools.azure_tts import oas3_azsure_tts@@ -20,7 +21,7 @@ async def text_to_speech(voice="zh-CN-XiaomoNeural",style="affectionate",role="Girl",- config: Config = metagpt.config2.config,+ config: Optional[Config] = None,):"""Text to speechFor more details, check out:`https://learn.microsoft.com/en-us/azure/ai-services/speech-service/language-support?tabs=tts`@@ -38,7 +39,7 @@ async def text_to_speech(:return: Returns the Base64-encoded .wav/.mp3 file data if successful, otherwise an empty string."""-+ config = config if config else Config.default()subscription_key = config.azure_tts_subscription_keyregion = config.azure_tts_regionif subscription_key and region:diff --git a/metagpt/memory/brain_memory.py b/metagpt/memory/brain_memory.pyindex c58148ead..8c2846d1d 100644--- a/metagpt/memory/brain_memory.py+++ b/metagpt/memory/brain_memory.py@@ -12,9 +12,9 @@ import jsonimport refrom typing import Dict, List, Optional-from pydantic import BaseModel, Field+from pydantic import BaseModel, Field, field_validator-from metagpt.config2 import config+from metagpt.config2 import Config as _Configfrom metagpt.const import DEFAULT_MAX_TOKENS, DEFAULT_TOKEN_SIZEfrom metagpt.logs import loggerfrom metagpt.provider import MetaGPTLLM@@ -32,6 +32,12 @@ class BrainMemory(BaseModel):last_talk: Optional[str] = Nonecacheable: bool = Truellm: Optional[BaseLLM] = Field(default=None, exclude=True)+ config: Optional[_Config] = None++ @field_validator("config")+ @classmethod+ def set_default_config(cls, config):+ return config if config else _Config.default()class Config:arbitrary_types_allowed = True@@ -54,9 +60,8 @@ class BrainMemory(BaseModel):texts = [m.content for m in self.knowledge]return "\n".join(texts)- @staticmethod- async def loads(redis_key: str) -> "BrainMemory":- redis = Redis(config.redis)+ async def loads(self, redis_key: str) -> "BrainMemory":+ redis = Redis(self.config.redis)if not redis_key:return BrainMemory()v = await redis.get(key=redis_key)@@ -70,7 +75,7 @@ class BrainMemory(BaseModel):async def dumps(self, redis_key: str, timeout_sec: int = 30 * 60):if not self.is_dirty:return- redis = Redis(config.redis)+ redis = Redis(self.config.redis)if not redis_key:return Falsev = self.model_dump_json()@@ -140,7 +145,7 @@ class BrainMemory(BaseModel):return textsummary = await self._summarize(text=text, max_words=max_words, keep_language=keep_language, limit=limit)if summary:- await self.set_history_summary(history_summary=summary, redis_key=config.redis_key)+ await self.set_history_summary(history_summary=summary, redis_key=self.config.redis_key)return summaryraise ValueError(f"text too long:{text_length}")@@ -164,7 +169,7 @@ class BrainMemory(BaseModel):msgs.reverse()self.history = msgsself.is_dirty = True- await self.dumps(redis_key=config.redis.key)+ await self.dumps(redis_key=self.config.redis.key)self.is_dirty = Falsereturn BrainMemory.to_metagpt_history_format(self.history)@@ -181,7 +186,7 @@ class BrainMemory(BaseModel):summary = await self.summarize(llm=llm, max_words=500)- language = config.language+ language = self.config.languagecommand = f"Translate the above summary into a {language} title of less than {max_words} words."summaries = [summary, command]msg = "\n".join(summaries)diff --git a/metagpt/rag/factories/embedding.py b/metagpt/rag/factories/embedding.pyindex 3613fd228..8a9d4bc95 100644--- a/metagpt/rag/factories/embedding.py+++ b/metagpt/rag/factories/embedding.py@@ -1,7 +1,7 @@"""RAG Embedding Factory."""from __future__ import annotations-from typing import Any+from typing import Any, Optionalfrom llama_index.core.embeddings import BaseEmbeddingfrom llama_index.embeddings.azure_openai import AzureOpenAIEmbedding@@ -9,7 +9,7 @@ from llama_index.embeddings.gemini import GeminiEmbeddingfrom llama_index.embeddings.ollama import OllamaEmbeddingfrom llama_index.embeddings.openai import OpenAIEmbedding-from metagpt.config2 import config+from metagpt.config2 import Configfrom metagpt.configs.embedding_config import EmbeddingTypefrom metagpt.configs.llm_config import LLMTypefrom metagpt.rag.factories.base import GenericFactory@@ -18,7 +18,7 @@ from metagpt.rag.factories.base import GenericFactoryclass RAGEmbeddingFactory(GenericFactory):"""Create LlamaIndex Embedding with MetaGPT's embedding config."""- def __init__(self):+ def __init__(self, config: Optional[Config] = None):creators = {EmbeddingType.OPENAI: self._create_openai,EmbeddingType.AZURE: self._create_azure,@@ -29,6 +29,7 @@ class RAGEmbeddingFactory(GenericFactory):LLMType.AZURE: self._create_azure,}super().__init__(creators)+ self.config = config if self.config else Config.default()def get_rag_embedding(self, key: EmbeddingType = None) -> BaseEmbedding:"""Key is EmbeddingType."""@@ -40,18 +41,18 @@ class RAGEmbeddingFactory(GenericFactory):If the embedding type is not specified, for backward compatibility, it checks if the LLM API type is either OPENAI or AZURE.Raise TypeError if embedding type not found."""- if config.embedding.api_type:- return config.embedding.api_type+ if self.config.embedding.api_type:+ return self.config.embedding.api_type- if config.llm.api_type in [LLMType.OPENAI, LLMType.AZURE]:- return config.llm.api_type+ if self.config.llm.api_type in [LLMType.OPENAI, LLMType.AZURE]:+ return self.config.llm.api_typeraise TypeError("To use RAG, please set your embedding in config2.yaml.")def _create_openai(self) -> OpenAIEmbedding:params = dict(- api_key=config.embedding.api_key or config.llm.api_key,- api_base=config.embedding.base_url or config.llm.base_url,+ api_key=self.config.embedding.api_key or self.config.llm.api_key,+ api_base=self.config.embedding.base_url or self.config.llm.base_url,)self._try_set_model_and_batch_size(params)@@ -60,9 +61,9 @@ class RAGEmbeddingFactory(GenericFactory):def _create_azure(self) -> AzureOpenAIEmbedding:params = dict(- api_key=config.embedding.api_key or config.llm.api_key,- azure_endpoint=config.embedding.base_url or config.llm.base_url,- api_version=config.embedding.api_version or config.llm.api_version,+ api_key=self.config.embedding.api_key or self.config.llm.api_key,+ azure_endpoint=self.config.embedding.base_url or self.config.llm.base_url,+ api_version=self.config.embedding.api_version or self.config.llm.api_version,)self._try_set_model_and_batch_size(params)@@ -71,8 +72,8 @@ class RAGEmbeddingFactory(GenericFactory):def _create_gemini(self) -> GeminiEmbedding:params = dict(- api_key=config.embedding.api_key,- api_base=config.embedding.base_url,+ api_key=self.config.embedding.api_key,+ api_base=self.config.embedding.base_url,)self._try_set_model_and_batch_size(params)@@ -81,7 +82,7 @@ class RAGEmbeddingFactory(GenericFactory):def _create_ollama(self) -> OllamaEmbedding:params = dict(- base_url=config.embedding.base_url,+ base_url=self.config.embedding.base_url,)self._try_set_model_and_batch_size(params)@@ -90,14 +91,15 @@ class RAGEmbeddingFactory(GenericFactory):def _try_set_model_and_batch_size(self, params: dict):"""Set the model_name and embed_batch_size only when they are specified."""- if config.embedding.model:- params["model_name"] = config.embedding.model+ if self.config.embedding.model:+ params["model_name"] = self.config.embedding.model- if config.embedding.embed_batch_size:- params["embed_batch_size"] = config.embedding.embed_batch_size+ if self.config.embedding.embed_batch_size:+ params["embed_batch_size"] = self.config.embedding.embed_batch_sizedef _raise_for_key(self, key: Any):raise ValueError(f"The embedding type is currently not supported: `{type(key)}`, {key}")-get_rag_embedding = RAGEmbeddingFactory().get_rag_embedding+def get_rag_embedding(key: EmbeddingType = None, config: Optional[Config] = None):+ return RAGEmbeddingFactory(config=config).get_rag_embedding(key)diff --git a/metagpt/rag/factories/llm.py b/metagpt/rag/factories/llm.pyindex 9fd19cab5..5d27cde3a 100644--- a/metagpt/rag/factories/llm.py+++ b/metagpt/rag/factories/llm.py@@ -10,9 +10,9 @@ from llama_index.core.llms import (LLMMetadata,)from llama_index.core.llms.callbacks import llm_completion_callback-from pydantic import Field+from pydantic import Field, model_validator-from metagpt.config2 import config+from metagpt.config2 import Configfrom metagpt.llm import LLMfrom metagpt.provider.base_llm import BaseLLMfrom metagpt.utils.async_helper import NestAsyncio@@ -26,9 +26,23 @@ class RAGLLM(CustomLLM):"""model_infer: BaseLLM = Field(..., description="The MetaGPT's LLM.")- context_window: int = TOKEN_MAX.get(config.llm.model, DEFAULT_CONTEXT_WINDOW)- num_output: int = config.llm.max_token- model_name: str = config.llm.model+ context_window: int = -1+ num_output: int = -1+ model_name: str = ""++ @model_validator(mode="after")+ def update_from_config(self):+ config = Config.default()+ if self.context_window < 0:+ self.context_window = TOKEN_MAX.get(config.llm.model, DEFAULT_CONTEXT_WINDOW)++ if self.num_output < 0:+ self.num_output = config.llm.max_token++ if not self.model_name:+ self.model_name = config.llm.model++ return self@propertydef metadata(self) -> LLMMetadata:diff --git a/metagpt/rag/schema.py b/metagpt/rag/schema.pyindex a8a10f90e..5e97e60c3 100644--- a/metagpt/rag/schema.py+++ b/metagpt/rag/schema.py@@ -10,7 +10,7 @@ from llama_index.core.schema import TextNodefrom llama_index.core.vector_stores.types import VectorStoreQueryModefrom pydantic import BaseModel, ConfigDict, Field, PrivateAttr, model_validator-from metagpt.config2 import config+from metagpt.config2 import Configfrom metagpt.configs.embedding_config import EmbeddingTypefrom metagpt.logs import loggerfrom metagpt.rag.interface import RAGObject@@ -45,6 +45,7 @@ class FAISSRetrieverConfig(IndexRetrieverConfig):@model_validator(mode="after")def check_dimensions(self):if self.dimensions == 0:+ config = Config.default()self.dimensions = config.embedding.dimensions or self._embedding_type_to_dimensions.get(config.embedding.api_type, 1536)diff --git a/metagpt/software_company.py b/metagpt/software_company.pyindex 2ea16f55f..f74b61191 100644--- a/metagpt/software_company.py+++ b/metagpt/software_company.py@@ -27,7 +27,7 @@ def generate_repo(recover_path=None,):"""Run the startup logic. Can be called from CLI or other Python scripts."""- from metagpt.config2 import config+ from metagpt.config2 import Configfrom metagpt.context import Contextfrom metagpt.roles import (Architect,@@ -38,6 +38,8 @@ def generate_repo()from metagpt.team import Team+ config = Config.default()+config.update_via_cli(project_path, project_name, inc, reqa_file, max_auto_summarize_code)ctx = Context(config=config)diff --git a/metagpt/tools/__init__.py b/metagpt/tools/__init__.pyindex 35fa04658..2027dbb1d 100644--- a/metagpt/tools/__init__.py+++ b/metagpt/tools/__init__.py@@ -6,33 +6,18 @@@File : __init__.py"""-from enum import Enumfrom metagpt.tools import libs # this registers all toolsfrom metagpt.tools.tool_registry import TOOL_REGISTRY--_ = libs, TOOL_REGISTRY # Avoid pre-commit error---class SearchEngineType(Enum):- SERPAPI_GOOGLE = "serpapi"- SERPER_GOOGLE = "serper"- DIRECT_GOOGLE = "google"- DUCK_DUCK_GO = "ddg"- CUSTOM_ENGINE = "custom"- BING = "bing"+from metagpt.configs.search_config import SearchEngineType+from metagpt.configs.browser_config import WebBrowserEngineType-class WebBrowserEngineType(Enum):- PLAYWRIGHT = "playwright"- SELENIUM = "selenium"- CUSTOM = "custom"-- @classmethod- def __missing__(cls, key):- """Default type conversion"""- return cls.CUSTOM+_ = libs, TOOL_REGISTRY # Avoid pre-commit errorclass SearchInterface:async def asearch(self, *args, **kwargs):...+++__all__ = ["SearchEngineType", "WebBrowserEngineType", "TOOL_REGISTRY"]diff --git a/metagpt/tools/libs/gpt_v_generator.py b/metagpt/tools/libs/gpt_v_generator.pyindex baedc3d61..66c023766 100644--- a/metagpt/tools/libs/gpt_v_generator.py+++ b/metagpt/tools/libs/gpt_v_generator.py@@ -7,7 +7,9 @@"""import refrom pathlib import Path+from typing import Optional+from metagpt.config2 import Configfrom metagpt.const import DEFAULT_WORKSPACE_ROOTfrom metagpt.logs import loggerfrom metagpt.tools.tool_registry import register_tool@@ -36,11 +38,11 @@ class GPTvGenerator:It utilizes a vision model to analyze the layout from an image and generate webpage codes accordingly."""- def __init__(self):+ def __init__(self, config: Optional[Config]):"""Initialize GPTvGenerator class with default values from the configuration."""- from metagpt.config2 import configfrom metagpt.llm import LLM+ config = config if config else Config.default()self.llm = LLM(llm_config=config.get_openai_llm())self.llm.model = "gpt-4-vision-preview"diff --git a/metagpt/tools/ut_writer.py b/metagpt/tools/ut_writer.pyindex 243871aff..9e67a3585 100644--- a/metagpt/tools/ut_writer.py+++ b/metagpt/tools/ut_writer.py@@ -4,7 +4,7 @@import jsonfrom pathlib import Path-from metagpt.config2 import config+from metagpt.config2 import Configfrom metagpt.provider.openai_api import OpenAILLM as GPTAPIfrom metagpt.utils.common import awrite@@ -282,6 +282,7 @@ class UTGenerator:"""Choose based on different calling methods"""result = ""if self.chatgpt_method == "API":+ config = Config.default()result = await GPTAPI(config.get_openai_llm()).aask_code(messages=messages)return resultdiff --git a/metagpt/utils/embedding.py b/metagpt/utils/embedding.pyindex 3d53a314c..3fcf1f25b 100644--- a/metagpt/utils/embedding.py+++ b/metagpt/utils/embedding.py@@ -7,10 +7,11 @@"""from llama_index.embeddings.openai import OpenAIEmbedding-from metagpt.config2 import config+from metagpt.config2 import Configdef get_embedding() -> OpenAIEmbedding:+ config = Config.default()llm = config.get_openai_llm()if llm is None:raise ValueError("To use OpenAIEmbedding, please ensure that config.llm.api_type is correctly set to 'openai'.")diff --git a/metagpt/utils/make_sk_kernel.py b/metagpt/utils/make_sk_kernel.pyindex 283a682d6..f0c55b07c 100644--- a/metagpt/utils/make_sk_kernel.py+++ b/metagpt/utils/make_sk_kernel.py@@ -13,10 +13,11 @@ from semantic_kernel.connectors.ai.open_ai.services.open_ai_chat_completion impoOpenAIChatCompletion,)-from metagpt.config2 import config+from metagpt.config2 import Configdef make_sk_kernel():+ config = Config.default()kernel = sk.Kernel()if llm := config.get_azure_llm():kernel.add_chat_service(diff --git a/metagpt/utils/mermaid.py b/metagpt/utils/mermaid.pyindex ba33b8d61..d87ae4f83 100644--- a/metagpt/utils/mermaid.py+++ b/metagpt/utils/mermaid.py@@ -9,12 +9,14 @@ import asyncioimport osfrom pathlib import Path-from metagpt.config2 import config+from metagpt.config2 import Configfrom metagpt.logs import loggerfrom metagpt.utils.common import awrite, check_cmd_exists-async def mermaid_to_file(engine, mermaid_code, output_file_without_suffix, width=2048, height=2048) -> int:+async def mermaid_to_file(+ engine, mermaid_code, output_file_without_suffix, width=2048, height=2048, config=None+) -> int:"""suffix: png/svg/pdf:param mermaid_code: mermaid code@@ -24,6 +26,7 @@ async def mermaid_to_file(engine, mermaid_code, output_file_without_suffix, widt:return: 0 if succeed, -1 if failed"""# Write the Mermaid code to a temporary file+ config = config if config else Config.default()dir_name = os.path.dirname(output_file_without_suffix)if dir_name and not os.path.exists(dir_name):os.makedirs(dir_name)diff --git a/metagpt/utils/mmdc_pyppeteer.py b/metagpt/utils/mmdc_pyppeteer.pyindex f029325f1..4e30ee538 100644--- a/metagpt/utils/mmdc_pyppeteer.py+++ b/metagpt/utils/mmdc_pyppeteer.py@@ -10,11 +10,11 @@ from urllib.parse import urljoinfrom pyppeteer import launch-from metagpt.config2 import config+from metagpt.config2 import Configfrom metagpt.logs import logger-async def mermaid_to_file(mermaid_code, output_file_without_suffix, width=2048, height=2048) -> int:+async def mermaid_to_file(mermaid_code, output_file_without_suffix, width=2048, height=2048, config=None) -> int:"""Converts the given Mermaid code to various output formats and saves them to files.@@ -27,6 +27,7 @@ async def mermaid_to_file(mermaid_code, output_file_without_suffix, width=2048,Returns:int: Returns 1 if the conversion and saving were successful, -1 otherwise."""+ config = config if config else Config.default()suffixes = ["png", "svg", "pdf"]__dirname = os.path.dirname(os.path.abspath(__file__))diff --git a/metagpt/utils/repair_llm_raw_output.py b/metagpt/utils/repair_llm_raw_output.pyindex 68fa73108..5c57693f7 100644--- a/metagpt/utils/repair_llm_raw_output.py+++ b/metagpt/utils/repair_llm_raw_output.py@@ -4,12 +4,12 @@import copyfrom enum import Enum-from typing import Callable, Union+from typing import Callable, Optional, Unionimport regex as refrom tenacity import RetryCallState, retry, stop_after_attempt, wait_fixed-from metagpt.config2 import config+from metagpt.config2 import Configfrom metagpt.logs import loggerfrom metagpt.utils.custom_decoder import CustomDecoder@@ -154,7 +154,9 @@ def _repair_llm_raw_output(output: str, req_key: str, repair_type: RepairType =return output-def repair_llm_raw_output(output: str, req_keys: list[str], repair_type: RepairType = None) -> str:+def repair_llm_raw_output(+ output: str, req_keys: list[str], repair_type: RepairType = None, config: Optional[Config] = None+) -> str:"""in open-source llm model, it usually can't follow the instruction well, the output may be incomplete,so here we try to repair it and use all repair methods by default.@@ -169,6 +171,7 @@ def repair_llm_raw_output(output: str, req_keys: list[str], repair_type: RepairTtarget: { xxx }output: { xxx }]"""+ config = config if config else Config.default()if not config.repair_llm_output:return output@@ -256,6 +259,7 @@ def run_after_exp_and_passon_next_retry(logger: "loguru.Logger") -> Callable[["R"next_action":"None"}"""+ config = Config.default()if retry_state.outcome.failed:if retry_state.args:# # can't be used as args=retry_state.args@@ -276,8 +280,12 @@ def run_after_exp_and_passon_next_retry(logger: "loguru.Logger") -> Callable[["Rreturn run_and_passon+def repair_stop_after_attempt(retry_state):+ return stop_after_attempt(3 if Config.default().repair_llm_output else 0)(retry_state)++@retry(- stop=stop_after_attempt(3 if config.repair_llm_output else 0),+ stop=repair_stop_after_attempt,wait=wait_fixed(1),after=run_after_exp_and_passon_next_retry(logger),)diff --git a/tests/metagpt/roles/di/run_swe_agent_for_benchmark.py b/tests/metagpt/roles/di/run_swe_agent_for_benchmark.pyindex e2aa3d17f..207521c97 100644--- a/tests/metagpt/roles/di/run_swe_agent_for_benchmark.py+++ b/tests/metagpt/roles/di/run_swe_agent_for_benchmark.py@@ -2,13 +2,14 @@ import asyncioimport jsonfrom datetime import datetime-from metagpt.config2 import config+from metagpt.config2 import Configfrom metagpt.const import DEFAULT_WORKSPACE_ROOT, METAGPT_ROOTfrom metagpt.logs import loggerfrom metagpt.roles.di.swe_agent import SWEAgentfrom metagpt.tools.libs.terminal import Terminalfrom metagpt.tools.swe_agent_commands.swe_agent_utils import load_hf_dataset+config = Config.default()# Specify by yourselfTEST_REPO_DIR = METAGPT_ROOT / "data" / "test_repo"DATA_DIR = METAGPT_ROOT / "data/hugging_face"diff --git a/tests/metagpt/test_document.py b/tests/metagpt/test_document.pyindex 9c076f4e6..29393bb13 100644--- a/tests/metagpt/test_document.py+++ b/tests/metagpt/test_document.py@@ -5,10 +5,12 @@@Author : alexanderwu@File : test_document.py"""-from metagpt.config2 import config+from metagpt.config2 import Configfrom metagpt.document import Repofrom metagpt.logs import logger+config = Config.default()+def set_existing_repo(path):repo1 = Repo.from_path(path)diff --git a/tests/metagpt/tools/test_azure_tts.py b/tests/metagpt/tools/test_azure_tts.pyindex f72b5663b..ee55616d2 100644--- a/tests/metagpt/tools/test_azure_tts.py+++ b/tests/metagpt/tools/test_azure_tts.py@@ -12,9 +12,11 @@ from pathlib import Pathimport pytestfrom azure.cognitiveservices.speech import ResultReason, SpeechSynthesizer-from metagpt.config2 import config+from metagpt.config2 import Configfrom metagpt.tools.azure_tts import AzureTTS+config = Config.default()+@pytest.mark.asyncioasync def test_azure_tts(mocker):diff --git a/tests/metagpt/tools/test_metagpt_text_to_image.py b/tests/metagpt/tools/test_metagpt_text_to_image.pyindex d3797a460..bd0fcaf8b 100644--- a/tests/metagpt/tools/test_metagpt_text_to_image.py+++ b/tests/metagpt/tools/test_metagpt_text_to_image.py@@ -10,9 +10,11 @@ from unittest.mock import AsyncMockimport pytest-from metagpt.config2 import config+from metagpt.config2 import Configfrom metagpt.tools.metagpt_text_to_image import oas3_metagpt_text_to_image+config = Config.default()+@pytest.mark.asyncioasync def test_draw(mocker):diff --git a/tests/metagpt/tools/test_moderation.py b/tests/metagpt/tools/test_moderation.pyindex 8dc9e9d5e..0f921887f 100644--- a/tests/metagpt/tools/test_moderation.py+++ b/tests/metagpt/tools/test_moderation.py@@ -8,10 +8,12 @@import pytest-from metagpt.config2 import config+from metagpt.config2 import Configfrom metagpt.llm import LLMfrom metagpt.tools.moderation import Moderation+config = Config.default()+@pytest.mark.asyncio@pytest.mark.parametrize(diff --git a/tests/metagpt/tools/test_openai_text_to_image.py b/tests/metagpt/tools/test_openai_text_to_image.pyindex 3f9169ddd..4856342d1 100644--- a/tests/metagpt/tools/test_openai_text_to_image.py+++ b/tests/metagpt/tools/test_openai_text_to_image.py@@ -11,7 +11,7 @@ import openaiimport pytestfrom pydantic import BaseModel-from metagpt.config2 import config+from metagpt.config2 import Configfrom metagpt.llm import LLMfrom metagpt.tools.openai_text_to_image import (OpenAIText2Image,@@ -19,6 +19,8 @@ from metagpt.tools.openai_text_to_image import ()from metagpt.utils.s3 import S3+config = Config.default()+@pytest.mark.asyncioasync def test_draw(mocker):diff --git a/tests/metagpt/tools/test_ut_writer.py b/tests/metagpt/tools/test_ut_writer.pyindex 3cc7e86bb..3ebbe6d9d 100644--- a/tests/metagpt/tools/test_ut_writer.py+++ b/tests/metagpt/tools/test_ut_writer.py@@ -20,10 +20,12 @@ from openai.types.chat.chat_completion_message_tool_call import (Function,)-from metagpt.config2 import config+from metagpt.config2 import Configfrom metagpt.const import API_QUESTIONS_PATH, UT_PY_PATHfrom metagpt.tools.ut_writer import YFT_PROMPT_PREFIX, UTGenerator+config = Config.default()+class TestUTWriter:@pytest.mark.asynciodiff --git a/tests/metagpt/utils/test_repair_llm_raw_output.py b/tests/metagpt/utils/test_repair_llm_raw_output.pyindex 7a29ea3ee..75bd9f165 100644--- a/tests/metagpt/utils/test_repair_llm_raw_output.py+++ b/tests/metagpt/utils/test_repair_llm_raw_output.py@@ -2,7 +2,9 @@# -*- coding: utf-8 -*-# @Desc : unittest of repair_llm_raw_output-from metagpt.config2 import config+from metagpt.config2 import Config++config = Config.default()"""CONFIG.repair_llm_output should be True before retry_parse_json_text imported.diff --git a/tests/mock/mock_llm.py b/tests/mock/mock_llm.pyindex 168125448..fdbf86825 100644--- a/tests/mock/mock_llm.py+++ b/tests/mock/mock_llm.py@@ -1,7 +1,7 @@import jsonfrom typing import Optional, Union-from metagpt.config2 import config+from metagpt.config2 import Configfrom metagpt.configs.llm_config import LLMTypefrom metagpt.const import LLM_API_TIMEOUTfrom metagpt.logs import logger@@ -10,6 +10,8 @@ from metagpt.provider.constant import GENERAL_FUNCTION_SCHEMAfrom metagpt.provider.openai_api import OpenAILLMfrom metagpt.schema import Message+config = Config.default()+OriginalLLM = OpenAILLM if config.llm.api_type == LLMType.OPENAI else AzureOpenAILLM