Skip to content

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

add AgentCreator and some minor formatting

yzlinfa8eb1745169
diff --git a/examples/agent_creator.py b/examples/agent_creator.py
new file mode 100644
index 000000000..748dc02b1
--- /dev/null
+++ b/examples/agent_creator.py
@@ -0,0 +1,99 @@
+'''
+Filename: MetaGPT/examples/agent_creator.py
+Created Date: Tuesday, September 12th 2023, 3:28:37 pm
+Author: garylin2099
+'''
+import re
+
+from metagpt.const import PROJECT_ROOT, WORKSPACE_ROOT
+from metagpt.actions import Action
+from metagpt.roles import Role
+from metagpt.schema import Message
+from metagpt.logs import logger
+
+with open(PROJECT_ROOT / "examples/build_customized_agent.py", "r") as f:
+ # use official example script to guide AgentCreator
+ MULTI_ACTION_AGENT_CODE_EXAMPLE = f.read()
+
+class CreateAgent(Action):
+
+ PROMPT_TEMPLATE = """
+ ### BACKGROUND
+ You are using an agent framework called metagpt to write agents capable of different actions,
+ the usage of metagpt can be illustrated by the following example:
+ ### EXAMPLE STARTS AT THIS LINE
+ {example}
+ ### EXAMPLE ENDS AT THIS LINE
+ ### TASK
+ Now you should create an agent with appropriate actions based on the instruction, consider carefully about
+ the PROMPT_TEMPLATE of all actions and when to call self._aask()
+ ### INSTRUCTION
+ {instruction}
+ ### YOUR CODE
+ Return ```python your_code_here ``` with NO other texts, your code:
+ """
+
+ async def run(self, example: str, instruction: str):
+
+ prompt = self.PROMPT_TEMPLATE.format(example=example, instruction=instruction)
+ # logger.info(prompt)
+
+ rsp = await self._aask(prompt)
+
+ code_text = CreateAgent.parse_code(rsp)
+
+ return code_text
+
+ @staticmethod
+ def parse_code(rsp):
+ pattern = r'```python(.*)```'
+ match = re.search(pattern, rsp, re.DOTALL)
+ code_text = match.group(1) if match else ""
+ with open(WORKSPACE_ROOT / "agent_created_agent.py", "w") as f:
+ f.write(code_text)
+ return code_text
+
+class AgentCreator(Role):
+ def __init__(
+ self,
+ name: str = "Matrix",
+ profile: str = "AgentCreator",
+ agent_template: str = MULTI_ACTION_AGENT_CODE_EXAMPLE,
+ **kwargs,
+ ):
+ super().__init__(name, profile, **kwargs)
+ self._init_actions([CreateAgent])
+ self.agent_template = agent_template
+
+ async def _act(self) -> Message:
+ logger.info(f"{self._setting}: ready to {self._rc.todo}")
+ todo = self._rc.todo
+ msg = self._rc.memory.get()[-1]
+
+ instruction = msg.content
+ code_text = await CreateAgent().run(example=self.agent_template, instruction=instruction)
+ msg = Message(content=code_text, role=self.profile, cause_by=todo)
+
+ return msg
+
+if __name__ == "__main__":
+ import asyncio
+
+ async def main():
+
+ agent_template = MULTI_ACTION_AGENT_CODE_EXAMPLE
+
+ creator = AgentCreator(agent_template=agent_template)
+
+ # msg = """Write an agent called SimpleTester that will take any code snippet (str) and return a testing code (str) for testing
+ # the given code snippet. Use pytest as the testing framework."""
+
+ msg = """
+ Write an agent called SimpleTester that will take any code snippet (str) and do the following:
+ 1. write a testing code (str) for testing the given code snippet, save the testing code as a .py file in the current working diretory;
+ 2. run the testing code.
+ You can use pytest as the testing framework.
+ """
+ result = await creator.run(msg)
+
+ asyncio.run(main())
diff --git a/examples/build_customized_agent.py b/examples/build_customized_agent.py
index 7122a9858..312ffa4d5 100644
--- a/examples/build_customized_agent.py
+++ b/examples/build_customized_agent.py
@@ -1,3 +1,8 @@
+'''
+Filename: MetaGPT/examples/build_customized_agent.py
+Created Date: Tuesday, September 19th 2023, 6:52:25 pm
+Author: garylin2099
+'''
import re
import subprocess
diff --git a/examples/debate.py b/examples/debate.py
index c98705fcc..4e90edd20 100644
--- a/examples/debate.py
+++ b/examples/debate.py
@@ -1,3 +1,8 @@
+'''
+Filename: MetaGPT/examples/debate.py
+Created Date: Tuesday, September 19th 2023, 6:52:25 pm
+Author: garylin2099
+'''
import asyncio
import platform
import fire
@@ -8,7 +13,8 @@ from metagpt.roles import Role
from metagpt.schema import Message
from metagpt.logs import logger
-class Shout(Action):
+class ShoutOut(Action):
+ """Action: Shout out loudly in a debate (quarrel)"""
PROMPT_TEMPLATE = """
## BACKGROUND
@@ -21,7 +27,7 @@ class Shout(Action):
craft a strong and emotional response in 80 words, in {name}'s rhetoric and viewpoints, your will argue:
"""
- def __init__(self, name="Shout", context=None, llm=None):
+ def __init__(self, name="ShoutOut", context=None, llm=None):
super().__init__(name, context, llm)
async def run(self, context: str, name: str, opponent_name: str):
@@ -37,12 +43,12 @@ class Trump(Role):
def __init__(
self,
name: str = "Trump",
- profile: str = "Trump",
+ profile: str = "Republican",
**kwargs,
):
super().__init__(name, profile, **kwargs)
- self._init_actions([Shout])
- self._watch([Shout])
+ self._init_actions([ShoutOut])
+ self._watch([ShoutOut])
self.name = "Trump"
self.opponent_name = "Biden"
@@ -56,18 +62,18 @@ class Trump(Role):
async def _act(self) -> Message:
logger.info(f"{self._setting}: ready to {self._rc.todo}")
- msg_history = self._rc.memory.get_by_actions([Shout])
+ msg_history = self._rc.memory.get_by_actions([ShoutOut])
context = []
for m in msg_history:
context.append(str(m))
context = "\n".join(context)
- rsp = await Shout().run(context=context, name=self.name, opponent_name=self.opponent_name)
+ rsp = await ShoutOut().run(context=context, name=self.name, opponent_name=self.opponent_name)
msg = Message(
content=rsp,
role=self.profile,
- cause_by=Shout,
+ cause_by=ShoutOut,
sent_from=self.name,
send_to=self.opponent_name,
)
@@ -79,12 +85,12 @@ class Biden(Role):
def __init__(
self,
name: str = "Biden",
- profile: str = "Biden",
+ profile: str = "Democrat",
**kwargs,
):
super().__init__(name, profile, **kwargs)
- self._init_actions([Shout])
- self._watch([BossRequirement, Shout])
+ self._init_actions([ShoutOut])
+ self._watch([BossRequirement, ShoutOut])
self.name = "Biden"
self.opponent_name = "Trump"
@@ -98,18 +104,18 @@ class Biden(Role):
async def _act(self) -> Message:
logger.info(f"{self._setting}: ready to {self._rc.todo}")
- msg_history = self._rc.memory.get_by_actions([BossRequirement, Shout])
+ msg_history = self._rc.memory.get_by_actions([BossRequirement, ShoutOut])
context = []
for m in msg_history:
context.append(str(m))
context = "\n".join(context)
- rsp = await Shout().run(context=context, name=self.name, opponent_name=self.opponent_name)
+ rsp = await ShoutOut().run(context=context, name=self.name, opponent_name=self.opponent_name)
msg = Message(
content=rsp,
role=self.profile,
- cause_by=Shout,
+ cause_by=ShoutOut,
sent_from=self.name,
send_to=self.opponent_name,
)
@@ -119,7 +125,8 @@ class Biden(Role):
async def startup(idea: str, investment: float = 3.0, n_round: int = 5,
code_review: bool = False, run_tests: bool = False):
- """Run a startup of presidents. Watch they quarrel. :) """
+ """We reuse the startup paradigm for roles to interact with each other.
+ Now we run a startup of presidents and watch they quarrel. :) """
company = SoftwareCompany()
company.hire([Biden(), Trump()])
company.invest(investment)
@@ -127,18 +134,16 @@ async def startup(idea: str, investment: float = 3.0, n_round: int = 5,
await company.run(n_round=n_round)
-def main(idea: str, investment: float = 3.0, n_round: int = 10, code_review: bool = False, run_tests: bool = False):
+def main(idea: str, investment: float = 3.0, n_round: int = 10):
"""
- We are a software startup comprised of AI. By investing in us, you are empowering a future filled with limitless possibilities.
- :param idea: Your innovative idea, such as "Creating a snake game."
- :param investment: As an investor, you have the opportunity to contribute a certain dollar amount to this AI company.
- :param n_round:
- :param code_review: Whether to use code review.
+ :param idea: Debate topic, such as "Topic: The U.S. should commit more in climate change fighting" or "Trump: Climate change is a hoax"
+ :param investment: contribute a certain dollar amount to watch the debate
+ :param n_round: maximum rounds of the debate
:return:
"""
if platform.system() == "Windows":
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
- asyncio.run(startup(idea, investment, n_round, code_review, run_tests))
+ asyncio.run(startup(idea, investment, n_round))
if __name__ == '__main__':
diff --git a/examples/use_off_the_shelf_agent.py b/examples/use_off_the_shelf_agent.py
index 48fb2b19e..2e10068bd 100644
--- a/examples/use_off_the_shelf_agent.py
+++ b/examples/use_off_the_shelf_agent.py
@@ -1,3 +1,8 @@
+'''
+Filename: MetaGPT/examples/use_off_the_shelf_agent.py
+Created Date: Tuesday, September 19th 2023, 6:52:25 pm
+Author: garylin2099
+'''
import asyncio
from metagpt.roles.product_manager import ProductManager