feat: replace CONFIG with OPTIONS

This commit is contained in:
莘权 马 2023-08-28 19:07:59 +08:00
parent 23ba0f3540
commit 7895af2c5a
8 changed files with 24 additions and 17 deletions

View file

@ -77,9 +77,9 @@ async def startup(lesson_file: str, investment: float = 3.0, n_round: int = 1, *
lesson = demo_lesson
company = SoftwareCompany()
company.hire([Teacher(options=company.options, cost_manager=company.cost_manager, *args, **kwargs)])
company.hire([Teacher(*args, **kwargs)])
company.invest(investment)
company.start_project(lesson, role="Teacher", cause_by=TeachingPlanRequirement)
company.start_project(lesson, cause_by=TeachingPlanRequirement, role="Teacher", **kwargs)
await company.run(n_round=1)

View file

@ -9,6 +9,7 @@
import pydantic
from metagpt.actions import Action
from metagpt.config import CONFIG
from metagpt.logs import logger
from metagpt.schema import Message
from metagpt.tools.search_engine import SearchEngine

View file

@ -20,7 +20,7 @@ class TeachingPlanRequirement(Action):
class WriteTeachingPlanPart(Action):
"""Write Teaching Plan Part"""
def __init__(self, options, name: str = "", context=None, llm=None, topic: str = "", language: str = "Chinese"):
def __init__(self, name: str = "", context=None, llm=None, topic: str = "", language: str = "Chinese"):
"""
:param name: action name
@ -29,7 +29,7 @@ class WriteTeachingPlanPart(Action):
:param topic: topic part of teaching plan
:param language: A human language, such as Chinese, English, French, etc.
"""
super().__init__(options, name, context, llm)
super().__init__(name, context, llm)
self.topic = topic
self.language = language
self.rsp = None

View file

@ -126,4 +126,9 @@ class Config(metaclass=Singleton):
opts.update(options)
OPTIONS.set(opts)
@property
def options(self):
"""Return all key-values"""
return OPTIONS.get()
CONFIG = Config()

View file

@ -67,7 +67,7 @@ class CostManager(BaseModel):
total_prompt_tokens: int = 0
total_completion_tokens: int = 0
total_budget: float = 0
max_budget: float
max_budget: float = CONFIG.max_budget
total_cost: float = 0
def update_cost(self, prompt_tokens, completion_tokens, model):
@ -135,7 +135,7 @@ class OpenAIGPTAPI(BaseGPTAPI, RateLimiter):
Check https://platform.openai.com/examples for examples
"""
def __init__(self, cost_manager):
def __init__(self, cost_manager=None):
self.__init_openai(CONFIG)
self.llm = openai
self.model = CONFIG.openai_api_model

View file

@ -10,7 +10,8 @@
For more about `fork` node in activity diagrams, see: `https://www.uml-diagrams.org/activity-diagrams.html`
This file defines a `fork` style meta role capable of generating arbitrary roles at runtime based on a
configuration file.
@Modified By: mashenquan, 2023/8/22. A definition has been provided for the return value of _think: returning false indicates that further reasoning cannot continue.
@Modified By: mashenquan, 2023/8/22. A definition has been provided for the return value of _think: returning false
indicates that further reasoning cannot continue.
"""
import asyncio
@ -34,7 +35,7 @@ SKILL_PATH = "SKILL_PATH"
class Assistant(Role):
"""解决通用问题的助手"""
"""Assistant for solving common issues."""
def __init__(self, options, cost_manager, name="Lily", profile="An assistant", goal="Help to solve problem",
constraints="Talk in {language}", desc="", *args, **kwargs):
@ -152,7 +153,7 @@ async def main():
break
msg = await role.act()
logger.info(msg)
# 获取用户终端输入
# Retrieve user terminal input.
logger.info("Enter prompt")
talk = input("You: ")
await role.talk(talk)

View file

@ -42,10 +42,10 @@ class SoftwareCompany(BaseModel):
if CONFIG.total_cost > CONFIG.max_budget:
raise NoMoneyException(CONFIG.total_cost, f'Insufficient funds: {CONFIG.max_budget}')
def start_project(self, idea):
def start_project(self, idea, role="BOSS", cause_by=BossRequirement, **kwargs):
"""Start a project from publishing boss requirement."""
self.idea = idea
self.environment.publish_message(Message(role="BOSS", content=idea, cause_by=BossRequirement))
self.environment.publish_message(Message(content=idea, role=role, cause_by=cause_by))
def _save(self):
logger.info(self.json())

View file

@ -11,6 +11,7 @@ from __future__ import annotations
import importlib
from typing import Callable, Coroutine, Literal, overload, Dict
from metagpt.config import CONFIG
from metagpt.tools import SearchEngineType
@ -28,23 +29,22 @@ class SearchEngine:
def __init__(
self,
options: Dict,
engine: SearchEngineType | None = None,
run_func: Callable[[str, int, bool], Coroutine[None, None, str | list[str]]] = None
):
engine = engine or options.get("search_engine")
engine = engine or CONFIG.search_engine
if engine == SearchEngineType.SERPAPI_GOOGLE:
module = "metagpt.tools.search_engine_serpapi"
run_func = importlib.import_module(module).SerpAPIWrapper(**options).run
run_func = importlib.import_module(module).SerpAPIWrapper(**CONFIG.options).run
elif engine == SearchEngineType.SERPER_GOOGLE:
module = "metagpt.tools.search_engine_serper"
run_func = importlib.import_module(module).SerperWrapper(**options).run
run_func = importlib.import_module(module).SerperWrapper(**CONFIG.options).run
elif engine == SearchEngineType.DIRECT_GOOGLE:
module = "metagpt.tools.search_engine_googleapi"
run_func = importlib.import_module(module).GoogleAPIWrapper(**options).run
run_func = importlib.import_module(module).GoogleAPIWrapper(**CONFIG.options).run
elif engine == SearchEngineType.DUCK_DUCK_GO:
module = "metagpt.tools.search_engine_ddg"
run_func = importlib.import_module(module).DDGAPIWrapper(**options).run
run_func = importlib.import_module(module).DDGAPIWrapper(**CONFIG.options).run
elif engine == SearchEngineType.CUSTOM_ENGINE:
pass # run_func = run_func
else: