Handoffs

Orchestrator-Workers Workflow

Handoffs example

It’s very simple to implement the Orchestrator-Workers workflow with tool calls in AgentScope. First, we create a function to allow the orchestrator to create workers dynamically.

  1. Orchestrator: {
  2. "type": "tool_use",
  3. "name": "create_worker",
  4. "input": {
  5. "task_description": "Print 'Hello, world!' using Python."
  6. },
  7. "id": "call_1d90b56b99974e669eb90d"
  8. }
  9. Worker: {
  10. "type": "tool_use",
  11. "name": "execute_python_code",
  12. "input": {
  13. "code": "print('Hello, world!')",
  14. "timeout": 5
  15. },
  16. "id": "call_1384e53003b941fcba6cc8"
  17. }
  18. system: {
  19. "type": "tool_result",
  20. "id": "call_1384e53003b941fcba6cc8",
  21. "name": "execute_python_code",
  22. "output": [
  23. {
  24. "type": "text",
  25. "text": "<returncode>0</returncode><stdout>Hello, world!\n</stdout><stderr></stderr>"
  26. }
  27. ]
  28. }
  29. Worker: The Python code has been executed and it printed:
  30. ```
  31. Hello, world!
  32. ```
  33. system: {
  34. "type": "tool_result",
  35. "id": "call_1d90b56b99974e669eb90d",
  36. "name": "create_worker",
  37. "output": [
  38. {
  39. "type": "text",
  40. "text": "The Python code has been executed and it printed:\n\n```\nHello, world!\n```"
  41. }
  42. ]
  43. }
  44. Orchestrator: The Python code has been successfully executed and it printed:
  45. ```
  46. Hello, world!
  47. ```
  1. import asyncio
  2. import os
  3. from agentscope.agent import ReActAgent
  4. from agentscope.formatter import DashScopeChatFormatter
  5. from agentscope.memory import InMemoryMemory
  6. from agentscope.message import Msg
  7. from agentscope.model import DashScopeChatModel
  8. from agentscope.tool import (
  9. ToolResponse,
  10. Toolkit,
  11. execute_python_code,
  12. )
  13. # The tool function to create a worker
  14. async def create_worker(
  15. task_description: str,
  16. ) -> ToolResponse:
  17. """Create a worker to finish the given task. The worker is equipped with python execution tool.
  18. Args:
  19. task_description (``str``):
  20. The description of the task to be finished by the worker.
  21. """
  22. # Equip the worker agent with some tools
  23. toolkit = Toolkit()
  24. toolkit.register_tool_function(execute_python_code)
  25. # Create a worker agent
  26. worker = ReActAgent(
  27. name="Worker",
  28. sys_prompt="You're a worker agent. Your target is to finish the given task.",
  29. model=DashScopeChatModel(
  30. model_name="qwen-max",
  31. api_key=os.environ["DASHSCOPE_API_KEY"],
  32. stream=False,
  33. ),
  34. formatter=DashScopeChatFormatter(),
  35. toolkit=toolkit,
  36. )
  37. # Let the worker finish the task
  38. res = await worker(Msg("user", task_description, "user"))
  39. return ToolResponse(
  40. content=res.get_content_blocks("text"),
  41. )
  42. async def run_handoffs() -> None:
  43. """Example of handoffs workflow."""
  44. # Initialize the orchestrator agent
  45. toolkit = Toolkit()
  46. toolkit.register_tool_function(create_worker)
  47. orchestrator = ReActAgent(
  48. name="Orchestrator",
  49. sys_prompt="You're an orchestrator agent. Your target is to finish the given task by decomposing it into smaller tasks and creating workers to finish them.",
  50. model=DashScopeChatModel(
  51. model_name="qwen-max",
  52. api_key=os.environ["DASHSCOPE_API_KEY"],
  53. stream=False,
  54. ),
  55. memory=InMemoryMemory(),
  56. formatter=DashScopeChatFormatter(),
  57. toolkit=toolkit,
  58. )
  59. # The task description
  60. task_description = "Execute hello world in Python"
  61. # Create a worker to finish the task
  62. await orchestrator(Msg("user", task_description, "user"))
  63. asyncio.run(run_handoffs())

Total running time of the script: (0 minutes 9.689 seconds)

Download Jupyter notebook: workflow_handoffs.ipynb

Download Python source code: workflow_handoffs.py

Download zipped: workflow_handoffs.zip

Gallery generated by Sphinx-Gallery