Merge branch 'role_zero_impr' into 'mgx_ops'

TL's repetitive assignment of tasks

See merge request pub/MetaGPT!173
This commit is contained in:
林义章 2024-06-17 13:56:59 +00:00
commit b056be5384
5 changed files with 180 additions and 162 deletions

View file

@ -159,7 +159,7 @@ class Environment(ExtEnv):
"""增加一个在当前环境的角色
Add a role in the current environment
"""
self.roles[role.profile] = role
self.roles[role.name] = role
role.set_env(self)
role.context = self.context
@ -168,7 +168,7 @@ class Environment(ExtEnv):
Add a batch of characters in the current environment
"""
for role in roles:
self.roles[role.profile] = role
self.roles[role.name] = role
for role in roles: # setup system message with roles
role.context = self.context

View file

@ -17,20 +17,33 @@ from metagpt.utils.common import any_to_str, any_to_str_set
class MGXEnv(Environment):
"""MGX Environment"""
# Before enabling TL to fully take over the routing, all software company roles need to be able to handle TL messages, which requires restructuring.
allow_bypass_team_leader: bool = True
# If True, fixed software sop bypassing TL is allowed, otherwise, TL will fully take over the routing
allow_bypass_team_leader: bool = False
direct_chat_roles: set[str] = set() # record direct chat: @role_name
def _publish_message(self, message: Message, peekable: bool = True) -> bool:
return super().publish_message(message, peekable)
def publish_message(self, message: Message, user_defined_recipient: str = "", publicer: str = "") -> bool:
"""let the team leader take over message publishing"""
tl = self.get_role("Team Leader")
tl = self.get_role("Tim") # TeamLeader's name is Tim
if user_defined_recipient:
# human user's direct chat message to a certain role
if self.get_role(user_defined_recipient).is_idle:
# User starts a new direct chat with a certain role, expecting a direct chat response from the role; Other roles including TL should not be involved.
# If the role is not idle, it means the user helps the role with its current work, in this case, we handle the role's response message as usual.
self.direct_chat_roles.add(user_defined_recipient)
self._publish_message(message)
# bypass team leader, team leader only needs to know but not to react
tl.rc.memory.add(self.move_message_info_to_content(message))
# # bypass team leader, team leader only needs to know but not to react (commented out because TL doesn't understand the message well in actual experiments)
# tl.rc.memory.add(self.move_message_info_to_content(message))
elif message.sent_from in self.direct_chat_roles:
# direct chat response from a certain role to human user, team leader and other roles in the env should not be involved, no need to publish
self.direct_chat_roles.remove(message.sent_from)
elif (
self.allow_bypass_team_leader
@ -106,3 +119,6 @@ class MGXEnv(Environment):
sent_from = converted_msg.metadata[AGENT] if AGENT in converted_msg.metadata else converted_msg.sent_from
converted_msg.content = f"from {sent_from} to {converted_msg.send_to}: {converted_msg.content}"
return converted_msg
def __repr__(self):
return "MGXEnv()"

View file

@ -20,7 +20,7 @@ class Task(BaseModel):
# Available Commands
{available_commands}
Special Command: Use {{"command_name": "pass"}} to do nothing and {{"command_name": "end"}} to indicate completion of all requirements and the end of actions.
Special Command: Use {{"command_name": "end"}} to do nothing or indicate completion of all requirements and the end of actions.
# Current Plan
{plan_status}

View file

@ -30,5 +30,6 @@ FINISH_CURRENT_TASK_CMD = """
"command_name": "Plan.finish_current_task",
"args": {{}}
}
]
```
"""

View file

@ -17,164 +17,165 @@ class DummyExpRetriever(ExpRetriever):
return ""
TL_EXAMPLE = """
## example 1
User Requirement: Create a cli snake game using Python.
Explanation: The requirement is about software development. Assign each tasks to a different team member based on their expertise. When publishing message to Product Manager, we copy original user requirement directly to ensure no information loss.
```json
[
{
"command_name": "Plan.append_task",
"args": {
"task_id": "1",
"dependent_task_ids": [],
"instruction": "Create a product requirement document (PRD) outlining the features, user interface, and user experience of the CLI python snake game.",
"assignee": "Alice"
}
},
{
"command_name": "Plan.append_task",
"args": {
"task_id": "2",
"dependent_task_ids": ["1"],
"instruction": "Design the software architecture for the CLI snake game, including the choice of programming language, libraries, and data flow.",
"assignee": "Bob"
}
},
{
"command_name": "Plan.append_task",
"args": {
"task_id": "3",
"dependent_task_ids": ["2"],
"instruction": "Break down the architecture into manageable tasks, identify task dependencies, and prepare a detailed task list for implementation.",
"assignee": "Eve"
}
},
{
"command_name": "Plan.append_task",
"args": {
"task_id": "4",
"dependent_task_ids": ["3"],
"instruction": "Implement the core game logic for the CLI snake game, including snake movement, food generation, and score tracking.",
"assignee": "Alex"
}
},
{
"command_name": "Plan.append_task",
"args": {
"task_id": "5",
"dependent_task_ids": ["4"],
"instruction": "Write comprehensive tests for the game logic and user interface to ensure functionality and reliability.",
"assignee": "Edward"
}
},
{
"command_name": "TeamLeader.publish_message",
"args": {
"content": "Create a cli snake game using Python",
"send_to": "Alice"
}
},
{
"command_name": "RoleZero.reply_to_human",
"args": {
"content": "I have assigned the tasks to the team members. Alice will create the PRD, Bob will design the software architecture, Eve will break down the architecture into tasks, Alex will implement the core game logic, and Edward will write comprehensive tests. The team will work on the project accordingly",
}
},
{
"command_name": "end"
}
]
```
## example 2
User Requirement: Run data analysis on sklearn Wine recognition dataset, include a plot, and train a model to predict wine class (20% as validation), and show validation accuracy.
Explanation: DON'T decompose requirement if it is a DATA-RELATED task, assign a single task directly to Data Analyst David. He will manage the decomposition and implementation.
```json
[
{
"command_name": "Plan.append_task",
"args": {
"task_id": "1",
"dependent_task_ids": [],
"instruction": "Run data analysis on sklearn Wine recognition dataset, include a plot, and train a model to predict wine class (20% as validation), and show validation accuracy.",
"assignee": "David"
}
},
{
"command_name": "TeamLeader.publish_message",
"args": {
"content": "Run data analysis on sklearn Wine recognition dataset, include a plot, and train a model to predict wine class (20% as validation), and show validation accuracy.",
"send_to": "David"
}
},
{
"command_name": "RoleZero.reply_to_human",
"args": {
"content": "I have assigned the task to David. He will break down the task further by himself and starts solving it.",
}
},
{
"command_name": "end"
}
]
```
## example 3
Conversation History:
[
...,
{'role': 'assistant', 'content': 'from Alice(Product Manager) to {'Bob'}: {'docs': {'20240424153821.json': {'root_path': 'docs/prd', 'filename': '20240424153821.json', 'content': '{"Language":"en_us","Programming Language":"Python","Original Requirements":"create a cli snake game","Project Name":"snake_game","Product Goals":["Develop an intuitive and addictive snake game",...], ...}}}}},
]
Explanation: You received a message from Alice, the Product Manager, that she has completed the PRD, use Plan.finish_current_task to mark her task as finished and moves the plan to the next task. Based on plan status, next task is for Bob (Architect), publish a message asking him to start. The message content should contain important path info.
```json
[
{
"command_name": "Plan.finish_current_task",
"args": {}
},
{
"command_name": "TeamLeader.publish_message",
"args": {
"content": "Please design the software architecture for the snake game based on the PRD created by Alice. The PRD is at 'docs/prd/20240424153821.json'. Include the choice of programming language, libraries, and data flow, etc.",
"send_to": "Bob"
}
},
{
"command_name": "RoleZero.reply_to_human",
"args": {
"content": "Alice has completed the PRD. I have marked her task as finished and sent the PRD to Bob. Bob will work on the software architecture.",
}
},
{
"command_name": "end"
}
]
```
## example 4
User Question: how does the project go?
Explanation: The user is asking for a general update on the project status. Give a straight answer about the current task the team is working on and provide a summary of the completed tasks.
```json
[
{
"command_name": "RoleZero.reply_to_human",
"args": {
"content": "The team is currently working on ... We have completed ...",
}
},
{
"command_name": "end"
}
]
```
"""
class SimpleExpRetriever(ExpRetriever):
"""A simple experience retriever that returns manually crafted examples."""
EXAMPLE: str = """
## example 1
User Requirement: Create a cli snake game using Python.
Explanation: The requirement is about software development. Assign each tasks to a different team member based on their expertise. When publishing message to Product Manager, we copy original user requirement directly to ensure no information loss.
```json
[
{
"command_name": "Plan.append_task",
"args": {
"task_id": "1",
"dependent_task_ids": [],
"instruction": "Create a product requirement document (PRD) outlining the features, user interface, and user experience of the CLI python snake game.",
"assignee": "Alice"
}
},
{
"command_name": "Plan.append_task",
"args": {
"task_id": "2",
"dependent_task_ids": ["1"],
"instruction": "Design the software architecture for the CLI snake game, including the choice of programming language, libraries, and data flow.",
"assignee": "Bob"
}
},
{
"command_name": "Plan.append_task",
"args": {
"task_id": "3",
"dependent_task_ids": ["2"],
"instruction": "Break down the architecture into manageable tasks, identify task dependencies, and prepare a detailed task list for implementation.",
"assignee": "Eve"
}
},
{
"command_name": "Plan.append_task",
"args": {
"task_id": "4",
"dependent_task_ids": ["3"],
"instruction": "Implement the core game logic for the CLI snake game, including snake movement, food generation, and score tracking.",
"assignee": "Alex"
}
},
{
"command_name": "Plan.append_task",
"args": {
"task_id": "5",
"dependent_task_ids": ["4"],
"instruction": "Write comprehensive tests for the game logic and user interface to ensure functionality and reliability.",
"assignee": "Edward"
}
},
{
"command_name": "TeamLeader.publish_message",
"args": {
"content": "Create a cli snake game using Python",
"send_to": "Alice"
}
},
{
"command_name": "RoleZero.reply_to_human",
"args": {
"content": "I have assigned the tasks to the team members. Alice will create the PRD, Bob will design the software architecture, Eve will break down the architecture into tasks, Alex will implement the core game logic, and Edward will write comprehensive tests. The team will work on the project accordingly",
}
},
{
"command_name": "end"
}
]
```
## example 2
User Requirement: Run data analysis on sklearn Wine recognition dataset, include a plot, and train a model to predict wine class (20% as validation), and show validation accuracy.
Explanation: DON'T decompose requirement if it is a DATA-RELATED task, assign a single task directly to Data Analyst David. He will manage the decomposition and implementation.
```json
[
{
"command_name": "Plan.append_task",
"args": {
"task_id": "1",
"dependent_task_ids": [],
"instruction": "Run data analysis on sklearn Wine recognition dataset, include a plot, and train a model to predict wine class (20% as validation), and show validation accuracy.",
"assignee": "David"
}
},
{
"command_name": "TeamLeader.publish_message",
"args": {
"content": "Run data analysis on sklearn Wine recognition dataset, include a plot, and train a model to predict wine class (20% as validation), and show validation accuracy.",
"send_to": "David"
}
},
{
"command_name": "RoleZero.reply_to_human",
"args": {
"content": "I have assigned the task to David. He will break down the task further by himself and starts solving it.",
}
},
{
"command_name": "end"
}
]
```
## example 3
Conversation History:
[
...,
{'role': 'assistant', 'content': 'from Alice(Product Manager) to {'Bob'}: {'docs': {'20240424153821.json': {'root_path': 'docs/prd', 'filename': '20240424153821.json', 'content': '{"Language":"en_us","Programming Language":"Python","Original Requirements":"create a cli snake game","Project Name":"snake_game","Product Goals":["Develop an intuitive and addictive snake game",...], ...}}}}},
]
Explanation: You received a message from Alice, the Product Manager, that she has completed the PRD, use Plan.finish_current_task to mark her task as finished and moves the plan to the next task. Based on plan status, next task is for Bob (Architect), publish a message asking him to start. The message content should contain important path info.
```json
[
{
"command_name": "Plan.finish_current_task",
"args": {}
},
{
"command_name": "TeamLeader.publish_message",
"args": {
"content": "Please design the software architecture for the snake game based on the PRD created by Alice. The PRD is at 'docs/prd/20240424153821.json'. Include the choice of programming language, libraries, and data flow, etc.",
"send_to": "Bob"
}
},
{
"command_name": "RoleZero.reply_to_human",
"args": {
"content": "Alice has completed the PRD. I have marked her task as finished and sent the PRD to Bob. Bob will work on the software architecture.",
}
},
{
"command_name": "end"
}
]
```
## example 4
User Question: how does the project go?
Explanation: The user is asking for a general update on the project status. Give a straight answer about the current task the team is working on and provide a summary of the completed tasks.
```json
[
{
"command_name": "RoleZero.reply_to_human",
"args": {
"content": "The team is currently working on ... We have completed ...",
}
},
{
"command_name": "end"
}
]
```
"""
def retrieve(self, context: str = "") -> str:
return self.EXAMPLE
return TL_EXAMPLE
class KeywordExpRetriever(ExpRetriever):