use CONFIG to adapt pydantic..

This commit is contained in:
geekan 2023-07-07 10:30:53 +08:00
parent e5d70f3f21
commit 4719bbeb7f
3 changed files with 26 additions and 17 deletions

View file

@ -57,7 +57,7 @@ class Config(metaclass=Singleton):
self.google_api_key = self._get('GOOGLE_API_KEY')
self.google_cse_id = self._get('GOOGLE_CSE_ID')
self.search_engine = self._get('SEARCH_ENGINE', SearchEngineType.SERPAPI_GOOGLE)
self.max_budget = self._get('MAX_BUDGET', 10)
self.max_budget = self._get('MAX_BUDGET', 10.0)
self.total_cost = 0.0
def _init_with_config_files_and_env(self, configs: dict, yaml_file):
@ -85,3 +85,6 @@ class Config(metaclass=Singleton):
if value is None:
raise ValueError(f"Key '{key}' not found in environment variables or in the YAML file")
return value
CONFIG = Config()

View file

@ -19,7 +19,7 @@ class Environment:
"""环境,承载一批角色,角色可以向环境发布消息,可以被其他角色观察到"""
def __init__(self):
self.roles: dict[str, Role] = {}
self.message_queue = Queue()
# self.message_queue = Queue()
self.memory = Memory()
self.history = ''
@ -39,7 +39,7 @@ class Environment:
def publish_message(self, message: Message):
"""向当前环境发布信息"""
self.message_queue.put(message)
# self.message_queue.put(message)
self.memory.add(message)
self.history += f"\n{message}"

View file

@ -5,8 +5,9 @@
@Author : alexanderwu
@File : software_company.py
"""
from pydantic import BaseModel
from metagpt.config import Config
from metagpt.config import CONFIG
from metagpt.actions import BossRequirement
from metagpt.logs import logger
from metagpt.environment import Environment
@ -15,16 +16,17 @@ from metagpt.schema import Message
from metagpt.utils.common import NoMoneyException
class SoftwareCompany:
class SoftwareCompany(BaseModel):
"""
Software Company: Possesses a team, SOP (Standard Operating Procedures), and a platform for instant messaging,
dedicated to writing executable code.
"""
def __init__(self):
self.environment = Environment()
self.config = Config()
self.investment = 0
self.idea = ""
environment: Environment = Environment()
investment: float = 0
idea: str = ""
class Config:
arbitrary_types_allowed = True
def hire(self, roles: list[Role]):
"""Hire roles to cooperate"""
@ -33,24 +35,28 @@ class SoftwareCompany:
def invest(self, investment: float):
"""Invest company. raise NoMoneyException when exceed max_budget."""
self.investment = investment
self.config.max_budget = investment
CONFIG.max_budget = investment
logger.info(f'Investment: {investment}')
# logger.info(self.config)
def _check_balance(self):
if self.config.total_cost > self.config.max_budget:
raise NoMoneyException(self.config.total_cost, f'Insufficient funds: {self.config.max_budget}')
if CONFIG.total_cost > CONFIG.max_budget:
raise NoMoneyException(CONFIG.total_cost, f'Insufficient funds: {CONFIG.max_budget}')
def start_project(self, idea):
"""Start a project from publish boss requirement."""
self.idea = idea
self.environment.publish_message(Message(role="BOSS", content=idea, cause_by=BossRequirement))
def _save(self):
logger.info(self.json())
async def run(self, n_round=3):
"""Run company until target round"""
while not self.environment.message_queue.empty():
self._check_balance()
while n_round > 0:
# self._save()
n_round -= 1
logger.debug(f"{n_round=}")
if n_round == 0:
return
self._check_balance()
await self.environment.run()
return self.environment.history