Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update role atomization capacity example #1695

Open
wants to merge 7 commits into
base: mgx_ops
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions examples/di/atomization_capacity_plan.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
from metagpt.logs import logger
from metagpt.roles.di.team_leader import TeamLeader
import fire


async def main():
tl = TeamLeader()
logger.info("\n=== Adding Initial Tasks ===")
tl.planner.plan.append_task(
task_id="T1",
dependent_task_ids=[],
instruction="Create Product Requirements Document (PRD)",
assignee="Alice"
)
tl.planner.plan.append_task(
task_id="T2",
dependent_task_ids=["T1"],
instruction="Design System Architecture",
assignee="Bob"
)

# 2. Add Development Tasks
logger.info("\n=== Adding Development Tasks ===")
tl.planner.plan.append_task(
task_id="T3",
dependent_task_ids=["T2"],
instruction="Implement Core Function Modules",
assignee="Alex"
)

tl.planner.plan.append_task(
task_id="T4",
dependent_task_ids=["T2"],
instruction="Implement User Interface",
assignee="Alex"
)

# 3. Complete Some Tasks
logger.info("\n=== Execute and Complete Tasks ===")
logger.info(f"Current Task: {tl.planner.plan.current_task.instruction}")
tl.planner.plan.finish_current_task() # Complete T1

logger.info(f"Current Task: {tl.planner.plan.current_task.instruction}")
tl.planner.plan.finish_current_task() # Complete T2

# 4. Replace Tasks
logger.info("\n=== Replace Task ===")
tl.planner.plan.replace_task(
task_id="T3",
new_dependent_task_ids=["T2"],
new_instruction="Implement Core Function Modules (Add New Features)",
new_assignee="Senior_Developer"
)

# 5. Add Testing Tasks
logger.info("\n=== Add Testing Tasks ===")
tl.planner.plan.append_task(
task_id="T5",
dependent_task_ids=["T3", "T4"],
instruction="Execute Integration Tests",
assignee="Edward"
)

# 6. Reset Task Demonstration
logger.info("\n=== Reset Task ===")
logger.info("Reset Task T3 (This will also reset T5 which depends on it)")
tl.planner.plan.reset_task("T3")

# Display Final Status
logger.info("\n=== Final Status ===")
logger.info(f"Completed Tasks: {len([t for t in tl.planner.plan.tasks if t.is_finished])}")
logger.info(f"Current Task: {tl.planner.plan.current_task.instruction}")
logger.info("All Tasks:")
for task in tl.planner.plan.tasks:
logger.info(f"- {task.task_id}: {task.instruction} (Completed: {task.is_finished})")

if __name__ == "__main__":
fire.Fire(main)
22 changes: 22 additions & 0 deletions examples/di/automated_planning_of_tasks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import fire

from metagpt.logs import logger
from metagpt.roles.di.team_leader import TeamLeader


async def main():
# Create an instance of TeamLeader
tl = TeamLeader()

# Update the plan with the goal to create a 2048 game
# This will auto generate tasks needed to accomplish the goal
await tl.planner.update_plan(goal="create a 2048 game.")

# Iterate through all tasks in the plan
# Log each task's ID, instruction and completion status
for task in tl.planner.plan.tasks:
logger.info(f"- {task.task_id}: {task.instruction} (Completed: {task.is_finished})")


if __name__ == "__main__":
fire.Fire(main)
40 changes: 40 additions & 0 deletions examples/di/data_analyst_write_code.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import fire

from metagpt.logs import logger
from metagpt.roles.di.data_analyst import DataAnalyst


async def main():
# Create an instance of DataAnalyst role
analyst = DataAnalyst()

# Set the main goal for the planner - constructing a 2D array
analyst.planner.plan.goal = "construct a two-dimensional array"

