Skip to content

Commit ce7d125

Browse files
committed
delete coreconfig, using the initial config instead
1 parent f500277 commit ce7d125

22 files changed

+324
-42
lines changed

metagpt/core/actions/action_graph.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"""
88
from __future__ import annotations
99

10-
# from metagpt.actions.action_node import ActionNode
10+
# from metagpt.core.actions.action_node import ActionNode
1111

1212

1313
class ActionGraph:

metagpt/core/config.py renamed to metagpt/core/config2.py

+73-13
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,21 @@
77
"""
88
import os
99
from pathlib import Path
10-
from typing import Dict, Iterable, Literal
10+
from typing import Dict, Iterable, List, Literal, Optional
1111

1212
from pydantic import BaseModel, Field, model_validator
1313

14+
from metagpt.core.configs.browser_config import BrowserConfig
15+
from metagpt.core.configs.embedding_config import EmbeddingConfig
1416
from metagpt.core.configs.exp_pool_config import ExperiencePoolConfig
15-
from metagpt.core.configs.llm_config import LLMConfig
17+
from metagpt.core.configs.llm_config import LLMConfig, LLMType
18+
from metagpt.core.configs.mermaid_config import MermaidConfig
19+
from metagpt.core.configs.omniparse_config import OmniParseConfig
20+
from metagpt.core.configs.redis_config import RedisConfig
21+
from metagpt.core.configs.role_custom_config import RoleCustomConfig
1622
from metagpt.core.configs.role_zero_config import RoleZeroConfig
23+
from metagpt.core.configs.s3_config import S3Config
24+
from metagpt.core.configs.search_config import SearchConfig
1725
from metagpt.core.configs.workspace_config import WorkspaceConfig
1826
from metagpt.core.const import CONFIG_ROOT, METAGPT_ROOT
1927
from metagpt.core.utils.yaml_model import YamlModel
@@ -38,23 +46,54 @@ def check_project_path(self):
3846
return self
3947

4048

41-
class CoreConfig(CLIParams, YamlModel):
49+
class Config(CLIParams, YamlModel):
4250
"""Configurations for MetaGPT"""
4351

44-
workspace: WorkspaceConfig = Field(default_factory=WorkspaceConfig)
45-
4652
# Key Parameters
4753
llm: LLMConfig
4854

55+
# RAG Embedding
56+
embedding: EmbeddingConfig = EmbeddingConfig()
57+
58+
# omniparse
59+
omniparse: OmniParseConfig = OmniParseConfig()
60+
4961
# Global Proxy. Will be used if llm.proxy is not set
5062
proxy: str = ""
5163

52-
# Experience Pool Parameters
53-
exp_pool: ExperiencePoolConfig = Field(default_factory=ExperiencePoolConfig)
64+
# Tool Parameters
65+
search: SearchConfig = SearchConfig()
66+
enable_search: bool = False
67+
browser: BrowserConfig = BrowserConfig()
68+
mermaid: MermaidConfig = MermaidConfig()
69+
70+
# Storage Parameters
71+
s3: Optional[S3Config] = None
72+
redis: Optional[RedisConfig] = None
5473

5574
# Misc Parameters
5675
repair_llm_output: bool = False
5776
prompt_schema: Literal["json", "markdown", "raw"] = "json"
77+
workspace: WorkspaceConfig = Field(default_factory=WorkspaceConfig)
78+
enable_longterm_memory: bool = False
79+
code_validate_k_times: int = 2
80+
81+
# Experience Pool Parameters
82+
exp_pool: ExperiencePoolConfig = Field(default_factory=ExperiencePoolConfig)
83+
84+
# Will be removed in the future
85+
metagpt_tti_url: str = ""
86+
language: str = "English"
87+
redis_key: str = "placeholder"
88+
iflytek_app_id: str = ""
89+
iflytek_api_secret: str = ""
90+
iflytek_api_key: str = ""
91+
azure_tts_subscription_key: str = ""
92+
azure_tts_region: str = ""
93+
_extra: dict = dict() # extra config dict
94+
95+
# Role's custom configuration
96+
roles: Optional[List[RoleCustomConfig]] = None
5897

5998
# RoleZero's configuration
6099
role_zero: RoleZeroConfig = Field(default_factory=RoleZeroConfig)
@@ -65,10 +104,10 @@ def from_home(cls, path):
65104
pathname = CONFIG_ROOT / path
66105
if not pathname.exists():
67106
return None
68-
return CoreConfig.from_yaml_file(pathname)
107+
return Config.from_yaml_file(pathname)
69108

