Skip to content

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

Merge main branch

mannaandpoem24e617b362cf
diff --cc metagpt/actions/design_api.py
index 082474098,2574550e4..073eb20ae
--- a/metagpt/actions/design_api.py
+++ b/metagpt/actions/design_api.py
@@@ -13,10 -13,8 +13,8 @@@ import jso
from pathlib import Path
from typing import Optional
- from pydantic import Field
-
from metagpt.actions import Action, ActionOutput
-from metagpt.actions.design_api_an import DESIGN_API_NODE
+from metagpt.actions.design_api_an import DESIGN_API_NODE, REFINED_DESIGN_NODES
from metagpt.config import CONFIG
from metagpt.const import (
DATA_API_DESIGN_FILE_REPO,
@@@ -87,8 -82,8 +82,8 @@@ class WriteDesign(Action)
async def _merge(self, prd_doc, system_design_doc, schema=CONFIG.prompt_schema):
context = NEW_REQ_TEMPLATE.format(old_design=system_design_doc.content, context=prd_doc.content)
- node = await DESIGN_API_NODE.fill(context=context, llm=self.llm, schema=schema)
+ node = await REFINED_DESIGN_NODES.fill(context=context, llm=self.llm, schema=schema)
- system_design_doc.content = node.instruct_content.json(ensure_ascii=False)
+ system_design_doc.content = node.instruct_content.model_dump_json()
return system_design_doc
async def _update_system_design(self, filename, prds_file_repo, system_design_file_repo) -> Document:
diff --cc metagpt/actions/design_api_an.py
index 3e8265e95,3737203cf..0833297f1
--- a/metagpt/actions/design_api_an.py
+++ b/metagpt/actions/design_api_an.py
@@@ -8,8 -8,7 +8,7 @@@
from typing import List
from metagpt.actions.action_node import ActionNode
- from metagpt.logs import logger
-from metagpt.utils.mermaid import MMC1, MMC2
+from metagpt.utils.mermaid import MMC1, MMC1_REFINE, MMC2, MMC2_REFINE
IMPLEMENTATION_APPROACH = ActionNode(
key="Implementation approach",
@@@ -144,27 -61,4 +143,16 @@@ NODES =
ANYTHING_UNCLEAR,
]
+INC_NODES = [INCREMENTAL_IMPLEMENTATION_APPROACH, INCREMENTAL_DATA_STRUCTURES_AND_INTERFACES, REFINED_PROGRAM_CALL_FLOW]
+
+REFINE_NODES = [
+ REFINED_IMPLEMENTATION_APPROACH,
+ FILE_LIST,
+ REFINED_DATA_STRUCTURES_AND_INTERFACES,
+ REFINED_PROGRAM_CALL_FLOW,
+ ANYTHING_UNCLEAR,
+]
+
DESIGN_API_NODE = ActionNode.from_children("DesignAPI", NODES)
+INCREMENTAL_DESIGN_NODES = ActionNode.from_children("Incremental_Design_API", INC_NODES)
+REFINED_DESIGN_NODES = ActionNode.from_children("Refined_Design_API", REFINE_NODES)
-
-
- def main():
- prompt = DESIGN_API_NODE.compile(context="")
- logger.info(prompt)
- prompt = REFINED_DESIGN_NODES.compile(context="")
- logger.info(prompt)
-
-
- if __name__ == "__main__":
- main()
diff --cc metagpt/actions/project_management.py
index f124ba8df,e40c2034b..3c379d96f
--- a/metagpt/actions/project_management.py
+++ b/metagpt/actions/project_management.py
@@@ -13,11 -13,9 +13,9 @@@
import json
from typing import Optional
- from pydantic import Field
-
from metagpt.actions import ActionOutput
from metagpt.actions.action import Action
-from metagpt.actions.project_management_an import PM_NODE
+from metagpt.actions.project_management_an import PM_NODE, REFINED_PM_NODES
from metagpt.config import CONFIG
from metagpt.const import (
PACKAGE_REQUIREMENTS_FILENAME,
@@@ -101,8 -93,8 +93,8 @@@ class WriteTasks(Action)
async def _merge(self, system_design_doc, task_doc, schema=CONFIG.prompt_schema) -> Document:
context = NEW_REQ_TEMPLATE.format(context=system_design_doc.content, old_tasks=task_doc.content)
- node = await PM_NODE.fill(context, self.llm, schema)
+ node = await REFINED_PM_NODES.fill(context, self.llm, schema)
- task_doc.content = node.instruct_content.json(ensure_ascii=False)
+ task_doc.content = node.instruct_content.model_dump_json()
return task_doc
@staticmethod
diff --cc metagpt/actions/write_prd.py
index a070e7a96,d51c0a7be..84a9fa13d
--- a/metagpt/actions/write_prd.py
+++ b/metagpt/actions/write_prd.py
@@@ -23,8 -21,7 +21,9 @@@ from metagpt.actions import Action, Act
from metagpt.actions.action_node import ActionNode
from metagpt.actions.fix_bug import FixBug
from metagpt.actions.write_prd_an import (
+ PROJECT_NAME,
+ REFINE_PRD_NODE,
+ REFINE_PRD_TEMPLATE,
WP_IS_RELATIVE_NODE,
WP_ISSUE_TYPE_NODE,
WRITE_PRD_NODE,
@@@ -137,15 -132,9 +134,13 @@@ class WritePRD(Action)
async def _merge(self, new_requirement_doc, prd_doc, schema=CONFIG.prompt_schema) -> Document:
if not CONFIG.project_name:
CONFIG.project_name = Path(CONFIG.project_path).name
-
- project_name = CONFIG.project_name if CONFIG.project_name else ""
- prompt = NEW_REQ_TEMPLATE.format(requirements=new_requirement_doc.content, old_prd=prd_doc.content)
- node = await WRITE_PRD_NODE.fill(context=prompt, llm=self.llm, schema=schema)
+ prompt = REFINE_PRD_TEMPLATE.format(
+ requirements=new_requirement_doc.content,
+ old_prd=prd_doc.content,
- project_name=project_name,
++ project_name=CONFIG.project_name,
+ )
+ node = await REFINE_PRD_NODE.fill(context=prompt, llm=self.llm, schema=schema)
- prd_doc.content = node.instruct_content.json(ensure_ascii=False)
+ prd_doc.content = node.instruct_content.model_dump_json()
await self._rename_workspace(node)
return prd_doc
diff --cc metagpt/roles/engineer.py
index fe2d369cb,e05e69cbb..eeade5856
--- a/metagpt/roles/engineer.py
+++ b/metagpt/roles/engineer.py
@@@ -107,10 -106,10 +111,10 @@@ class Engineer(Role)
3. Do we need other codes (currently needed)?
TODO: The goal is not to need it. After clear task decomposition, based on the design idea, you should be able to write a single file without needing other codes. If you can't, it means you need a clearer definition. This is the key to writing longer code.
"""
- coding_context = await todo.run()
+ coding_context = await todo.run(guideline=guideline)
# Code review
if review:
- action = WriteCodeReview(context=coding_context, llm=self._llm)
+ action = WriteCodeReview(context=coding_context, llm=self.llm)
self._init_action_system_message(action)
coding_context = await action.run()
await src_file_repo.save(
@@@ -307,34 -311,11 +320,39 @@@
summarizations[ctx].append(filename)
for ctx, filenames in summarizations.items():
ctx.codes_filenames = filenames
- self.summarize_todos.append(SummarizeCode(context=ctx, llm=self._llm))
+ self.summarize_todos.append(SummarizeCode(context=ctx, llm=self.llm))
if self.summarize_todos:
- self._rc.todo = self.summarize_todos[0]
+ self.rc.todo = self.summarize_todos[0]
+
+ @property
+ def todo(self) -> str:
+ """AgentStore uses this attribute to display to the user what actions the current role should take."""
+ return self.next_todo_action
+
+ async def _write_code_guideline(self):
+ logger.info("Writing code guideline..")
+
+ requirement = str(self._rc.memory.get_by_role("Human")[0])
+ # prd_file_repo = CONFIG.git_repo.new_file_repository(PRDS_FILE_REPO)
+ design_file_repo = CONFIG.git_repo.new_file_repository(SYSTEM_DESIGN_FILE_REPO)
+ task_file_repo = CONFIG.git_repo.new_file_repository(TASK_FILE_REPO)
+ # prd = await prd_file_repo.get_all()
+ # prd = "\n".join([doc.content for doc in prd])
+ design = await design_file_repo.get_all()
+ design = "\n".join([doc.content for doc in design])
+ tasks = await task_file_repo.get_all()
+ tasks = "\n".join([doc.content for doc in tasks])
+ old_codes = await self.get_old_codes()
+
+ context = CODE_GUIDELINE_CONTEXT.format(requirement=requirement, tasks=tasks, design=design, code=old_codes)
+ node = await WriteCodeGuideline().run(context=context)
+ guideline = node.instruct_content.json(ensure_ascii=False)
+ return guideline
+
+ @staticmethod
+ async def get_old_codes() -> str:
+ CONFIG.old_workspace = CONFIG.git_repo.workdir / os.path.basename(CONFIG.project_path)
+ old_file_repo = CONFIG.git_repo.new_file_repository(relative_path=CONFIG.old_workspace)
+ old_codes = await old_file_repo.get_all()
+ codes = [f"----- {code.filename}\n```{code.content}```" for code in old_codes]
+ return "\n".join(codes)
diff --cc metagpt/utils/mermaid.py
index 76adbf5a7,235b4979c..25c593e6c
--- a/metagpt/utils/mermaid.py
+++ b/metagpt/utils/mermaid.py
@@@ -9,8 -10,9 +9,7 @@@ import asynci
import os
from pathlib import Path
-import aiofiles
-
from metagpt.config import CONFIG
- from metagpt.const import METAGPT_ROOT
from metagpt.logs import logger
from metagpt.utils.common import check_cmd_exists
@@@ -118,47 -123,11 +118,51 @@@ classDiagra
SearchEngine --> Index
SearchEngine --> Ranking
SearchEngine --> Summary
- Index --> KnowledgeBase"""
+ Index --> KnowledgeBase
+ """
- MMC1_REFINE = """classDiagram
++MMC1_REFINE = """
++classDiagram
+ class Main {
+ -SearchEngine search_engine
+ +main() str
+ +newMethod() str # Incremental change
+ }
+ class SearchEngine {
+ -Index index
+ -Ranking ranking
+ -Summary summary
+ +search(query: str) str
+ +newMethod() str # Incremental change
+ }
+ class Index {
+ -KnowledgeBase knowledge_base
+ +create_index(data: dict)
+ +query_index(query: str) list
+ +newMethod() list # Incremental change
+ }
+ class Ranking {
+ +rank_results(results: list) list
+ +newMethod() list # Incremental change
+ }
+ class Summary {
+ +summarize_results(results: list) str
+ +newMethod() str # Incremental change
+ }
+ class KnowledgeBase {
+ +update(data: dict)
+ +fetch_data(query: str) dict
+ +newMethod() # Incremental change
+ }
+ Main --> SearchEngine
+ SearchEngine --> Index
+ SearchEngine --> Ranking
+ SearchEngine --> Summary
- Index --> KnowledgeBase"""
++ Index --> KnowledgeBase
++"""
+
- MMC2 = """sequenceDiagram
+ MMC2 = """
+ sequenceDiagram
participant M as Main
participant SE as SearchEngine
participant I as Index
@@@ -174,37 -143,5 +178,34 @@@
R-->>SE: return ranked_results
SE->>S: summarize_results(ranked_results)
S-->>SE: return summary
- SE-->>M: return summary"""
+ SE-->>M: return summary
+ """
+
- MMC2_REFINE = """sequenceDiagram
++MMC2_REFINE = """
++sequenceDiagram
+ participant M as Main
+ participant SE as SearchEngine
+ participant I as Index
+ participant R as Ranking
+ participant S as Summary
+ participant KB as KnowledgeBase
+ M->>SE: search(query)
+ SE->>I: query_index(query)
+ I->>KB: fetch_data(query)
+ KB-->>I: return data
+ I-->>SE: return results
+ SE->>R: rank_results(results)
+ R-->>SE: return ranked_results
+ SE->>S: summarize_results(ranked_results)
+ S-->>SE: return summary
+ SE-->>M: return summary
+ M->>SE: newMethod() # Incremental change
+ SE->>I: newMethod() # Incremental change
+ I->>KB: newMethod() # Incremental change
+ KB-->>I: newMethod() # Incremental change
+ SE->>R: newMethod() # Incremental change
+ R-->>SE: newMethod() # Incremental change
+ SE->>S: newMethod() # Incremental change
+ S-->>SE: newMethod() # Incremental change
- SE-->>M: newMethod() # Incremental change"""
-
- if __name__ == "__main__":
- loop = asyncio.new_event_loop()
- result = loop.run_until_complete(mermaid_to_file(MMC1, METAGPT_ROOT / f"{CONFIG.mermaid_engine}/1"))
- result = loop.run_until_complete(mermaid_to_file(MMC2, METAGPT_ROOT / f"{CONFIG.mermaid_engine}/1"))
- loop.close()
++ SE-->>M: newMethod() # Incremental change
++"""