Skip to content

🌟 The Multi-Agent Framework: First AI Software Company, Towards Natural Language Programming

use config

geekan001ec115d758
diff --git a/metagpt/actions/design_api.py b/metagpt/actions/design_api.py
index 3e978f823..5f973bb60 100644
--- a/metagpt/actions/design_api.py
+++ b/metagpt/actions/design_api.py
@@ -110,7 +110,7 @@ class WriteDesign(Action):
if not data_api_design:
return
pathname = self.git_repo.workdir / DATA_API_DESIGN_FILE_REPO / Path(design_doc.filename).with_suffix("")
- await WriteDesign._save_mermaid_file(data_api_design, pathname)
+ await self._save_mermaid_file(data_api_design, pathname)
logger.info(f"Save class view to {str(pathname)}")
async def _save_seq_flow(self, design_doc):
@@ -119,13 +119,12 @@ class WriteDesign(Action):
if not seq_flow:
return
pathname = self.git_repo.workdir / Path(SEQ_FLOW_FILE_REPO) / Path(design_doc.filename).with_suffix("")
- await WriteDesign._save_mermaid_file(seq_flow, pathname)
+ await self._save_mermaid_file(seq_flow, pathname)
logger.info(f"Saving sequence flow to {str(pathname)}")
async def _save_pdf(self, design_doc):
await self.file_repo.save_as(doc=design_doc, with_suffix=".md", relative_path=SYSTEM_DESIGN_PDF_FILE_REPO)
- @staticmethod
- async def _save_mermaid_file(data: str, pathname: Path):
+ async def _save_mermaid_file(self, data: str, pathname: Path):
pathname.parent.mkdir(parents=True, exist_ok=True)
- await mermaid_to_file(data, pathname)
+ await mermaid_to_file(self.config.mermaid_engine, data, pathname)
diff --git a/metagpt/actions/research.py b/metagpt/actions/research.py
index a635714ef..0af49a1cf 100644
--- a/metagpt/actions/research.py
+++ b/metagpt/actions/research.py
@@ -8,7 +8,6 @@ from typing import Callable, Optional, Union
from pydantic import Field, parse_obj_as
from metagpt.actions import Action
-from metagpt.config import CONFIG
from metagpt.config2 import config
from metagpt.logs import logger
from metagpt.tools.search_engine import SearchEngine
@@ -216,9 +215,7 @@ class WebBrowseAndSummarize(Action):
for u, content in zip([url, *urls], contents):
content = content.inner_text
chunk_summaries = []
- for prompt in generate_prompt_chunk(
- content, prompt_template, self.llm.model, system_text, CONFIG.max_tokens_rsp
- ):
+ for prompt in generate_prompt_chunk(content, prompt_template, self.llm.model, system_text, 4096):
logger.debug(prompt)
summary = await self._aask(prompt, [system_text])
if summary == "Not relevant.":
diff --git a/metagpt/config2.py b/metagpt/config2.py
index 2a9611627..6345c1b8c 100644
--- a/metagpt/config2.py
+++ b/metagpt/config2.py
@@ -74,6 +74,12 @@ class Config(CLIParams, YamlModel):
mmdc: str = "mmdc"
puppeteer_config: str = ""
pyppeteer_executable_path: str = ""
+ IFLYTEK_APP_ID: str = ""
+ IFLYTEK_APP_SECRET: str = ""
+ IFLYTEK_APP_KEY: str = ""
+ AZURE_TTS_SUBSCRIPTION_KEY: str = ""
+ AZURE_TTS_REGION: str = ""
+ mermaid_engine: str = "nodejs"
@classmethod
def default(cls):
diff --git a/metagpt/learn/text_to_speech.py b/metagpt/learn/text_to_speech.py
index f12e52b8e..8ffafbd0e 100644
--- a/metagpt/learn/text_to_speech.py
+++ b/metagpt/learn/text_to_speech.py
@@ -7,7 +7,6 @@
@Desc : Text-to-Speech skill, which provides text-to-speech functionality
"""
-from metagpt.config import CONFIG
from metagpt.config2 import config
from metagpt.const import BASE64_FORMAT
from metagpt.tools.azure_tts import oas3_azsure_tts
@@ -45,7 +44,7 @@ async def text_to_speech(
"""
- if (CONFIG.AZURE_TTS_SUBSCRIPTION_KEY and CONFIG.AZURE_TTS_REGION) or (subscription_key and region):
+ if subscription_key and region:
audio_declaration = "data:audio/wav;base64,"
base64_data = await oas3_azsure_tts(text, lang, voice, style, role, subscription_key, region)
s3 = S3(config.s3)
@@ -53,14 +52,12 @@ async def text_to_speech(
if url:
return f"[{text}]({url})"
return audio_declaration + base64_data if base64_data else base64_data
- if (CONFIG.IFLYTEK_APP_ID and CONFIG.IFLYTEK_API_KEY and CONFIG.IFLYTEK_API_SECRET) or (
- iflytek_app_id and iflytek_api_key and iflytek_api_secret
- ):
+ if iflytek_app_id and iflytek_api_key and iflytek_api_secret:
audio_declaration = "data:audio/mp3;base64,"
base64_data = await oas3_iflytek_tts(
text=text, app_id=iflytek_app_id, api_key=iflytek_api_key, api_secret=iflytek_api_secret
)
- s3 = S3()
+ s3 = S3(config.s3)
url = await s3.cache(data=base64_data, file_ext=".mp3", format=BASE64_FORMAT)
if url:
return f"[{text}]({url})"
diff --git a/metagpt/tools/azure_tts.py b/metagpt/tools/azure_tts.py
index f4f8aa0a2..2e0e2267c 100644
--- a/metagpt/tools/azure_tts.py
+++ b/metagpt/tools/azure_tts.py
@@ -13,7 +13,6 @@ from uuid import uuid4
import aiofiles
from azure.cognitiveservices.speech import AudioConfig, SpeechConfig, SpeechSynthesizer
-from metagpt.config import CONFIG
from metagpt.logs import logger
@@ -25,8 +24,8 @@ class AzureTTS:
:param subscription_key: key is used to access your Azure AI service API, see: `https://portal.azure.com/` > `Resource Management` > `Keys and Endpoint`
:param region: This is the location (or region) of your resource. You may need to use this field when making calls to this API.
"""
- self.subscription_key = subscription_key if subscription_key else CONFIG.AZURE_TTS_SUBSCRIPTION_KEY
- self.region = region if region else CONFIG.AZURE_TTS_REGION
+ self.subscription_key = subscription_key
+ self.region = region
# 参数参考:https://learn.microsoft.com/zh-cn/azure/cognitive-services/speech-service/language-support?tabs=tts#voice-styles-and-roles
async def synthesize_speech(self, lang, voice, text, output_file):
@@ -83,10 +82,6 @@ async def oas3_azsure_tts(text, lang="", voice="", style="", role="", subscripti
role = "Girl"
if not style:
style = "affectionate"
- if not subscription_key:
- subscription_key = CONFIG.AZURE_TTS_SUBSCRIPTION_KEY
- if not region:
- region = CONFIG.AZURE_TTS_REGION
xml_value = AzureTTS.role_style_text(role=role, style=style, text=text)
tts = AzureTTS(subscription_key=subscription_key, region=region)
diff --git a/metagpt/tools/iflytek_tts.py b/metagpt/tools/iflytek_tts.py
index ad2395362..6ce48826b 100644
--- a/metagpt/tools/iflytek_tts.py
+++ b/metagpt/tools/iflytek_tts.py
@@ -23,7 +23,6 @@ import aiofiles
import websockets as websockets
from pydantic import BaseModel
-from metagpt.config import CONFIG
from metagpt.logs import logger
@@ -56,9 +55,9 @@ class IFlyTekTTS(object):
:param api_key: WebAPI argument, see: `https://console.xfyun.cn/services/tts`
:param api_secret: WebAPI argument, see: `https://console.xfyun.cn/services/tts`
"""
- self.app_id = app_id or CONFIG.IFLYTEK_APP_ID
- self.api_key = api_key or CONFIG.IFLYTEK_API_KEY
- self.api_secret = api_secret or CONFIG.API_SECRET
+ self.app_id = app_id
+ self.api_key = api_key
+ self.api_secret = api_secret
async def synthesize_speech(self, text, output_file: str, voice=DEFAULT_IFLYTEK_VOICE):
url = self._create_url()
@@ -127,14 +126,6 @@ async def oas3_iflytek_tts(text: str, voice: str = "", app_id: str = "", api_key
:return: Returns the Base64-encoded .mp3 file data if successful, otherwise an empty string.
"""
- if not app_id:
- app_id = CONFIG.IFLYTEK_APP_ID
- if not api_key:
- api_key = CONFIG.IFLYTEK_API_KEY
- if not api_secret:
- api_secret = CONFIG.IFLYTEK_API_SECRET
- if not voice:
- voice = CONFIG.IFLYTEK_VOICE or DEFAULT_IFLYTEK_VOICE
filename = Path(__file__).parent / (uuid.uuid4().hex + ".mp3")
try:
diff --git a/metagpt/tools/search_engine.py b/metagpt/tools/search_engine.py
index 64388a11f..fd237d537 100644
--- a/metagpt/tools/search_engine.py
+++ b/metagpt/tools/search_engine.py
@@ -10,7 +10,6 @@ from typing import Callable, Coroutine, Literal, Optional, Union, overload
from semantic_kernel.skill_definition import sk_function
-from metagpt.config import CONFIG
from metagpt.tools import SearchEngineType
@@ -46,7 +45,6 @@ class SearchEngine:
engine: Optional[SearchEngineType] = None,
run_func: Callable[[str, int, bool], Coroutine[None, None, Union[str, list[str]]]] = None,
):
- engine = engine or CONFIG.search_engine
if engine == SearchEngineType.SERPAPI_GOOGLE:
module = "metagpt.tools.search_engine_serpapi"
run_func = importlib.import_module(module).SerpAPIWrapper().run
diff --git a/metagpt/utils/mermaid.py b/metagpt/utils/mermaid.py
index 893d05be0..3f6a2ef12 100644
--- a/metagpt/utils/mermaid.py
+++ b/metagpt/utils/mermaid.py
@@ -17,7 +17,7 @@ from metagpt.logs import logger
from metagpt.utils.common import check_cmd_exists
-async def mermaid_to_file(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) -> int:
"""suffix: png/svg/pdf
:param mermaid_code: mermaid code
@@ -35,7 +35,6 @@ async def mermaid_to_file(mermaid_code, output_file_without_suffix, width=2048,
await f.write(mermaid_code)
# tmp.write_text(mermaid_code, encoding="utf-8")
- engine = config.mermaid["default"].engine
if engine == "nodejs":
if check_cmd_exists(config.mmdc) != 0:
logger.warning(
diff --git a/tests/metagpt/learn/test_text_to_speech.py b/tests/metagpt/learn/test_text_to_speech.py
index aca08b9a2..41611171c 100644
--- a/tests/metagpt/learn/test_text_to_speech.py
+++ b/tests/metagpt/learn/test_text_to_speech.py
@@ -9,34 +9,43 @@
import pytest
-from metagpt.config import CONFIG
+from metagpt.config2 import config
from metagpt.learn.text_to_speech import text_to_speech
@pytest.mark.asyncio
async def test_text_to_speech():
# Prerequisites
- assert CONFIG.IFLYTEK_APP_ID
- assert CONFIG.IFLYTEK_API_KEY
- assert CONFIG.IFLYTEK_API_SECRET
- assert CONFIG.AZURE_TTS_SUBSCRIPTION_KEY and CONFIG.AZURE_TTS_SUBSCRIPTION_KEY != "YOUR_API_KEY"
- assert CONFIG.AZURE_TTS_REGION
+ assert config.IFLYTEK_APP_ID
+ assert config.IFLYTEK_API_KEY
+ assert config.IFLYTEK_API_SECRET
+ assert config.AZURE_TTS_SUBSCRIPTION_KEY and config.AZURE_TTS_SUBSCRIPTION_KEY != "YOUR_API_KEY"
+ assert config.AZURE_TTS_REGION
+ i = config.copy()
# test azure
- data = await text_to_speech("panda emoji")
+ data = await text_to_speech(
+ "panda emoji",
+ subscription_key=i.AZURE_TTS_SUBSCRIPTION_KEY,
+ region=i.AZURE_TTS_REGION,
+ iflytek_api_key=i.IFLYTEK_API_KEY,
+ iflytek_api_secret=i.IFLYTEK_API_SECRET,
+ iflytek_app_id=i.IFLYTEK_APP_ID,
+ )
assert "base64" in data or "http" in data
# test iflytek
## Mock session env
- old_options = CONFIG.options.copy()
- new_options = old_options.copy()
- new_options["AZURE_TTS_SUBSCRIPTION_KEY"] = ""
- CONFIG.set_context(new_options)
- try:
- data = await text_to_speech("panda emoji")
- assert "base64" in data or "http" in data
- finally:
- CONFIG.set_context(old_options)
+ i.AZURE_TTS_SUBSCRIPTION_KEY = ""
+ data = await text_to_speech(
+ "panda emoji",
+ subscription_key=i.AZURE_TTS_SUBSCRIPTION_KEY,
+ region=i.AZURE_TTS_REGION,
+ iflytek_api_key=i.IFLYTEK_API_KEY,
+ iflytek_api_secret=i.IFLYTEK_API_SECRET,
+ iflytek_app_id=i.IFLYTEK_APP_ID,
+ )
+ assert "base64" in data or "http" in data
if __name__ == "__main__":
diff --git a/tests/metagpt/tools/test_azure_tts.py b/tests/metagpt/tools/test_azure_tts.py
index a33925a5c..e856d3b27 100644
--- a/tests/metagpt/tools/test_azure_tts.py
+++ b/tests/metagpt/tools/test_azure_tts.py
@@ -11,7 +11,6 @@
import pytest
from azure.cognitiveservices.speech import ResultReason
-from metagpt.config import CONFIG
from metagpt.config2 import config
from metagpt.tools.azure_tts import AzureTTS
@@ -19,8 +18,8 @@ from metagpt.tools.azure_tts import AzureTTS
@pytest.mark.asyncio
async def test_azure_tts():
# Prerequisites
- assert CONFIG.AZURE_TTS_SUBSCRIPTION_KEY and CONFIG.AZURE_TTS_SUBSCRIPTION_KEY != "YOUR_API_KEY"
- assert CONFIG.AZURE_TTS_REGION
+ assert config.AZURE_TTS_SUBSCRIPTION_KEY and config.AZURE_TTS_SUBSCRIPTION_KEY != "YOUR_API_KEY"
+ assert config.AZURE_TTS_REGION
azure_tts = AzureTTS(subscription_key="", region="")
text = """
diff --git a/tests/metagpt/tools/test_iflytek_tts.py b/tests/metagpt/tools/test_iflytek_tts.py
index 58d8a83ce..18af0a723 100644
--- a/tests/metagpt/tools/test_iflytek_tts.py
+++ b/tests/metagpt/tools/test_iflytek_tts.py
@@ -7,22 +7,22 @@
"""
import pytest
-from metagpt.config import CONFIG
+from metagpt.config2 import config
from metagpt.tools.iflytek_tts import oas3_iflytek_tts
@pytest.mark.asyncio
async def test_tts():
# Prerequisites
- assert CONFIG.IFLYTEK_APP_ID
- assert CONFIG.IFLYTEK_API_KEY
- assert CONFIG.IFLYTEK_API_SECRET
+ assert config.IFLYTEK_APP_ID
+ assert config.IFLYTEK_API_KEY
+ assert config.IFLYTEK_API_SECRET
result = await oas3_iflytek_tts(
text="你好,hello",
- app_id=CONFIG.IFLYTEK_APP_ID,
- api_key=CONFIG.IFLYTEK_API_KEY,
- api_secret=CONFIG.IFLYTEK_API_SECRET,
+ app_id=config.IFLYTEK_APP_ID,
+ api_key=config.IFLYTEK_API_KEY,
+ api_secret=config.IFLYTEK_API_SECRET,
)
assert result
diff --git a/tests/metagpt/tools/test_web_browser_engine_playwright.py b/tests/metagpt/tools/test_web_browser_engine_playwright.py
index 0f2679531..32019bad9 100644
--- a/tests/metagpt/tools/test_web_browser_engine_playwright.py
+++ b/tests/metagpt/tools/test_web_browser_engine_playwright.py
@@ -4,7 +4,7 @@
import pytest
-from metagpt.config import CONFIG
+from metagpt.config2 import config
from metagpt.tools import web_browser_engine_playwright
from metagpt.utils.parse_html import WebPage
@@ -20,11 +20,11 @@ from metagpt.utils.parse_html import WebPage
ids=["chromium-normal", "firefox-normal", "webkit-normal"],
)
async def test_scrape_web_page(browser_type, use_proxy, kwagrs, url, urls, proxy, capfd):
- global_proxy = CONFIG.global_proxy
+ global_proxy = config.proxy
try:
if use_proxy:
server, proxy = await proxy
- CONFIG.global_proxy = proxy
+ config.proxy = proxy
browser = web_browser_engine_playwright.PlaywrightWrapper(browser_type=browser_type, **kwagrs)
result = await browser.run(url)
assert isinstance(result, WebPage)
@@ -39,7 +39,7 @@ async def test_scrape_web_page(browser_type, use_proxy, kwagrs, url, urls, proxy
server.close()
assert "Proxy:" in capfd.readouterr().out
finally:
- CONFIG.global_proxy = global_proxy
+ config.proxy = global_proxy
if __name__ == "__main__":
diff --git a/tests/metagpt/tools/test_web_browser_engine_selenium.py b/tests/metagpt/tools/test_web_browser_engine_selenium.py
index 8fe365352..bd5abcb9b 100644
--- a/tests/metagpt/tools/test_web_browser_engine_selenium.py
+++ b/tests/metagpt/tools/test_web_browser_engine_selenium.py
@@ -4,7 +4,7 @@
import pytest
-from metagpt.config import CONFIG
+from metagpt.config2 import config
from metagpt.tools import web_browser_engine_selenium
from metagpt.utils.parse_html import WebPage
@@ -23,11 +23,11 @@ async def test_scrape_web_page(browser_type, use_proxy, url, urls, proxy, capfd)
# Prerequisites
# firefox, chrome, Microsoft Edge
- global_proxy = CONFIG.global_proxy
+ global_proxy = config.proxy
try:
if use_proxy:
server, proxy = await proxy
- CONFIG.global_proxy = proxy
+ config.proxy = proxy
browser = web_browser_engine_selenium.SeleniumWrapper(browser_type=browser_type)
result = await browser.run(url)
assert isinstance(result, WebPage)
@@ -42,7 +42,7 @@ async def test_scrape_web_page(browser_type, use_proxy, url, urls, proxy, capfd)
server.close()
assert "Proxy:" in capfd.readouterr().out
finally:
- CONFIG.global_proxy = global_proxy
+ config.proxy = global_proxy
if __name__ == "__main__":
diff --git a/tests/metagpt/utils/test_mermaid.py b/tests/metagpt/utils/test_mermaid.py
index 6345e9c51..367223332 100644
--- a/tests/metagpt/utils/test_mermaid.py
+++ b/tests/metagpt/utils/test_mermaid.py
@@ -8,7 +8,6 @@
import pytest
-from metagpt.config import CONFIG
from metagpt.context import CONTEXT
from metagpt.utils.common import check_cmd_exists
from metagpt.utils.mermaid import MMC1, mermaid_to_file
@@ -22,9 +21,8 @@ async def test_mermaid(engine):
# playwright prerequisites: playwright install --with-deps chromium
assert check_cmd_exists("npm") == 0
- CONFIG.mermaid_engine = engine
- save_to = CONTEXT.git_repo.workdir / f"{CONFIG.mermaid_engine}/1"
- await mermaid_to_file(MMC1, save_to)
+ save_to = CONTEXT.git_repo.workdir / f"{engine}/1"
+ await mermaid_to_file(engine, MMC1, save_to)
# ink does not support pdf
if engine == "ink":