.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "tutorial/workflow_handoffs.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_tutorial_workflow_handoffs.py: Handoffs ======================================== .. figure:: ../../_static/images/handoffs.png :width: 80% :align: center :alt: 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. .. GENERATED FROM PYTHON SOURCE LINES 17-93 .. rst-class:: sphx-glr-script-out .. code-block:: none Orchestrator: { "type": "tool_use", "name": "create_worker", "input": { "task_description": "Execute a Python script that prints 'Hello, World!' to the console." }, "id": "call_5e34ca3ab1744ff2a945c6" } Worker: { "type": "tool_use", "name": "execute_python_code", "input": { "code": "print('Hello, World!')", "timeout": 5 }, "id": "call_310a45d33dad4fe596c10e" } system: { "type": "tool_result", "id": "call_310a45d33dad4fe596c10e", "name": "execute_python_code", "output": [ { "type": "text", "text": "0Hello, World!\n" } ] } Worker: The Python script has been executed successfully and printed 'Hello, World!' to the console. system: { "type": "tool_result", "id": "call_5e34ca3ab1744ff2a945c6", "name": "create_worker", "output": [ { "type": "text", "text": "The Python script has been executed successfully and printed 'Hello, World!' to the console." } ] } Orchestrator: The Python script has been executed and printed 'Hello, World!' to the console. | .. code-block:: Python import asyncio import os from agentscope.agent import ReActAgent from agentscope.formatter import DashScopeChatFormatter from agentscope.memory import InMemoryMemory from agentscope.message import Msg from agentscope.model import DashScopeChatModel from agentscope.tool import ( ToolResponse, Toolkit, execute_python_code, ) # The tool function to create a worker async def create_worker( task_description: str, ) -> ToolResponse: """Create a worker to finish the given task. The worker is equipped with python execution tool. Args: task_description (``str``): The description of the task to be finished by the worker. """ # Equip the worker agent with some tools toolkit = Toolkit() toolkit.register_tool_function(execute_python_code) # Create a worker agent worker = ReActAgent( name="Worker", sys_prompt="You're a worker agent. Your target is to finish the given task.", model=DashScopeChatModel( model_name="qwen-max", api_key=os.environ["DASHSCOPE_API_KEY"], stream=False, ), formatter=DashScopeChatFormatter(), toolkit=toolkit, ) # Let the worker finish the task res = await worker(Msg("user", task_description, "user")) return ToolResponse( content=res.get_content_blocks("text"), ) async def run_handoffs() -> None: """Example of handoffs workflow.""" # Initialize the orchestrator agent toolkit = Toolkit() toolkit.register_tool_function(create_worker) orchestrator = ReActAgent( name="Orchestrator", 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.", model=DashScopeChatModel( model_name="qwen-max", api_key=os.environ["DASHSCOPE_API_KEY"], stream=False, ), memory=InMemoryMemory(), formatter=DashScopeChatFormatter(), toolkit=toolkit, ) # The task description task_description = "Execute hello world in Python" # Create a worker to finish the task await orchestrator(Msg("user", task_description, "user")) asyncio.run(run_handoffs()) .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 11.629 seconds) .. _sphx_glr_download_tutorial_workflow_handoffs.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: workflow_handoffs.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: workflow_handoffs.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: workflow_handoffs.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_