format and add fixed sop compatibility

This commit is contained in:
garylin2099 2024-06-05 14:39:41 +08:00
parent c6b28643bd
commit 89ad59876b
4 changed files with 128 additions and 52 deletions

View file

@ -1,22 +1,19 @@
from __future__ import annotations
import asyncio
from pydantic import model_validator
from metagpt.prompts.di.engineer2 import ENGINEER2_INSTRUCTION
from metagpt.roles.di.role_zero import RoleZero
from metagpt.tools.libs.editor import Editor
from test3 import design_doc_2048, design_doc_snake, task_doc_2048, task_doc_snake
class Engineer2(RoleZero):
name: str = "Alex"
profile: str = "Engineer"
goal: str = "Take on game, app, and web development"
tools: str = ["Plan", "Editor:write,read,write_content", "RoleZero"]
instruction: str = ENGINEER2_INSTRUCTION
tools: str = ["Plan", "Editor:write,read,write_content", "RoleZero"]
editor: Editor = Editor()
@model_validator(mode="after")
@ -32,44 +29,3 @@ class Engineer2(RoleZero):
"RoleZero.reply_to_human": self.reply_to_human,
}
return self
GAME_REQ_2048 = f"""
Create a 2048 game, follow the design doc and task doc. Write your code under /Users/gary/Files/temp/workspace/2048_game/src.
After writing all codes, write a code review for the codes, make improvement or adjustment based on the review.
Notice: You MUST implement the full code, don't leave comment without implementation!
Design doc:
{task_doc_2048}
Task doc:
{design_doc_2048}
"""
GAME_REQ_SNAKE = f"""
Create a snake game, follow the design doc and task doc. Write your code under /Users/gary/Files/temp/workspace/snake_game/src.
After writing all codes, write a code review for the codes, make improvement or adjustment based on the review.
Notice: You MUST implement the full code, don't leave comment without implementation!
Design doc:
{task_doc_snake}
Task doc:
{design_doc_snake}
"""
GAME_REQ_2048_NO_DOC = """
Create a 2048 game with pygame. Write your code under /Users/gary/Files/temp/workspace/2048_game/src.
Consider what files you will write, break down the requests to multiple tasks and write one file in each task.
After writing all codes, write a code review for the codes, make improvement or adjustment based on the review.
Notice: You MUST implement the full code, don't leave comment without implementation!
"""
GAME_INC_REQ_2048 = """
I found an issue with the 2048 code: when tiles are merged, no new tiles pop up.
Write code review for the codes (game.py, main.py, ui.py) under under /Users/gary/Files/temp/workspace/2048_game_bugs/src.
Then correct any issues you find. You can review all code in one time, and solve issues in one time.
"""
GAME_INC_REQ_SNAKE = """
Found this issue, TypeError: generate_new_position() missing 1 required positional argument: 'snake_body'
Write code review for the codes (food.py, game.py, main.py, snake.py, ui.py) under under /Users/gary/Files/temp/workspace/snake_game_bugs/src.
Then correct any issues you find. You can review all code in one time, and solve issues in one time.
"""
CASUAL_CHAT = """what's your name?"""
if __name__ == "__main__":
engineer2 = Engineer2()
asyncio.run(engineer2.run(GAME_REQ_2048_NO_DOC))

View file

@ -23,8 +23,9 @@ from metagpt.utils.common import CodeParser
@register_tool(include_functions=["ask_human", "reply_to_human"])
class RoleZero(Role):
"""A role serving as the basis for other MGX roles."""
"""A role who can think and act dynamically"""
# Basic Info
name: str = "Zero"
profile: str = "RoleZero"
goal: str = ""
@ -32,21 +33,26 @@ class RoleZero(Role):
cmd_prompt: str = CMD_PROMPT
instruction: str = ROLE_INSTRUCTION
# React Mode
react_mode: Literal["react"] = "react"
max_react_loop: int = 20 # used for react mode
user_requirement: str = ""
command_rsp: str = "" # the raw string containing the commands
commands: list[dict] = [] # commands to be executed
memory_k: int = 20 # number of memories (messages) to use as historical context
# Tools
tools: list[str] = [] # Use special symbol ["<all>"] to indicate use of all registered tools
tool_recommender: ToolRecommender = None
tool_execution_map: dict[str, callable] = {}
special_tool_commands: list[str] = ["Plan.finish_current_task", "end"]
# Experience
experience_retriever: ExpRetriever = DummyExpRetriever()
# Others
user_requirement: str = ""
command_rsp: str = "" # the raw string containing the commands
commands: list[dict] = [] # commands to be executed
memory_k: int = 20 # number of memories (messages) to use as historical context
use_fixed_sop: bool = False
@model_validator(mode="after")
def set_plan_and_tool(self) -> "RoleZero":
# We force using this parameter for DataAnalyst
@ -70,6 +76,10 @@ class RoleZero(Role):
async def _think(self) -> bool:
"""Useful in 'react' mode. Use LLM to decide whether and what to do next."""
# Compatibility
if self.use_fixed_sop:
return await super()._think()
### 0. Preparation ###
if not self.rc.todo and not self.rc.news:
return False
@ -98,7 +108,7 @@ class RoleZero(Role):
tools = await self.tool_recommender.recommend_tools()
tool_info = json.dumps({tool.name: tool.schemas for tool in tools})
### Make Decision ###
### Make Decision Dynamically ###
prompt = self.cmd_prompt.format(
plan_status=plan_status,
current_task=current_task,
@ -114,6 +124,9 @@ class RoleZero(Role):
return True
async def _act(self) -> Message:
if self.use_fixed_sop:
return await super()._act()
try:
commands = json.loads(CodeParser.parse_code(block=None, lang="json", text=self.command_rsp))
except Exception as e:

View file

@ -22,6 +22,7 @@ class TeamLeader(RoleZero):
max_react_loop: int = 1 # TeamLeader only reacts once each time
tools: list[str] = ["Plan", "RoleZero", "TeamLeader"]
experience_retriever: ExpRetriever = SimpleExpRetriever()
@model_validator(mode="after")