Handoffs
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.
Orchestrator: {"type": "tool_use","name": "create_worker","input": {"task_description": "Print 'Hello, world!' using Python."},"id": "call_1d90b56b99974e669eb90d"}Worker: {"type": "tool_use","name": "execute_python_code","input": {"code": "print('Hello, world!')","timeout": 5},"id": "call_1384e53003b941fcba6cc8"}system: {"type": "tool_result","id": "call_1384e53003b941fcba6cc8","name": "execute_python_code","output": [{"type": "text","text": "<returncode>0</returncode><stdout>Hello, world!\n</stdout><stderr></stderr>"}]}Worker: The Python code has been executed and it printed:```Hello, world!```system: {"type": "tool_result","id": "call_1d90b56b99974e669eb90d","name": "create_worker","output": [{"type": "text","text": "The Python code has been executed and it printed:\n\n```\nHello, world!\n```"}]}Orchestrator: The Python code has been successfully executed and it printed:```Hello, world!```
import asyncioimport osfrom agentscope.agent import ReActAgentfrom agentscope.formatter import DashScopeChatFormatterfrom agentscope.memory import InMemoryMemoryfrom agentscope.message import Msgfrom agentscope.model import DashScopeChatModelfrom agentscope.tool import (ToolResponse,Toolkit,execute_python_code,)# The tool function to create a workerasync 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 toolstoolkit = Toolkit()toolkit.register_tool_function(execute_python_code)# Create a worker agentworker = 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 taskres = 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 agenttoolkit = 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 descriptiontask_description = "Execute hello world in Python"# Create a worker to finish the taskawait orchestrator(Msg("user", task_description, "user"))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
当前内容版权归 AgentScope 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 AgentScope .
