以examples/build_customized_multi_agents.py为例
一、team.hire([roles]):招募role
1、代码
team.hire(
[
SimpleCoder(),
SimpleTester(),
SimpleReviewer(is_human=add_human),
]
)
2、 roles之间的动作触发
team中的各role通过watch监听与自己有关的行动触发
① class SimpleCoder(Role):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self._watch([UserRequirement]) ###Coder的行动由用户需求触发
self.set_actions([SimpleWriteCode])
② class SimpleTester(Role):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.set_actions([SimpleWriteTest])
self._watch([SimpleWriteCode])
③ class SimpleReviewer(Role):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.set_actions([SimpleWriteReview])
self._watch([SimpleWriteTest])
二、team.run_project(idea):传入用户指令idea
1、代码
def run_project(self, idea, send_to: str = ""):
"""Run a project from publishing user requirement."""
self.idea = idea
# Human requirement.
self.env.publish_message(
Message(role="Human", content=idea, cause_by=UserRequirement, send_to=send_to or MESSAGE_ROUTE_TO_ALL),
peekable=False,
)
这里将idea作为用户需求,并发送到环境中,由此SimpleCoder监测到UserRequirement,从而触发SimpleCoder的动作
三、await team.run(n_round=n_round)
1、代码
async def run(self, n_round=3, idea="", send_to="", auto_archive=True):
"""Run company until target round or no money"""
if idea:
self.run_project(idea=idea, send_to=send_to)
while n_round > 0: ###执行n_round轮
n_round -= 1
self._check_balance()
await self.env.run()
logger.debug(f"max {n_round=} left.")
self.env.archive(auto_archive) ###是否已经达成目标
return self.env.history