70109
@classmethod
71-
def default(cls, reload: bool = False, **kwargs) -> "CoreConfig":
110+
def default(cls, reload: bool = False, **kwargs) -> "Config":
72111
"""Load default config
73112
- Priority: env < default_config_paths
74113
- Inside default_config_paths, the latter one overwrites the former one
@@ -78,24 +117,24 @@ def default(cls, reload: bool = False, **kwargs) -> "CoreConfig":
78117
CONFIG_ROOT / "config2.yaml",
79118
)
80119
if reload or default_config_paths not in _CONFIG_CACHE:
81-
dicts = [dict(os.environ), *(CoreConfig.read_yaml(path) for path in default_config_paths), kwargs]
120+
dicts = [dict(os.environ), *(Config.read_yaml(path) for path in default_config_paths), kwargs]
82121
final = merge_dict(dicts)
83-
_CONFIG_CACHE[default_config_paths] = CoreConfig(**final)
122+
_CONFIG_CACHE[default_config_paths] = Config(**final)
84123
return _CONFIG_CACHE[default_config_paths]
85124

86125
@classmethod
87126
def from_llm_config(cls, llm_config: dict):
88127
"""user config llm
89128
example:
90129
llm_config = {"api_type": "xxx", "api_key": "xxx", "model": "xxx"}
91-
gpt4 = CoreConfig.from_llm_config(llm_config)
130+
gpt4 = Config.from_llm_config(llm_config)
92131
A = Role(name="A", profile="Democratic candidate", goal="Win the election", actions=[a1], watch=[a2], config=gpt4)
93132
"""
94133
llm_config = LLMConfig.model_validate(llm_config)
95134
dicts = [dict(os.environ)]
96135
dicts += [{"llm": llm_config}]
97136
final = merge_dict(dicts)
98-
return CoreConfig(**final)
137+
return Config(**final)
99138

100139
def update_via_cli(self, project_path, project_name, inc, reqa_file, max_auto_summarize_code):
101140
"""update config via cli"""
@@ -110,6 +149,26 @@ def update_via_cli(self, project_path, project_name, inc, reqa_file, max_auto_su
110149
self.reqa_file = reqa_file
111150
self.max_auto_summarize_code = max_auto_summarize_code
112151

152+
@property
153+
def extra(self):
154+
return self._extra
155+
156+
@extra.setter
157+
def extra(self, value: dict):
158+
self._extra = value
159+
160+
def get_openai_llm(self) -> Optional[LLMConfig]:
161+
"""Get OpenAI LLMConfig by name. If no OpenAI, raise Exception"""
162+
if self.llm.api_type == LLMType.OPENAI:
163+
return self.llm
164+
return None
165+
166+
def get_azure_llm(self) -> Optional[LLMConfig]:
167+
"""Get Azure LLMConfig by name. If no Azure, raise Exception"""
168+
if self.llm.api_type == LLMType.AZURE:
169+
return self.llm
170+
return None
171+
113172

114173
def merge_dict(dicts: Iterable[Dict]) -> Dict:
115174
"""Merge multiple dicts into one, with the latter dict overwriting the former"""
@@ -120,3 +179,4 @@ def merge_dict(dicts: Iterable[Dict]) -> Dict:
120179

121180

122181
_CONFIG_CACHE = {}
182+
config = Config.default()
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
"""
4+
@Time : 2024/1/4 19:06
5+
@Author : alexanderwu
6+
@File : browser_config.py
7+
"""
8+
from enum import Enum
9+
from typing import Literal
10+
11+
from metagpt.core.utils.yaml_model import YamlModel
12+
13+
14+
class WebBrowserEngineType(Enum):
15+
PLAYWRIGHT = "playwright"
16+
SELENIUM = "selenium"
17+
CUSTOM = "custom"
18+
19+
@classmethod
20+
def __missing__(cls, key):
21+
"""Default type conversion"""
22+
return cls.CUSTOM
23+
24+
25+
class BrowserConfig(YamlModel):
26+
"""Config for Browser"""
27+
28+
engine: WebBrowserEngineType = WebBrowserEngineType.PLAYWRIGHT
29+
browser_type: Literal["chromium", "firefox", "webkit", "chrome", "firefox", "edge", "ie"] = "chromium"
30+
"""If the engine is Playwright, the value should be one of "chromium", "firefox", or "webkit". If it is Selenium, the value
31+
should be either "chrome", "firefox", "edge", or "ie"."""
+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
from enum import Enum
2+
from typing import Optional
3+
4+
from pydantic import field_validator
5+
6+
from metagpt.core.utils.yaml_model import YamlModel
7+
8+
9+
class EmbeddingType(Enum):
10+
OPENAI = "openai"
11+
AZURE = "azure"
12+
GEMINI = "gemini"
13+
OLLAMA = "ollama"
14+
15+
16+
class EmbeddingConfig(YamlModel):
17+
"""Config for Embedding.
18+
19+
Examples:
20+
---------
21+
api_type: "openai"
22+
api_key: "YOU_API_KEY"
23+
dimensions: "YOUR_MODEL_DIMENSIONS"
24+
25+
api_type: "azure"
26+
api_key: "YOU_API_KEY"
27+
base_url: "YOU_BASE_URL"
28+
api_version: "YOU_API_VERSION"
29+
dimensions: "YOUR_MODEL_DIMENSIONS"
30+
31+
api_type: "gemini"
32+
api_key: "YOU_API_KEY"
33+
34+
api_type: "ollama"
35+
base_url: "YOU_BASE_URL"
36+
model: "YOU_MODEL"
37+
dimensions: "YOUR_MODEL_DIMENSIONS"
38+
"""
39+
40+
api_type: Optional[EmbeddingType] = None
41+
api_key: Optional[str] = None
42+
base_url: Optional[str] = None
43+
api_version: Optional[str] = None
44+
45+
model: Optional[str] = None
46+
embed_batch_size: Optional[int] = None
47+
dimensions: Optional[int] = None # output dimension of embedding model
48+
49+
@field_validator("api_type", mode="before")
50+
@classmethod
51+
def check_api_type(cls, v):
52+
if v == "":
53+
return None
54+
return v

metagpt/core/configs/models_config.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
from pydantic import Field, field_validator
1919

20-
from metagpt.config2 import merge_dict
20+
from metagpt.core.config2 import merge_dict
2121
from metagpt.core.configs.llm_config import LLMConfig
2222
from metagpt.core.const import CONFIG_ROOT, METAGPT_ROOT
2323
from metagpt.core.utils.yaml_model import YamlModel
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from metagpt.core.utils.yaml_model import YamlModel
2+
3+
4+
class OmniParseConfig(YamlModel):
5+
api_key: str = ""
6+
base_url: str = ""
7+
timeout: int = 600

metagpt/core/configs/redis_config.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
"""
4+
@Time : 2024/1/4 19:06
5+
@Author : alexanderwu
6+
@File : redis_config.py
7+
"""
8+
from metagpt.core.utils.yaml_model import YamlModelWithoutDefault
9+
10+
11+
class RedisConfig(YamlModelWithoutDefault):
12+
host: str
13+
port: int
14+
username: str = ""
15+
password: str
16+
db: str
17+
18+
def to_url(self):
19+
return f"redis://{self.host}:{self.port}"
20+
21+
def to_kwargs(self):
22+
return {
23+
"username": self.username,
24+
"password": self.password,
25+
"db": self.db,
26+
}
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
"""
4+
@Time : 2024/4/22 16:33
5+
@Author : Justin
6+
@File : role_custom_config.py
7+
"""
8+
from metagpt.core.configs.llm_config import LLMConfig
9+
from metagpt.core.utils.yaml_model import YamlModel
10+
11+
12+
class RoleCustomConfig(YamlModel):
13+
"""custom config for roles
14+
role: role's className or role's role_id
15+
To be expanded
16+
"""
17+
18+
role: str = ""
19+
llm: LLMConfig

metagpt/core/configs/s3_config.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
"""
4+
@Time : 2024/1/4 19:07
5+
@Author : alexanderwu
6+
@File : s3_config.py
7+
"""
8+
from metagpt.core.utils.yaml_model import YamlModelWithoutDefault
9+
10+
11+
class S3Config(YamlModelWithoutDefault):
12+
access_key: str
13+
secret_key: str
14+
endpoint: str
15+
bucket: str

metagpt/core/configs/search_config.py

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
"""
4+
@Time : 2024/1/4 19:06
5+
@Author : alexanderwu
6+
@File : search_config.py
7+
"""
8+
from enum import Enum
9+
from typing import Callable, Optional
10+
11+
from pydantic import ConfigDict, Field
12+
13+
from metagpt.core.utils.yaml_model import YamlModel
14+
15+
16+
class SearchEngineType(Enum):
17+
SERPAPI_GOOGLE = "serpapi"
18+
SERPER_GOOGLE = "serper"
19+
DIRECT_GOOGLE = "google"
20+
DUCK_DUCK_GO = "ddg"
21+
CUSTOM_ENGINE = "custom"
22+
BING = "bing"
23+
24+
25+
class SearchConfig(YamlModel):
26+
"""Config for Search"""
27+
28+
model_config = ConfigDict(extra="allow")
29+
30+
api_type: SearchEngineType = SearchEngineType.DUCK_DUCK_GO
31+
api_key: str = ""
32+
cse_id: str = "" # for google
33+
search_func: Optional[Callable] = None
34+
params: dict = Field(
35+
default_factory=lambda: {
36+
"engine": "google",
37+
"google_domain": "google.com",
38+
"gl": "us",
39+
"hl": "en",
40+
}
41+
)

metagpt/core/context.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
from pydantic import BaseModel, ConfigDict, Field
1414

15-
from metagpt.core.config import CoreConfig
15+
from metagpt.core.config2 import Config
1616
from metagpt.core.configs.llm_config import LLMConfig, LLMType
1717
from metagpt.core.provider.base_llm import BaseLLM
1818
from metagpt.core.provider.llm_provider_registry import create_llm_instance
@@ -61,7 +61,7 @@ class Context(BaseModel):
6161
model_config = ConfigDict(arbitrary_types_allowed=True)
6262

6363
kwargs: AttrDict = AttrDict()
64-
config: CoreConfig = Field(default_factory=CoreConfig.default)
64+
config: Config = Field(default_factory=Config.default)
6565

6666
cost_manager: CostManager = CostManager()
6767

0 commit comments

Comments
 (0)