# Add a specific task to the planner with detailed parameters:
# - task_id: Unique identifier for the task
# - dependent_task_ids: List of tasks that need to be completed before this one (empty in this case)
# - instruction: Description of what needs to be done
# - assignee: Who will execute the task (David)
# - task_type: Category of the task (DATA_ANALYSIS)
analyst.planner.plan.append_task(
task_id="1",
dependent_task_ids=[],
instruction="construct a two-dimensional array",
assignee="David",
task_type="DATA_ANALYSIS",
)

# Execute the code generation and execution for creating a 2D array
# The write_and_exec_code method will:
# 1. Generate the necessary code for creating a 2D array
# 2. Execute the generated code
# 3. Return the result
result = await analyst.write_and_exec_code("construct a two-dimensional array")

# Log the result of the code execution
logger.info(result)


if __name__ == "__main__":
fire.Fire(main)
37 changes: 37 additions & 0 deletions examples/di/interacting_with_human.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import fire
from metagpt.environment.mgx.mgx_env import MGXEnv
from metagpt.logs import logger
from metagpt.roles.di.team_leader import TeamLeader
from metagpt.schema import Message


async def main():
# Initialize the MetaGPT environment
env = MGXEnv()
# Add a TeamLeader role to the environment
env.add_roles([TeamLeader()])

# Get input from human user about what they want to do
human_rsp = await env.ask_human("What do you want to do?")

# Log the human response for tracking
logger.info(human_rsp)
# Create and publish a message with the human response in the environment
env.publish_message(Message(content=human_rsp, role="user"))

# Get the TeamLeader role instance named 'Mike'
tl = env.get_role("Mike")
# Execute the TeamLeader's tasks
await tl.run()

# Log information about each task in the TeamLeader's plan
for task in tl.planner.plan.tasks:
logger.info(f"- {task.task_id}: {task.instruction} (Completed: {task.is_finished})")

# Send an empty response back to the human and log it
resp = await env.reply_to_human("")
logger.info(resp)


if __name__ == "__main__":
fire.Fire(main)
11 changes: 10 additions & 1 deletion examples/use_off_the_shelf_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,20 @@

from metagpt.logs import logger
from metagpt.roles.product_manager import ProductManager
from metagpt.environment.mgx.mgx_env import MGXEnv
from metagpt.schema import Message
from metagpt.roles.di.team_leader import TeamLeader


async def main():
msg = "Write a PRD for a snake game"
role = ProductManager()
env = MGXEnv()
env.add_roles([TeamLeader(), ProductManager()])
env.publish_message(Message(content=msg, role="user"))
tl = env.get_role("Mike")
await tl.run()

role = env.get_role("Alice")
result = await role.run(msg)
logger.info(result.content[:100])

Expand Down
24 changes: 24 additions & 0 deletions examples/write_design.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import asyncio

from metagpt.logs import logger
from metagpt.roles.architect import Architect
from metagpt.environment.mgx.mgx_env import MGXEnv
from metagpt.schema import Message
from metagpt.roles.di.team_leader import TeamLeader


async def main():
msg = "Write a TRD for a snake game"
env = MGXEnv()
env.add_roles([TeamLeader(), Architect()])
env.publish_message(Message(content=msg, role="user"))
tl = env.get_role("Mike")
await tl.run()

role = env.get_role("Bob")
result = await role.run(msg)
logger.info(result)


if __name__ == "__main__":
asyncio.run(main())
24 changes: 24 additions & 0 deletions examples/write_game_code.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import asyncio

from metagpt.logs import logger
from metagpt.environment.mgx.mgx_env import MGXEnv
from metagpt.schema import Message
from metagpt.roles.di.team_leader import TeamLeader
from metagpt.roles.di.engineer2 import Engineer2


async def main():
msg = "Write code for a 2048 game"
env = MGXEnv()
env.add_roles([TeamLeader(), Engineer2()])
env.publish_message(Message(content=msg, role="user"))
tl = env.get_role("Mike")
await tl.run()

role = env.get_role("Alex")
result = await role.run(msg)
logger.info(result)


if __name__ == "__main__":
asyncio.run(main())
Loading