Merge branch 'mgx_ops' of https://gitlab.deepwisdomai.com/pub/MetaGPT into check_role_zero

This commit is contained in:
garylin2099 2024-08-14 20:40:47 +08:00
commit 2583fad6a8
51 changed files with 277 additions and 176 deletions

View file

@ -566,7 +566,7 @@ def general_after_log(i: "loguru.Logger", sec_format: str = "%0.3f") -> Callable
return log_it
def read_json_file(json_file: str, encoding="utf-8") -> list[Any]:
def read_json_file(json_file: str, encoding: str = "utf-8") -> list[Any]:
if not Path(json_file).exists():
raise FileNotFoundError(f"json_file: {json_file} not exist, return []")
@ -595,7 +595,7 @@ def handle_unknown_serialization(x: Any) -> str:
return f"<Unserializable {type(x).__name__} object>"
def write_json_file(json_file: str, data: Any, encoding: str = None, indent: int = 4, use_fallback: bool = False):
def write_json_file(json_file: str, data: Any, encoding: str = "utf-8", indent: int = 4, use_fallback: bool = False):
folder_path = Path(json_file).parent
if not folder_path.exists():
folder_path.mkdir(parents=True, exist_ok=True)

View file

@ -7,10 +7,11 @@
"""
from llama_index.embeddings.openai import OpenAIEmbedding
from metagpt.config2 import config
from metagpt.config2 import Config
def 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'.")

View file

@ -13,10 +13,11 @@ from semantic_kernel.connectors.ai.open_ai.services.open_ai_chat_completion impo
OpenAIChatCompletion,
)
from metagpt.config2 import config
from metagpt.config2 import Config
def make_sk_kernel():
config = Config.default()
kernel = sk.Kernel()
if llm := config.get_azure_llm():
kernel.add_chat_service(

View file

@ -9,12 +9,14 @@ import asyncio
import os
from pathlib import Path
from metagpt.config2 import config
from metagpt.config2 import Config
from metagpt.logs import logger
from 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)

View file

@ -10,11 +10,11 @@ from urllib.parse import urljoin
from pyppeteer import launch
from metagpt.config2 import config
from metagpt.config2 import Config
from 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__))

View file

@ -4,12 +4,12 @@
import copy
from enum import Enum
from typing import Callable, Union
from typing import Callable, Optional, Union
import regex as re
from tenacity import RetryCallState, retry, stop_after_attempt, wait_fixed
from metagpt.config2 import config
from metagpt.config2 import Config
from metagpt.logs import logger
from 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: RepairT
target: { 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[["R
return 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),
)